Skip to content

Commit

Permalink
Try 3.10 grammar more often + remove redundant out()
Browse files Browse the repository at this point in the history
Say you're attrs and you don't configure target-version. Previously
Black would default to trying every single grammar it has, including the
3.10 grammar. With this PR, now target-version is configured for you
effectively so now get_grammars() is more selective in which grammars it
returns. If any target versions don't support the match statement, the
3.10 grammar won't be tried.

And while in theory a 3.7+ project shoudn't be using 3.10 features,
attrs has a test file which uses match (which fails to parse because the
3.10 grammar isn't selected). To avoid breaking attrs, get_grammars()
will now return the 3.10 grammar as long as *any* the target versions
support match.

The out() call was made redundant by an older PR that prints the
configuration if --verbose is passed.
  • Loading branch information
ichard26 committed Jan 31, 2023
1 parent 4c8393d commit 1b5b6f4
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 5 deletions.
2 changes: 0 additions & 2 deletions src/black/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,8 +520,6 @@ def main( # noqa: C901
for param, value in ctx.default_map.items():
out(f"{param}: {value}")

out(f"Target version: {[v.name.lower() for v in target_version]}", fg="blue")

error_msg = "Oh no! 💥 💔 💥"
if (
required_version
Expand Down
6 changes: 3 additions & 3 deletions src/black/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
else:
from typing import Final

from black.mode import Feature, TargetVersion, supports_feature
from black.mode import VERSION_TO_FEATURES, Feature, TargetVersion, supports_feature
from black.nodes import syms
from blib2to3 import pygram
from blib2to3.pgen2 import driver
Expand Down Expand Up @@ -52,7 +52,7 @@ def get_grammars(target_versions: Set[TargetVersion]) -> List[Grammar]:
if not target_versions:
# No target_version specified, so try all grammars.
return [
# Python 3.7+
# Python 3.7-3.9
pygram.python_grammar_no_print_statement_no_exec_statement_async_keywords,
# Python 3.0-3.6
pygram.python_grammar_no_print_statement_no_exec_statement,
Expand All @@ -72,7 +72,7 @@ def get_grammars(target_versions: Set[TargetVersion]) -> List[Grammar]:
if not supports_feature(target_versions, Feature.ASYNC_KEYWORDS):
# Python 3.0-3.6
grammars.append(pygram.python_grammar_no_print_statement_no_exec_statement)
if supports_feature(target_versions, Feature.PATTERN_MATCHING):
if any(Feature.PATTERN_MATCHING in VERSION_TO_FEATURES[v] for v in target_versions):
# Python 3.10+
grammars.append(pygram.python_grammar_soft_keywords)

Expand Down

0 comments on commit 1b5b6f4

Please sign in to comment.