From 3cde63a11b6b779f904dc123b2c4711dde2cd102 Mon Sep 17 00:00:00 2001 From: Richard Si <63936253+ichard26@users.noreply.github.com> Date: Sat, 29 Aug 2020 18:56:13 -0400 Subject: [PATCH 1/2] Fix incorrect space before colon in if/while stmts Previously Black would format this code ``` if (foo := True): print(foo) ``` as ``` if (foo := True) : print(foo) ``` adding an incorrect space after the RPAR. Buggy code in the normalize_invisible_parens function caused the colon to be wrapped in invisible parentheses. The LPAR of that pair was then prefixed with a single space at the request of the whitespace function. This commit fixes the accidental skipping of a pre-condition check which must return True before parenthesis normalization of a specific child Leaf or Node can happen. The pre-condition check being skipped was why the colon was wrapped in invisible parentheses. --- src/black/__init__.py | 4 ++-- tests/data/pep_572.py | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/black/__init__.py b/src/black/__init__.py index 048e771ce96..64a18655905 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -5190,9 +5190,9 @@ def normalize_invisible_parens(node: Node, parens_after: Set[str]) -> None: if check_lpar: if is_walrus_assignment(child): - continue + pass - if child.type == syms.atom: + elif child.type == syms.atom: if maybe_make_parens_invisible_in_atom(child, parent=node): wrap_in_parentheses(node, child, visible=False) elif is_one_tuple(child): diff --git a/tests/data/pep_572.py b/tests/data/pep_572.py index 9e429f913ce..637b3bb38c6 100644 --- a/tests/data/pep_572.py +++ b/tests/data/pep_572.py @@ -2,6 +2,8 @@ (a := a) if (match := pattern.search(data)) is None: pass +if (match := pattern.search(data)): + pass [y := f(x), y ** 2, y ** 3] filtered_data = [y for x in data if (y := f(x)) is None] (y := f(x)) @@ -41,3 +43,5 @@ def foo(answer: (p := 42) = 5): while x := f(x): pass +while (x := f(x)): + pass From 188814dcaaa9e6ccfaa3d3d19fe590d371c2748b Mon Sep 17 00:00:00 2001 From: Richard Si <63936253+ichard26@users.noreply.github.com> Date: Sat, 29 Aug 2020 19:58:03 -0400 Subject: [PATCH 2/2] Add an entry in CHANGES.md --- CHANGES.md | 3 +++ docs/change_log.md | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 1c53604d4d5..7e356f1f29c 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -7,6 +7,9 @@ - `Black` now respects `--skip-string-normalization` when normalizing multiline docstring quotes (#1637) +- `Black` no longer adds an incorrect space after a parenthesized assignment expression + in if/while statements (#1655) + - fixed a crash when PWD=/ on POSIX (#1631) ### 20.8b1 diff --git a/docs/change_log.md b/docs/change_log.md index b7337166659..cc5015f873c 100644 --- a/docs/change_log.md +++ b/docs/change_log.md @@ -9,6 +9,11 @@ - `Black` now respects `--skip-string-normalization` when normalizing multiline docstring quotes (#1637) +- `Black` no longer adds an incorrect space after a parenthesized assignment expression + in if/while statements (#1655) + +- fixed a crash when PWD=/ on POSIX (#1631) + ### 20.8b1 #### _Packaging_