From b97ec62368ac57b29a0ccd5fc68ba875418eb8cc Mon Sep 17 00:00:00 2001 From: Batuhan Taskaya Date: Sat, 18 Dec 2021 00:43:14 +0300 Subject: [PATCH 1/1] Imply 3.8+ when annotated assigments used with unparenthesized tuples (#2708) --- CHANGES.md | 2 ++ src/black/__init__.py | 7 +++++++ src/black/mode.py | 4 ++++ tests/test_black.py | 12 ++++++++++++ 4 files changed, 25 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index ae0bf80..0452f18 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -12,6 +12,8 @@ - No longer color diff headers white as it's unreadable in light themed terminals (#2691) - Tuple unpacking on `return` and `yield` constructs now implies 3.8+ (#2700) +- Unparenthesized tuples on annotated assignments (e.g + `values: Tuple[int, ...] = 1, 2, 3`) now implies 3.8+ (#2708) ## 21.12b0 diff --git a/src/black/__init__.py b/src/black/__init__.py index 08c239d..d8b9819 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -1218,6 +1218,13 @@ def get_features_used( # noqa: C901 ): features.add(Feature.UNPACKING_ON_FLOW) + elif ( + n.type == syms.annassign + and len(n.children) >= 4 + and n.children[3].type == syms.testlist_star_expr + ): + features.add(Feature.ANN_ASSIGN_EXTENDED_RHS) + # Python 2 only features (for its deprecation) except for integers, see above elif n.type == syms.print_stmt: features.add(Feature.PRINT_STMT) diff --git a/src/black/mode.py b/src/black/mode.py index b28dcd8..bd4428a 100644 --- a/src/black/mode.py +++ b/src/black/mode.py @@ -50,6 +50,7 @@ class Feature(Enum): RELAXED_DECORATORS = 10 PATTERN_MATCHING = 11 UNPACKING_ON_FLOW = 12 + ANN_ASSIGN_EXTENDED_RHS = 13 FORCE_OPTIONAL_PARENTHESES = 50 # __future__ flags @@ -118,6 +119,7 @@ VERSION_TO_FEATURES: Dict[TargetVersion, Set[Feature]] = { Feature.ASSIGNMENT_EXPRESSIONS, Feature.POS_ONLY_ARGUMENTS, Feature.UNPACKING_ON_FLOW, + Feature.ANN_ASSIGN_EXTENDED_RHS, }, TargetVersion.PY39: { Feature.UNICODE_LITERALS, @@ -131,6 +133,7 @@ VERSION_TO_FEATURES: Dict[TargetVersion, Set[Feature]] = { Feature.RELAXED_DECORATORS, Feature.POS_ONLY_ARGUMENTS, Feature.UNPACKING_ON_FLOW, + Feature.ANN_ASSIGN_EXTENDED_RHS, }, TargetVersion.PY310: { Feature.UNICODE_LITERALS, @@ -144,6 +147,7 @@ VERSION_TO_FEATURES: Dict[TargetVersion, Set[Feature]] = { Feature.RELAXED_DECORATORS, Feature.POS_ONLY_ARGUMENTS, Feature.UNPACKING_ON_FLOW, + Feature.ANN_ASSIGN_EXTENDED_RHS, Feature.PATTERN_MATCHING, }, } diff --git a/tests/test_black.py b/tests/test_black.py index 8726cc1..628647e 100644 --- a/tests/test_black.py +++ b/tests/test_black.py @@ -818,6 +818,18 @@ class BlackTestCase(BlackBaseTestCase): self.assertEqual(black.get_features_used(node), {Feature.UNPACKING_ON_FLOW}) node = black.lib2to3_parse("def fn(): return a, *b, c") self.assertEqual(black.get_features_used(node), {Feature.UNPACKING_ON_FLOW}) + node = black.lib2to3_parse("x = a, *b, c") + self.assertEqual(black.get_features_used(node), set()) + node = black.lib2to3_parse("x: Any = regular") + self.assertEqual(black.get_features_used(node), set()) + node = black.lib2to3_parse("x: Any = (regular, regular)") + self.assertEqual(black.get_features_used(node), set()) + node = black.lib2to3_parse("x: Any = Complex(Type(1))[something]") + self.assertEqual(black.get_features_used(node), set()) + node = black.lib2to3_parse("x: Tuple[int, ...] = a, b, c") + self.assertEqual( + black.get_features_used(node), {Feature.ANN_ASSIGN_EXTENDED_RHS} + ) def test_get_features_used_for_future_flags(self) -> None: for src, features in [ -- 2.39.5