From e94a41f92a568706700522aaad48ebd137fe1d8b Mon Sep 17 00:00:00 2001 From: Zsolt Dollenstein Date: Sun, 22 Jul 2018 14:47:42 +0100 Subject: [PATCH] Look at actual parenthesis when generating ignored leafs. Fixes #385 --- README.md | 1 + black.py | 14 +------- tests/data/fmtonoff.py | 72 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index c34e578..61cb88a 100644 --- a/README.md +++ b/README.md @@ -823,6 +823,7 @@ More details can be found in [CONTRIBUTING](CONTRIBUTING.md). ### 18.8b0 * fix parsing of `__future__` imports with renames (#389) +* fix scope of `# fmt: off` when directly preceding `yield` and other nodes (#385) ### 18.6b4 diff --git a/black.py b/black.py index 36a180d..c9b8be9 100644 --- a/black.py +++ b/black.py @@ -797,18 +797,6 @@ UNPACKING_PARENTS = { syms.testlist_gexp, syms.testlist_star_expr, } -SURROUNDED_BY_BRACKETS = { - syms.typedargslist, - syms.arglist, - syms.subscriptlist, - syms.vfplist, - syms.import_as_names, - syms.yield_expr, - syms.testlist_gexp, - syms.testlist_star_expr, - syms.listmaker, - syms.dictsetmaker, -} TEST_DESCENDANTS = { syms.test, syms.lambdef, @@ -1853,7 +1841,7 @@ def container_of(leaf: Leaf) -> LN: if parent.type == syms.file_input: break - if parent.type in SURROUNDED_BY_BRACKETS: + if parent.prev_sibling is not None and parent.prev_sibling.type in BRACKETS: break container = parent diff --git a/tests/data/fmtonoff.py b/tests/data/fmtonoff.py index b2e9fc0..35f2889 100644 --- a/tests/data/fmtonoff.py +++ b/tests/data/fmtonoff.py @@ -48,6 +48,39 @@ something = { # fmt: off key: 'value', } + +def subscriptlist(): + atom[ + # fmt: off + 'some big and', + 'complex subscript', + # fmt: on + goes + here, andhere, + ] + +def import_as_names(): + # fmt: off + from hello import a, b + 'unformatted' + # fmt: on + +def testlist_star_expr(): + # fmt: off + a , b = *hello + 'unformatted' + # fmt: on + +def yield_expr(): + # fmt: off + yield hello + 'unformatted' + # fmt: on + 'formatted' + # fmt: off + ( yield hello ) + 'unformatted' + # fmt: on + def example(session): # fmt: off result = session\ @@ -142,6 +175,7 @@ cfg.rule( xxxxxxxxxx_xxxxxxxxxxx_xxxxxxx_xxxxxxxxx=5 ) # fmt: off +yield 'hello' # No formatting to the end of the file l=[1,2,3] d={'a':1, @@ -219,6 +253,43 @@ something = { } +def subscriptlist(): + atom[ + # fmt: off + 'some big and', + 'complex subscript', + # fmt: on + goes + here, + andhere, + ] + + +def import_as_names(): + # fmt: off + from hello import a, b + 'unformatted' + # fmt: on + + +def testlist_star_expr(): + # fmt: off + a , b = *hello + 'unformatted' + # fmt: on + + +def yield_expr(): + # fmt: off + yield hello + 'unformatted' + # fmt: on + "formatted" + # fmt: off + ( yield hello ) + 'unformatted' + # fmt: on + + def example(session): # fmt: off result = session\ @@ -327,6 +398,7 @@ cfg.rule( xxxxxxxxxx_xxxxxxxxxxx_xxxxxxx_xxxxxxxxx=5, ) # fmt: off +yield 'hello' # No formatting to the end of the file l=[1,2,3] d={'a':1, -- 2.39.5