From 8b06aa8bdb5313701df3514aba09bcee62bd7103 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Thu, 22 Jun 2023 20:54:31 -0700 Subject: [PATCH 1/5] failing test --- tests/data/simple_cases/ignore_pyi.py | 2 ++ tests/test_format.py | 5 +++-- 2 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 tests/data/simple_cases/ignore_pyi.py diff --git a/tests/data/simple_cases/ignore_pyi.py b/tests/data/simple_cases/ignore_pyi.py new file mode 100644 index 00000000000..5e2c0fc19b3 --- /dev/null +++ b/tests/data/simple_cases/ignore_pyi.py @@ -0,0 +1,2 @@ +def f(): # type: ignore + ... diff --git a/tests/test_format.py b/tests/test_format.py index 8e0ada99cba..fb4d8eb4346 100644 --- a/tests/test_format.py +++ b/tests/test_format.py @@ -33,9 +33,10 @@ def check_file( @pytest.mark.parametrize("filename", all_data_cases("simple_cases")) def test_simple_format(filename: str) -> None: magic_trailing_comma = filename != "skip_magic_trailing_comma" - check_file( - "simple_cases", filename, black.Mode(magic_trailing_comma=magic_trailing_comma) + mode = black.Mode( + magic_trailing_comma=magic_trailing_comma, is_pyi=filename.endswith("_pyi") ) + check_file("simple_cases", filename, mode) @pytest.mark.parametrize("filename", all_data_cases("preview")) From 6574c21845fdd0c83cf1b150fcbc3dac954c5ca5 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Thu, 22 Jun 2023 21:00:43 -0700 Subject: [PATCH 2/5] Fix it --- CHANGES.md | 2 ++ src/black/nodes.py | 5 +++++ tests/data/simple_cases/ignore_pyi.py | 16 ++++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index fd4d911287d..c94b1aaa340 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -12,6 +12,8 @@ - Fix a bug where an illegal trailing comma was added to return type annotations using PEP 604 unions (#3735) +- Fix a bug where comments in stub files were removed under some circumstances. This bug + would lead to crashes if the removed comment was a `# type: ignore` (#3745). ### Preview style diff --git a/src/black/nodes.py b/src/black/nodes.py index 45070909df4..38fb505cf76 100644 --- a/src/black/nodes.py +++ b/src/black/nodes.py @@ -718,6 +718,11 @@ def is_multiline_string(leaf: Leaf) -> bool: def is_stub_suite(node: Node) -> bool: """Return True if `node` is a suite with a stub body.""" + + # If there is a comment, we want to keep it. + if node.prefix.strip(): + return False + if ( len(node.children) != 4 or node.children[0].type != token.NEWLINE diff --git a/tests/data/simple_cases/ignore_pyi.py b/tests/data/simple_cases/ignore_pyi.py index 5e2c0fc19b3..9d2bfd1571f 100644 --- a/tests/data/simple_cases/ignore_pyi.py +++ b/tests/data/simple_cases/ignore_pyi.py @@ -1,2 +1,18 @@ def f(): # type: ignore ... + +class x: # some comment + ... + +class y: + ... # comment + +# output + +def f(): # type: ignore + ... + +class x: # some comment + ... + +class y: ... # comment From 08f5e5dc473aa005363ccf3925906252888b63cc Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Thu, 22 Jun 2023 21:04:42 -0700 Subject: [PATCH 3/5] one more test --- tests/data/simple_cases/ignore_pyi.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/data/simple_cases/ignore_pyi.py b/tests/data/simple_cases/ignore_pyi.py index 9d2bfd1571f..e1eb373ad4d 100644 --- a/tests/data/simple_cases/ignore_pyi.py +++ b/tests/data/simple_cases/ignore_pyi.py @@ -7,6 +7,10 @@ class x: # some comment class y: ... # comment +# whitespace doesn't matter (note the next line has a trailing space and tab) +class z: + ... + # output def f(): # type: ignore @@ -16,3 +20,6 @@ class x: # some comment ... class y: ... # comment + +# whitespace doesn't matter (note the next line has a trailing space and tab) +class z: ... From d54164efa68af0af59409ed3b00c13de406d54b7 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Thu, 22 Jun 2023 21:14:42 -0700 Subject: [PATCH 4/5] Fix more similar bugs --- CHANGES.md | 5 +++-- src/black/nodes.py | 6 +++++- tests/data/simple_cases/ignore_pyi.py | 16 ++++++++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index c94b1aaa340..003a2ffb6a6 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -12,8 +12,9 @@ - Fix a bug where an illegal trailing comma was added to return type annotations using PEP 604 unions (#3735) -- Fix a bug where comments in stub files were removed under some circumstances. This bug - would lead to crashes if the removed comment was a `# type: ignore` (#3745). +- Fix several bugs where comments in stub files were removed under some circumstances. + These bugs would lead to crashes if the removed comment was a `# type: ignore` + (#3745). ### Preview style diff --git a/src/black/nodes.py b/src/black/nodes.py index 38fb505cf76..6ea7eb07340 100644 --- a/src/black/nodes.py +++ b/src/black/nodes.py @@ -731,6 +731,9 @@ def is_stub_suite(node: Node) -> bool: ): return False + if node.children[3].prefix.strip(): + return False + return is_stub_body(node.children[2]) @@ -744,7 +747,8 @@ def is_stub_body(node: LN) -> bool: child = node.children[0] return ( - child.type == syms.atom + not child.prefix.strip() + and child.type == syms.atom and len(child.children) == 3 and all(leaf == Leaf(token.DOT, ".") for leaf in child.children) ) diff --git a/tests/data/simple_cases/ignore_pyi.py b/tests/data/simple_cases/ignore_pyi.py index e1eb373ad4d..3ef61079bfe 100644 --- a/tests/data/simple_cases/ignore_pyi.py +++ b/tests/data/simple_cases/ignore_pyi.py @@ -11,6 +11,14 @@ class y: class z: ... +def g(): + # hi + ... + +def h(): + ... + # bye + # output def f(): # type: ignore @@ -23,3 +31,11 @@ class y: ... # comment # whitespace doesn't matter (note the next line has a trailing space and tab) class z: ... + +def g(): + # hi + ... + +def h(): + ... + # bye From 18a64969f093e190aa22b7c7b8f630a50db254c2 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Thu, 22 Jun 2023 21:16:01 -0700 Subject: [PATCH 5/5] Improve message --- CHANGES.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 003a2ffb6a6..cc7a346293a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -12,9 +12,8 @@ - Fix a bug where an illegal trailing comma was added to return type annotations using PEP 604 unions (#3735) -- Fix several bugs where comments in stub files were removed under some circumstances. - These bugs would lead to crashes if the removed comment was a `# type: ignore` - (#3745). +- Fix several bugs and crashes where comments in stub files were removed or mishandled + under some circumstances. (#3745). ### Preview style