Unverified Commit d960d5d2 authored by Pierre Sassoulas's avatar Pierre Sassoulas Committed by GitHub
Browse files

Remove NBSP at the beginning of comments (#2092)

Closes #2091
parent ea4e714b
Loading
Loading
Loading
Loading
+2 −0
Changes for CHANGES.md: 2 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -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)

+7 −0
Changes for src/black/__init__.py: 7 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -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
+5 −0
Changes for tests/data/comments7.py: 5 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -129,6 +129,8 @@ def test_fails_invalid_post_data(
    ):
        ...

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

# output

from .config import (
@@ -264,3 +266,6 @@ def test_fails_invalid_post_data(
        self, pyramid_config, db_request, post_data, message
    ):
        ...


square = Square(4)  # type: Optional[Square]
+44 −0
Changes for tests/data/comments_non_breaking_space.py: 44 added lines, 0 removed lines.
Original line number Diff line number Diff line
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 −0
Changes for tests/test_format.py: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@
    "comments5",
    "comments6",
    "comments7",
    "comments_non_breaking_space",
    "comment_after_escaped_newline",
    "composition",
    "composition_no_trailing_comma",