From 92b377556e24616d5980a9010cf558da7fa35d28 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=C5=81ukasz=20Langa?= Date: Wed, 21 Mar 2018 18:23:46 -0700 Subject: [PATCH] Don't omit whitespace when the factor is not a math operator Fixes #55 --- README.md | 5 +++++ black.py | 7 ++++++- tests/expression.py | 4 ++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 20ab4be..aba30ab 100644 --- a/README.md +++ b/README.md @@ -293,6 +293,11 @@ More details can be found in [CONTRIBUTING](CONTRIBUTING.md). ## Change Log +### 18.3a4 (unreleased) + +* don't omit whitespace if the previous factor leaf wasn't a math + operator (#55) + ### 18.3a3 * don't remove single empty lines outside of bracketed expressions diff --git a/black.py b/black.py index 1c07de7..0dd7630 100644 --- a/black.py +++ b/black.py @@ -340,6 +340,7 @@ MATH_OPERATORS = { token.AMPER, token.PERCENT, token.CIRCUMFLEX, + token.TILDE, token.LEFTSHIFT, token.RIGHTSHIFT, token.DOUBLESTAR, @@ -888,7 +889,11 @@ def whitespace(leaf: Leaf) -> str: # noqa C901 if prevp.parent and prevp.parent.type in {syms.subscript, syms.sliceop}: return NO - elif prevp.parent and prevp.parent.type in {syms.factor, syms.star_expr}: + elif ( + prevp.parent + and prevp.parent.type in {syms.factor, syms.star_expr} + and prevp.type in MATH_OPERATORS + ): return NO elif prev.type in OPENING_BRACKETS: diff --git a/tests/expression.py b/tests/expression.py index dbb9303..c18942e 100644 --- a/tests/expression.py +++ b/tests/expression.py @@ -63,6 +63,8 @@ str or None if (1 if True else 2) else str or bytes or None [((i ** 2) + j) for i in (1, 2, 3) for j in (1, 2, 3)] {i: 0 for i in (1, 2, 3)} {i: j for i, j in ((1, 'a'), (2, 'b'), (3, 'c'))} +{a: b * 2 for a, b in dictionary.items()} +{a: b * -2 for a, b in dictionary.items()} {k: v for k, v in this_is_a_very_long_variable_which_will_cause_a_trailing_comma_which_breaks_the_comprehension} Python3 > Python2 > COBOL Life is Life @@ -214,6 +216,8 @@ str or None if (1 if True else 2) else str or bytes or None [((i ** 2) + j) for i in (1, 2, 3) for j in (1, 2, 3)] {i: 0 for i in (1, 2, 3)} {i: j for i, j in ((1, 'a'), (2, 'b'), (3, 'c'))} +{a: b * 2 for a, b in dictionary.items()} +{a: b * -2 for a, b in dictionary.items()} { k: v for k, v in this_is_a_very_long_variable_which_will_cause_a_trailing_comma_which_breaks_the_comprehension -- 2.39.5