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

Unpacking on flow constructs (return/yield) now implies 3.8+ #2700

Merged
merged 1 commit into from
Dec 16, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
(#2686)
- 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)

## 21.12b0

Expand Down
8 changes: 8 additions & 0 deletions src/black/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1210,6 +1210,14 @@ def get_features_used( # noqa: C901
if argch.type in STARS:
features.add(feature)

elif (
n.type in {syms.return_stmt, syms.yield_expr}
and len(n.children) >= 2
and n.children[1].type == syms.testlist_star_expr
and any(child.type == syms.star_expr for child in n.children[1].children)
):
features.add(Feature.UNPACKING_ON_FLOW)

# 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
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class Feature(Enum):
POS_ONLY_ARGUMENTS = 9
RELAXED_DECORATORS = 10
PATTERN_MATCHING = 11
UNPACKING_ON_FLOW = 12
FORCE_OPTIONAL_PARENTHESES = 50

# __future__ flags
Expand Down Expand Up @@ -116,6 +117,7 @@ class Feature(Enum):
Feature.FUTURE_ANNOTATIONS,
Feature.ASSIGNMENT_EXPRESSIONS,
Feature.POS_ONLY_ARGUMENTS,
Feature.UNPACKING_ON_FLOW,
},
TargetVersion.PY39: {
Feature.UNICODE_LITERALS,
Expand All @@ -128,6 +130,7 @@ class Feature(Enum):
Feature.ASSIGNMENT_EXPRESSIONS,
Feature.RELAXED_DECORATORS,
Feature.POS_ONLY_ARGUMENTS,
Feature.UNPACKING_ON_FLOW,
},
TargetVersion.PY310: {
Feature.UNICODE_LITERALS,
Expand All @@ -140,6 +143,7 @@ class Feature(Enum):
Feature.ASSIGNMENT_EXPRESSIONS,
Feature.RELAXED_DECORATORS,
Feature.POS_ONLY_ARGUMENTS,
Feature.UNPACKING_ON_FLOW,
Feature.PATTERN_MATCHING,
},
}
Expand Down
8 changes: 8 additions & 0 deletions tests/test_black.py
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,14 @@ def test_get_features_used(self) -> None:
self.assertEqual(black.get_features_used(node), {Feature.POS_ONLY_ARGUMENTS})
node = black.lib2to3_parse("def fn(a, /, b): ...")
self.assertEqual(black.get_features_used(node), {Feature.POS_ONLY_ARGUMENTS})
node = black.lib2to3_parse("def fn(): yield a, b")
self.assertEqual(black.get_features_used(node), set())
node = black.lib2to3_parse("def fn(): return a, b")
self.assertEqual(black.get_features_used(node), set())
node = black.lib2to3_parse("def fn(): yield *b, c")
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})

def test_get_features_used_for_future_flags(self) -> None:
for src, features in [
Expand Down