# Python 2.7
pygram.python_grammar,
]
- elif all(version.is_python2() for version in target_versions):
+
+ if all(version.is_python2() for version in target_versions):
# Python 2-only code, so try Python 2 grammars.
return [
# Python 2.7 with future print_function import
# Python 2.7
pygram.python_grammar,
]
- else:
- # Python 3-compatible code, so only try Python 3 grammar.
- grammars = []
- # If we have to parse both, try to parse async as a keyword first
- if not supports_feature(target_versions, Feature.ASYNC_IDENTIFIERS):
- # Python 3.7+
- grammars.append(
- pygram.python_grammar_no_print_statement_no_exec_statement_async_keywords # noqa: B950
- )
- 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)
- # At least one of the above branches must have been taken, because every Python
- # version has exactly one of the two 'ASYNC_*' flags
- return grammars
+
+ # Python 3-compatible code, so only try Python 3 grammar.
+ grammars = []
+ # If we have to parse both, try to parse async as a keyword first
+ if not supports_feature(target_versions, Feature.ASYNC_IDENTIFIERS):
+ # Python 3.7+
+ grammars.append(
+ pygram.python_grammar_no_print_statement_no_exec_statement_async_keywords
+ )
+ 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)
+ # At least one of the above branches must have been taken, because every Python
+ # version has exactly one of the two 'ASYNC_*' flags
+ return grammars
def lib2to3_parse(src_txt: str, target_versions: Iterable[TargetVersion] = ()) -> Node:
# All splits failed, best effort split with no omits.
# This mostly happens to multiline strings that are by definition
# reported as not fitting a single line.
- # line_length=1 is silly, but somehow produces better formatting
- # than other things we've tried so far. See #762 and #781.
+ # line_length=1 here was historically a bug that somehow became a feature.
+ # See #762 and #781 for the full story.
yield from right_hand_split(line, line_length=1, features=features)
if line.inside_brackets: