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

Remove NBSP at the beggining of comments #2092

Merged
merged 3 commits into from Apr 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -4,6 +4,8 @@

#### _Black_

- `Black` now cleans up leading non-breaking spaces in comments (#2092)

- `Black` now respects `--skip-string-normalization` when normalizing multiline
docstring quotes (#1637)

Expand Down
7 changes: 7 additions & 0 deletions src/black/__init__.py
Expand Up @@ -2709,6 +2709,13 @@ def make_comment(content: str) -> str:

if content[0] == "#":
content = content[1:]
NON_BREAKING_SPACE = " "
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
return "#" + content
Expand Down
7 changes: 6 additions & 1 deletion 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 @@ -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]
44 changes: 44 additions & 0 deletions tests/data/comments_non_breaking_space.py
@@ -0,0 +1,44 @@
from .config import ( ConfigTypeAttributes, Int, Path, # String,
# DEFAULT_TYPE_ATTRIBUTES,
)

result = 1 # A simple comment
result = ( 1, ) # Another one

result = 1 # type: ignore
result = 1# This comment is talking about type: ignore
square = Square(4) # type: Optional[Square]

def function(a:int=42):
""" This docstring is already formatted
a
b
"""
#  There's a NBSP + 3 spaces before
# And 4 spaces on the next line
pass

# output
from .config import (
ConfigTypeAttributes,
Int,
Path, # String,
# DEFAULT_TYPE_ATTRIBUTES,
)

result = 1 # A simple comment
result = (1,) # Another one

result = 1 #  type: ignore
result = 1 # This comment is talking about type: ignore
square = Square(4) #  type: Optional[Square]


def function(a: int = 42):
"""This docstring is already formatted
a
b
"""
# There's a NBSP + 3 spaces before
# And 4 spaces on the next line
pass
1 change: 1 addition & 0 deletions tests/test_format.py
Expand Up @@ -27,6 +27,7 @@
"comments5",
"comments6",
"comments7",
"comments_non_breaking_space",
"comment_after_escaped_newline",
"composition",
"composition_no_trailing_comma",
Expand Down