From 08fe7f10fc4d177cbfdaf32baf5356ff1f00d1b8 Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Sun, 11 Apr 2021 19:29:31 +0200 Subject: [PATCH] Handle the case with type hint in the pre-3.6 style Also handle notmal comment not starting with 'type:' better. --- src/black/__init__.py | 6 +++++- tests/data/comments7.py | 7 ++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/black/__init__.py b/src/black/__init__.py index 40e82cc0c07..be3ba449876 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -2710,7 +2710,11 @@ def make_comment(content: str) -> str: if content[0] == "#": content = content[1:] NON_BREAKING_SPACE = " " - if content and content[0] == NON_BREAKING_SPACE and "type: ignore" not in content: + if ( + content + and content[0] == NON_BREAKING_SPACE + and not content.lstrip().startswith("type:") + ): content = " " + content[1:] # Replace NBSP by a simple space if content and content[0] not in " !:#'%": content = " " + content diff --git a/tests/data/comments7.py b/tests/data/comments7.py index 0e2bd35bf55..ca9d7c62b21 100644 --- a/tests/data/comments7.py +++ b/tests/data/comments7.py @@ -129,6 +129,8 @@ def test_fails_invalid_post_data( ): ... +square = Square(4) # type: Optional[Square] + # output from .config import ( @@ -263,4 +265,7 @@ class C: def test_fails_invalid_post_data( self, pyramid_config, db_request, post_data, message ): - ... \ No newline at end of file + ... + + +square = Square(4) # type: Optional[Square]