From e1d3e5d10aff92df65f4c55d197b6135fa4d1485 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Sun, 9 Jul 2023 15:33:07 -0700 Subject: [PATCH 1/3] Fix crash on type comment with trailing space Solves #3496 --- src/black/parsing.py | 9 ++++++--- tests/data/simple_cases/comments2.py | 5 +++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/black/parsing.py b/src/black/parsing.py index 455c5eed968..5ea14745c4e 100644 --- a/src/black/parsing.py +++ b/src/black/parsing.py @@ -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. + normalized = value.rstrip(" ") else: normalized = value yield f"{' ' * (depth+2)}{normalized!r}, # {value.__class__.__name__}" diff --git a/tests/data/simple_cases/comments2.py b/tests/data/simple_cases/comments2.py index 37e185abf4f..1487dc4b6e2 100644 --- a/tests/data/simple_cases/comments2.py +++ b/tests/data/simple_cases/comments2.py @@ -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 ### ####################### @@ -332,6 +335,8 @@ def _init_host(self, parsed) -> None: pass +a = "type comment with trailing space" # type: str + ####################### ### SECTION COMMENT ### ####################### From 2fee938603d30c4a084959e8577010d1527e4e89 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Sun, 9 Jul 2023 15:41:02 -0700 Subject: [PATCH 2/3] news --- CHANGES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 1b0475fc7b9..5d2732693d0 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -17,6 +17,8 @@ PEP 604 unions (#3735) - Fix a bug where multi-line open parenthesis magic comment like `type: ignore` were not correctly parsed (#3740) +- Fix error in AST validation when Black removes trailing whitespace in a type comment + (#3773) ### Preview style From 68afae77d9820c338497bf671049dada8a49d82d Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Sun, 9 Jul 2023 15:45:43 -0700 Subject: [PATCH 3/3] review --- src/black/parsing.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/black/parsing.py b/src/black/parsing.py index 5ea14745c4e..e98e019cac6 100644 --- a/src/black/parsing.py +++ b/src/black/parsing.py @@ -218,8 +218,8 @@ def stringify_ast(node: ast.AST, depth: int = 0) -> Iterator[str]: # 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. - normalized = value.rstrip(" ") + # Trailing whitespace in type comments is removed. + normalized = value.rstrip() else: normalized = value yield f"{' ' * (depth+2)}{normalized!r}, # {value.__class__.__name__}"