From b420d190b8f0782c0a7f68996a8a5d07799e8d01 Mon Sep 17 00:00:00 2001 From: Yilei Yang Date: Wed, 6 Jul 2022 11:44:07 -0700 Subject: [PATCH 1/7] Fix an infinite loop when handling `# fmt: on/off`. --- src/black/comments.py | 1 + tests/data/simple_cases/fmtonoff5.py | 11 +++++++++++ 2 files changed, 12 insertions(+) create mode 100644 tests/data/simple_cases/fmtonoff5.py diff --git a/src/black/comments.py b/src/black/comments.py index 23bf87fca7c..67587e93092 100644 --- a/src/black/comments.py +++ b/src/black/comments.py @@ -241,6 +241,7 @@ def generate_ignored_nodes( if contains_fmt_on_at_column(child, leaf.column, preview=preview): return yield child + return else: yield container container = container.next_sibling diff --git a/tests/data/simple_cases/fmtonoff5.py b/tests/data/simple_cases/fmtonoff5.py new file mode 100644 index 00000000000..ad4d6455dae --- /dev/null +++ b/tests/data/simple_cases/fmtonoff5.py @@ -0,0 +1,11 @@ +# Regression test for https://github.com/psf/black/issues/3129. +setup( + entry_points={ + # fmt: off + "console_scripts": [ + "foo-bar" + "=foo.bar.:main", + # fmt: on + ] + }, +) From 3a32173756a9f3b41f9c3807434c01c975202f2f Mon Sep 17 00:00:00 2001 From: Yilei Yang Date: Wed, 6 Jul 2022 11:50:08 -0700 Subject: [PATCH 2/7] Update CHANGES.md --- CHANGES.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 0bfa7ccd62c..ceae1800610 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,6 +10,9 @@ +- Fix an infinite loop when using `#fmt: on/off` on lines with unmatching brackets. + (#3158) + ### Preview style From f09195eb376ffccc9dbc8afc65a02170c53fa15f Mon Sep 17 00:00:00 2001 From: Yilei Yang Date: Wed, 6 Jul 2022 12:45:46 -0700 Subject: [PATCH 3/7] Add more test cases from other reported issues. --- tests/data/simple_cases/fmtonoff5.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/data/simple_cases/fmtonoff5.py b/tests/data/simple_cases/fmtonoff5.py index ad4d6455dae..14ae05f4973 100644 --- a/tests/data/simple_cases/fmtonoff5.py +++ b/tests/data/simple_cases/fmtonoff5.py @@ -9,3 +9,29 @@ ] }, ) + + +# Regression test for https://github.com/psf/black/issues/3129. +run( + # fmt: off + [ + "ls", + "-la", + ] + # fmt: on + + path + , + check=True, +) + + +# Regression test for https://github.com/psf/black/issues/3026. +def test_func(): + # yapf: disable + if a: + return True + # yapf: enable + elif b: + return True + + return False From 6bca8a2f8fea1d2fde6186562e539f345b7b2913 Mon Sep 17 00:00:00 2001 From: Yilei Yang Date: Wed, 6 Jul 2022 12:48:19 -0700 Subject: [PATCH 4/7] Update CHANGES.md for expanded scope. --- CHANGES.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index ceae1800610..f2cfb267568 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,8 +10,8 @@ -- Fix an infinite loop when using `#fmt: on/off` on lines with unmatching brackets. - (#3158) +- Fix an infinite loop when using `#fmt: on/off` in the middle of an expression or code + block (#3158) ### Preview style From 676bb7385f98cb9de12f97e0e0b55930de9cad9d Mon Sep 17 00:00:00 2001 From: "Yilei \"Dolee\" Yang" Date: Thu, 7 Jul 2022 16:38:19 -0700 Subject: [PATCH 5/7] Update CHANGES.md Co-authored-by: Richard Si <63936253+ichard26@users.noreply.github.com> --- CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index f2cfb267568..4e28268ed03 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,7 +10,7 @@ -- Fix an infinite loop when using `#fmt: on/off` in the middle of an expression or code +- Fix an infinite loop when using `# fmt: on/off` in the middle of an expression or code block (#3158) ### Preview style From 3e554ed55d66dcda83145cd4673bd3013a1c9ea4 Mon Sep 17 00:00:00 2001 From: Yilei Yang Date: Thu, 7 Jul 2022 16:42:07 -0700 Subject: [PATCH 6/7] Instead of include all children, return early when encountering a Leaf node with `# fmt: on`. Also special case as a hack to include closing brackets. The alternative is to fail the formatting. --- src/black/comments.py | 11 +++++++++-- tests/data/simple_cases/fmtonoff5.py | 7 +++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/black/comments.py b/src/black/comments.py index 67587e93092..8e7d04b9645 100644 --- a/src/black/comments.py +++ b/src/black/comments.py @@ -13,7 +13,7 @@ from blib2to3.pgen2 import token from black.nodes import first_leaf_column, preceding_leaf, container_of -from black.nodes import STANDALONE_COMMENT, WHITESPACE +from black.nodes import CLOSING_BRACKETS, STANDALONE_COMMENT, WHITESPACE # types LN = Union[Leaf, Node] @@ -238,10 +238,17 @@ def generate_ignored_nodes( # fix for fmt: on in children if contains_fmt_on_at_column(container, leaf.column, preview=preview): for child in container.children: + if isinstance(child, Leaf) and is_fmt_on(child, preview=preview): + if child.type in CLOSING_BRACKETS: + # This means `# fmt: on` is placed at a different bracket level + # than `# fmt: off`. This is an invalid use, but as a courtesy, + # we include this closing bracket in the ignored nodes. + # The alternative is to fail the formatting. + yield child + return if contains_fmt_on_at_column(child, leaf.column, preview=preview): return yield child - return else: yield container container = container.next_sibling diff --git a/tests/data/simple_cases/fmtonoff5.py b/tests/data/simple_cases/fmtonoff5.py index 14ae05f4973..32186c0e01b 100644 --- a/tests/data/simple_cases/fmtonoff5.py +++ b/tests/data/simple_cases/fmtonoff5.py @@ -6,7 +6,7 @@ "foo-bar" "=foo.bar.:main", # fmt: on - ] + ] # Includes an formatted indentation. }, ) @@ -19,8 +19,7 @@ "-la", ] # fmt: on - + path - , + + path, check=True, ) @@ -28,7 +27,7 @@ # Regression test for https://github.com/psf/black/issues/3026. def test_func(): # yapf: disable - if a: + if unformatted( args ): return True # yapf: enable elif b: From 73b97129049fa50e374a806d1b6e17b7b564c3f9 Mon Sep 17 00:00:00 2001 From: Yilei Yang Date: Thu, 7 Jul 2022 16:43:54 -0700 Subject: [PATCH 7/7] Fix the link to the regression test's issue number. --- tests/data/simple_cases/fmtonoff5.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/data/simple_cases/fmtonoff5.py b/tests/data/simple_cases/fmtonoff5.py index 32186c0e01b..746aa41f4e4 100644 --- a/tests/data/simple_cases/fmtonoff5.py +++ b/tests/data/simple_cases/fmtonoff5.py @@ -11,7 +11,7 @@ ) -# Regression test for https://github.com/psf/black/issues/3129. +# Regression test for https://github.com/psf/black/issues/2015. run( # fmt: off [