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 crash on type comment with trailing space #3773

Merged
merged 3 commits into from Jul 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 6 additions & 3 deletions src/black/parsing.py
Expand Up @@ -208,15 +208,18 @@ def stringify_ast(node: ast.AST, depth: int = 0) -> Iterator[str]:

else:
normalized: object
# Constant strings may be indented across newlines, if they are
# docstrings; fold spaces after newlines when comparing. Similarly,
# trailing and leading space may be removed.
if (
isinstance(node, ast.Constant)
and field == "value"
and isinstance(value, str)
):
# Constant strings may be indented across newlines, if they are
# docstrings; fold spaces after newlines when comparing. Similarly,
# trailing and leading space may be removed.
normalized = _normalize("\n", value)
elif field == "type_comment" and isinstance(value, str):
# Trailing spaces in type comments are removed.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Interesting that we apparently do not remove trailing tabs, only spaces. That might be a bug?

In any case, this change looks good, thanks!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh, we do remove tabs. Sorry, I forgot those exist.

Copy link
Collaborator

Choose a reason for hiding this comment

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

To be clear we actually don't strip trailing tabs:

(py311) jelle@m2mb-jelle black % black -c "x = int # type: int\t"     
x = int  # type: int\t
(py311) jelle@m2mb-jelle black % black -c "x = int # type: int   "
x = int # type: int   
error: cannot format <string>: INTERNAL ERROR: Black produced code that is not equivalent to the source.  Please report a bug on https://github.com/psf/black/issues.  This diff might be helpful: /var/folders/hh/hdb5qd2x3v51v6l0gknpfjfr0000gp/T/blk_7xu6fnsp.log

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh wait, that's not actually a tab, is it? Just a \t in the comment.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, that's not a tab. black -c $'x = int # type: int\t'

normalized = value.rstrip(" ")
else:
normalized = value
yield f"{' ' * (depth+2)}{normalized!r}, # {value.__class__.__name__}"
Expand Down
5 changes: 5 additions & 0 deletions tests/data/simple_cases/comments2.py
Expand Up @@ -154,6 +154,9 @@ def _init_host(self, parsed) -> None:
not parsed.hostname.strip()):
pass


a = "type comment with trailing space" # type: str

#######################
### SECTION COMMENT ###
#######################
Expand Down Expand Up @@ -332,6 +335,8 @@ def _init_host(self, parsed) -> None:
pass


a = "type comment with trailing space" # type: str

#######################
### SECTION COMMENT ###
#######################
Expand Down