Skip to content

Commit

Permalink
Handle the case with type hint in the pre-3.6 style
Browse files Browse the repository at this point in the history
Also handle notmal comment not starting with 'type:' better.
  • Loading branch information
Pierre-Sassoulas committed Apr 11, 2021
1 parent f66a033 commit d3288db
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/black/__init__.py
Expand Up @@ -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
Expand Down Expand Up @@ -4617,7 +4621,7 @@ def TErr(err_msg: str) -> Err[CannotTransform]:
def contains_pragma_comment(comment_list: List[Leaf]) -> bool:
"""
Returns:
True iff one of the comments in @comment_list is a pragma used by one
True if one of the comments in @comment_list is a pragma used by one
of the more common static analysis tools for python (e.g. mypy, flake8,
pylint).
"""
Expand Down
9 changes: 7 additions & 2 deletions tests/data/comments7.py
Expand Up @@ -129,6 +129,8 @@ def test_fails_invalid_post_data(
):
...

square = Square(4) # type: Optional[Square]

# output

from .config import (
Expand Down Expand Up @@ -199,7 +201,7 @@ def func():
a[-1], # type: ignore
)

#  The type: ignore exception only applies to line length, not
# The type: ignore exception only applies to line length, not
# other types of formatting.
c = call(
"aaaaaaaa",
Expand Down Expand Up @@ -263,4 +265,7 @@ class C:
def test_fails_invalid_post_data(
self, pyramid_config, db_request, post_data, message
):
...
...


square = Square(4) #  type: Optional[Square]

0 comments on commit d3288db

Please sign in to comment.