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

fix: Don't normalize whitespace before fmt:skip comments #4146

Merged
merged 9 commits into from
Jan 25, 2024
Merged
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- For stubs, fix logic to enforce empty line after nested classes with bodies (#4141)
- Fix crash when using a walrus in a dictionary (#4155)
- Fix unnecessary parentheses when wrapping long dicts (#4135)
- Stop normalizing spaces before `# fmt: skip` comments (#4146)

### Configuration

Expand Down
12 changes: 10 additions & 2 deletions src/black/comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class ProtoComment:
newlines: int # how many newlines before the comment
consumed: int # how many characters of the original leaf's prefix did we consume
form_feed: bool # is there a form feed before the comment
leading_whitespace: str # leading whitespace before the comment, if any


def generate_comments(leaf: LN) -> Iterator[Leaf]:
Expand Down Expand Up @@ -88,7 +89,9 @@ def list_comments(prefix: str, *, is_endmarker: bool) -> List[ProtoComment]:
form_feed = False
for index, full_line in enumerate(re.split("\r?\n", prefix)):
consumed += len(full_line) + 1 # adding the length of the split '\n'
line = full_line.lstrip()
match = re.match(r"^(\s*)(\S.*|)$", full_line)
assert match
whitespace, line = match.groups()
if not line:
nlines += 1
if "\f" in full_line:
Expand All @@ -113,6 +116,7 @@ def list_comments(prefix: str, *, is_endmarker: bool) -> List[ProtoComment]:
newlines=nlines,
consumed=consumed,
form_feed=form_feed,
leading_whitespace=whitespace,
)
)
form_feed = False
Expand Down Expand Up @@ -230,7 +234,11 @@ def convert_one_fmt_off_pair(
standalone_comment_prefix += fmt_off_prefix
hidden_value = comment.value + "\n" + hidden_value
if _contains_fmt_skip_comment(comment.value, mode):
hidden_value += " " + comment.value
hidden_value += (
comment.leading_whitespace
if Preview.no_normalize_fmt_skip_whitespace in mode
else " "
) + comment.value
if hidden_value.endswith("\n"):
# That happens when one of the `ignored_nodes` ended with a NEWLINE
# leaf (possibly followed by a DEDENT).
Expand Down
1 change: 1 addition & 0 deletions src/black/mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ class Preview(Enum):
allow_form_feeds = auto()
unify_docstring_detection = auto()
respect_east_asian_width = auto()
no_normalize_fmt_skip_whitespace = auto()


class Deprecated(UserWarning):
Expand Down
9 changes: 9 additions & 0 deletions tests/data/cases/fmtskip9.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# flags: --preview
print () # fmt: skip
print () # fmt:skip


# output

print () # fmt: skip
print () # fmt:skip