Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Imply 3.8+ when annotated assigments used with unparenthesized tuples #2708

Merged
merged 1 commit into from Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -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

Expand Down
7 changes: 7 additions & 0 deletions src/black/__init__.py
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions src/black/mode.py
Expand Up @@ -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
Expand Down Expand Up @@ -118,6 +119,7 @@ class Feature(Enum):
Feature.ASSIGNMENT_EXPRESSIONS,
Feature.POS_ONLY_ARGUMENTS,
Feature.UNPACKING_ON_FLOW,
Feature.ANN_ASSIGN_EXTENDED_RHS,
},
TargetVersion.PY39: {
Feature.UNICODE_LITERALS,
Expand All @@ -131,6 +133,7 @@ class Feature(Enum):
Feature.RELAXED_DECORATORS,
Feature.POS_ONLY_ARGUMENTS,
Feature.UNPACKING_ON_FLOW,
Feature.ANN_ASSIGN_EXTENDED_RHS,
},
TargetVersion.PY310: {
Feature.UNICODE_LITERALS,
Expand All @@ -144,6 +147,7 @@ class Feature(Enum):
Feature.RELAXED_DECORATORS,
Feature.POS_ONLY_ARGUMENTS,
Feature.UNPACKING_ON_FLOW,
Feature.ANN_ASSIGN_EXTENDED_RHS,
Feature.PATTERN_MATCHING,
},
}
Expand Down
12 changes: 12 additions & 0 deletions tests/test_black.py
Expand Up @@ -818,6 +818,18 @@ def test_get_features_used(self) -> None:
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 [
Expand Down