From 03600c9dd2fee7e14ed51e11609993c75ab846d5 Mon Sep 17 00:00:00 2001 From: Etienne Tripier Date: Wed, 26 Apr 2023 11:35:09 -0700 Subject: [PATCH] Fix "# pyright: ignore" comment mobility --- CHANGES.md | 3 +++ src/black/nodes.py | 4 +++- tests/data/preview/comments7.py | 1 + tests/data/preview/prefer_rhs_split.py | 14 ++++++++++++++ tests/data/simple_cases/comments6.py | 26 ++++++++++++++++++++++++++ 5 files changed, 47 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 7c76bca4f6a..baf613e4120 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -14,6 +14,9 @@ +- Black now avoids breaking up lines containing a comment beginning with "# pyright: + ignore". (#3661) + ### Configuration diff --git a/src/black/nodes.py b/src/black/nodes.py index 45070909df4..dbe716fa420 100644 --- a/src/black/nodes.py +++ b/src/black/nodes.py @@ -821,7 +821,9 @@ def is_type_comment(leaf: Leaf, suffix: str = "") -> bool: Only returns true for type comments for now.""" t = leaf.type v = leaf.value - return t in {token.COMMENT, STANDALONE_COMMENT} and v.startswith("# type:" + suffix) + return t in {token.COMMENT, STANDALONE_COMMENT} and ( + v.startswith(f"# pyright:{suffix}") or v.startswith(f"# type:{suffix}") + ) def wrap_in_parentheses(parent: Node, child: LN, *, visible: bool = True) -> None: diff --git a/tests/data/preview/comments7.py b/tests/data/preview/comments7.py index ec2dc501d8e..a012286201d 100644 --- a/tests/data/preview/comments7.py +++ b/tests/data/preview/comments7.py @@ -187,6 +187,7 @@ def func(): 0.0789, a[-1], # type: ignore ) + c = call(0.0123, 0.0456, 0.0789, 0.0123, 0.0789, a[-1]) # pyright: ignore[reportGeneralTypeIssues] c = call(0.0123, 0.0456, 0.0789, 0.0123, 0.0789, a[-1]) # type: ignore c = call( 0.0123, diff --git a/tests/data/preview/prefer_rhs_split.py b/tests/data/preview/prefer_rhs_split.py index a809eacc773..f518a6ecb33 100644 --- a/tests/data/preview/prefer_rhs_split.py +++ b/tests/data/preview/prefer_rhs_split.py @@ -55,6 +55,20 @@ ) +# Make sure unsplittable pyright: ignore comments won't be moved. +some_kind_of_table[some_key] = util.some_function( # pyright: ignore[reportGeneralTypeIssues] # noqa: E501 + some_arg +).intersection(pk_cols) + +some_kind_of_table[ + some_key +] = lambda obj: obj.some_long_named_method() # pyright: ignore[reportGeneralTypeIssues] # noqa: E501 + +some_kind_of_table[ + some_key # pyright: ignore[reportGeneralTypeIssues] # noqa: E501 +] = lambda obj: obj.some_long_named_method() + + # Make sure unsplittable type ignore won't be moved. some_kind_of_table[some_key] = util.some_function( # type: ignore # noqa: E501 some_arg diff --git a/tests/data/simple_cases/comments6.py b/tests/data/simple_cases/comments6.py index 735c6aa6d7a..f493243f397 100644 --- a/tests/data/simple_cases/comments6.py +++ b/tests/data/simple_cases/comments6.py @@ -86,6 +86,19 @@ def f( def func( a=some_list[0], # type: int ): # type: () -> int + c = call( + 0.0123, + 0.0456, + 0.0789, + 0.0123, + 0.0456, + 0.0789, + 0.0123, + 0.0456, + 0.0789, + a[-1], # pyright: ignore[reportGeneralTypeIssues] + ) + c = call( 0.0123, 0.0456, @@ -99,6 +112,10 @@ def func( a[-1], # type: ignore ) + c = call( + "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa" # pyright: ignore[reportGeneralTypeIssues] + ) + c = call( "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa" # type: ignore ) @@ -108,11 +125,20 @@ def func( "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ) +AAAAAAAAAAAAA = [AAAAAAAAAAAAA] + SHARED_AAAAAAAAAAAAA + USER_AAAAAAAAAAAAA + AAAAAAAAAAAAA # pyright: ignore[reportGeneralTypeIssues] + AAAAAAAAAAAAA = [AAAAAAAAAAAAA] + SHARED_AAAAAAAAAAAAA + USER_AAAAAAAAAAAAA + AAAAAAAAAAAAA # type: ignore +call_to_some_function_asdf( + foo, + [AAAAAAAAAAAAAAAAAAAAAAA, AAAAAAAAAAAAAAAAAAAAAAA, AAAAAAAAAAAAAAAAAAAAAAA, BBBBBBBBBBBB], # pyright: ignore[reportGeneralTypeIssues] +) + call_to_some_function_asdf( foo, [AAAAAAAAAAAAAAAAAAAAAAA, AAAAAAAAAAAAAAAAAAAAAAA, AAAAAAAAAAAAAAAAAAAAAAA, BBBBBBBBBBBB], # type: ignore ) +aaaaaaaaaaaaa, bbbbbbbbb = map(list, map(itertools.chain.from_iterable, zip(*items))) # pyright: ignore[reportGeneralTypeIssues] + aaaaaaaaaaaaa, bbbbbbbbb = map(list, map(itertools.chain.from_iterable, zip(*items))) # type: ignore[arg-type]