from blib2to3.pgen2.grammar import Grammar
from blib2to3.pgen2.parse import ParseError
-from _version import version as __version__
+from _black_version import version as __version__
DEFAULT_LINE_LENGTH = 88
DEFAULT_EXCLUDES = r"/(\.eggs|\.git|\.hg|\.mypy_cache|\.nox|\.tox|\.venv|\.svn|_build|buck-out|build|dist)/" # noqa: B950
# 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:
self.current_line.append(node)
yield from super().visit_default(node)
- def visit_factor(self, node: Node) -> Iterator[Line]:
- """Force parentheses between a unary op and a binary power:
-
- -2 ** 8 -> -(2 ** 8)
- """
- child = node.children[1]
- if child.type == syms.power and len(child.children) == 3:
- lpar = Leaf(token.LPAR, "(")
- rpar = Leaf(token.RPAR, ")")
- index = child.remove() or 0
- node.insert_child(index, Node(syms.atom, [lpar, child, rpar]))
- yield from self.visit_default(node)
-
def visit_INDENT(self, node: Node) -> Iterator[Line]:
"""Increase indentation level, maybe yield a line."""
# In blib2to3 INDENT never holds comments.
yield from self.line()
yield from self.visit_default(leaf)
+ def visit_factor(self, node: Node) -> Iterator[Line]:
+ """Force parentheses between a unary op and a binary power:
+
+ -2 ** 8 -> -(2 ** 8)
+ """
+ _operator, operand = node.children
+ if (
+ operand.type == syms.power
+ and len(operand.children) == 3
+ and operand.children[1].type == token.DOUBLESTAR
+ ):
+ lpar = Leaf(token.LPAR, "(")
+ rpar = Leaf(token.RPAR, ")")
+ index = operand.remove() or 0
+ node.insert_child(index, Node(syms.atom, [lpar, operand, rpar]))
+ yield from self.visit_default(node)
+
def __attrs_post_init__(self) -> None:
"""You are in a twisty little maze of passages."""
v = self.visit_stmt
# 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.
- yield from right_hand_split(line, line_length, features=features)
+ # 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:
split_funcs = [delimiter_split, standalone_comment_split, rhs]
# Since body is a new indent level, remove spurious leading whitespace.
normalize_prefix(leaves[0], inside_brackets=True)
# Ensure a trailing comma for imports and standalone function arguments, but
- # be careful not to add one after any comments.
- no_commas = original.is_def and not any(
- l.type == token.COMMA for l in leaves
+ # be careful not to add one after any comments or within type annotations.
+ no_commas = (
+ original.is_def
+ and opening_bracket.value == "("
+ and not any(l.type == token.COMMA for l in leaves)
)
if original.is_import or no_commas:
# Then ignore with `exclude` option.
try:
normalized_path = "/" + child.resolve().relative_to(root).as_posix()
+ except OSError as e:
+ report.path_ignored(child, f"cannot be read because {e}")
+ continue
except ValueError:
if child.is_symlink():
report.path_ignored(
@contextmanager
def nullcontext() -> Iterator[None]:
- """Return context manager that does nothing.
- Similar to `nullcontext` from python 3.7"""
+ """Return an empty context manager.
+
+ To be used like `nullcontext` in Python 3.7.
+ """
yield