Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standardize type comments #2698

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
`case Foo(bar=baz as quux)` (#2749)
- No longer color diff headers white as it's unreadable in light themed terminals
(#2691)
- Standardize type comments to always have one space (#2698)
- Tuple unpacking on `return` and `yield` constructs now implies 3.8+ (#2700)
- Unparenthesized tuples on annotated assignments (e.g
`values: Tuple[int, ...] = 1, 2, 3`) now implies 3.8+ (#2708)
Expand Down
7 changes: 6 additions & 1 deletion src/black/comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ def make_comment(content: str) -> str:
and not content.lstrip().startswith("type:")
):
content = " " + content[1:] # Replace NBSP by a simple space
elif NON_BREAKING_SPACE not in content:
type_comment = re.match(r"\s*type\s*:\s*(.*)", content)
if type_comment:
content = "type: " + type_comment.group(1)

if content and content[0] not in " !:#'%":
content = " " + content
return "#" + content
Expand Down Expand Up @@ -271,7 +276,7 @@ def contains_pragma_comment(comment_list: List[Leaf]) -> bool:
pylint).
"""
for comment in comment_list:
if comment.value.startswith(("# type:", "# noqa", "# pylint:")):
if re.match(r"#\s*(type\s*:|pylint\s*:|noqa\s*)", comment.value):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if re.match(r"#\s*(type\s*:|pylint\s*:|noqa\s*)", comment.value):
if re.match(r"#\s*(type\s*:|pylint\s*:|noqa\s*|nosec\s*)", comment.value):

Would be nice if Bandit is supported as well.

return True

return False
7 changes: 6 additions & 1 deletion src/black/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import sys
import re
from typing import (
Collection,
Generic,
Expand Down Expand Up @@ -803,7 +804,11 @@ def is_type_comment(leaf: Leaf, suffix: str = "") -> bool:
Only returns true for type comments for now."""
t = leaf.type
v = leaf.value
return t in {token.COMMENT, STANDALONE_COMMENT} and v.startswith("# type:" + suffix)
type_comment_regex = r"#\s*type\s*:\s*" + suffix
return (
t in {token.COMMENT, STANDALONE_COMMENT}
and re.match(type_comment_regex, v) is not None
)


def wrap_in_parentheses(parent: Node, child: LN, *, visible: bool = True) -> None:
Expand Down
6 changes: 3 additions & 3 deletions tests/data/comments7.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ def func():
0.0789,
0.0123,
0.0789,
a[-1], # type: ignore
a[-1], # type: ignore
)
c = call(
0.0123,
0.0456,
0.0789,
0.0123,
0.0789,
a[-1] # type: ignore
a[-1] # type:ignore
)
c = call(
0.0123,
Expand All @@ -70,7 +70,7 @@ def func():
0.0123,
0.0456,
0.0789,
a[-1] # type: ignore
a[-1] # type : ignore
)

# The type: ignore exception only applies to line length, not
Expand Down
4 changes: 4 additions & 0 deletions tests/data/long_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@

pragma_comment_string2 = "Lines which end with an inline pragma comment of the form `# <pragma>: <...>` should be left alone." # noqa

pragma_comment_string3 = "Lines which end with an inline pragma comment of the form `# <pragma>: <...>` should be left alone." # noqa

"""This is a really really really long triple quote string and it should not be touched."""

triple_quote_string = """This is a really really really long triple quote string assignment and it should not be touched."""
Expand Down Expand Up @@ -463,6 +465,8 @@ def foo():

pragma_comment_string2 = "Lines which end with an inline pragma comment of the form `# <pragma>: <...>` should be left alone." # noqa

pragma_comment_string3 = "Lines which end with an inline pragma comment of the form `# <pragma>: <...>` should be left alone." # noqa

"""This is a really really really long triple quote string and it should not be touched."""

triple_quote_string = """This is a really really really long triple quote string assignment and it should not be touched."""
Expand Down