From: Batuhan Taskaya Date: Fri, 17 Dec 2021 21:43:14 +0000 (+0300) Subject: Imply 3.8+ when annotated assigments used with unparenthesized tuples (#2708) X-Git-Url: https://git.madduck.net/etc/vim.git/commitdiff_plain/b97ec62368ac57b29a0ccd5fc68ba875418eb8cc?ds=sidebyside Imply 3.8+ when annotated assigments used with unparenthesized tuples (#2708) --- 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 [