more relatively noncontroversial features

This commit is contained in:
Jelle Zijlstra 2023-11-20 21:44:26 -08:00
parent 51bb901a8a
commit c71b5e23bd
4 changed files with 12 additions and 48 deletions

View File

@ -282,9 +282,7 @@ def visit_match_case(self, node: Node) -> Iterator[Line]:
def visit_suite(self, node: Node) -> Iterator[Line]: def visit_suite(self, node: Node) -> Iterator[Line]:
"""Visit a suite.""" """Visit a suite."""
if ( if is_stub_suite(node):
self.mode.is_pyi or Preview.dummy_implementations in self.mode
) and is_stub_suite(node):
yield from self.visit(node.children[2]) yield from self.visit(node.children[2])
else: else:
yield from self.visit_default(node) yield from self.visit_default(node)
@ -299,9 +297,7 @@ def visit_simple_stmt(self, node: Node) -> Iterator[Line]:
is_suite_like = node.parent and node.parent.type in STATEMENT is_suite_like = node.parent and node.parent.type in STATEMENT
if is_suite_like: if is_suite_like:
if ( if is_stub_body(node):
self.mode.is_pyi or Preview.dummy_implementations in self.mode
) and is_stub_body(node):
yield from self.visit_default(node) yield from self.visit_default(node)
else: else:
yield from self.line(+1) yield from self.line(+1)
@ -309,11 +305,7 @@ def visit_simple_stmt(self, node: Node) -> Iterator[Line]:
yield from self.line(-1) yield from self.line(-1)
else: else:
if ( if not node.parent or not is_stub_suite(node.parent):
not (self.mode.is_pyi or Preview.dummy_implementations in self.mode)
or not node.parent
or not is_stub_suite(node.parent)
):
yield from self.line() yield from self.line()
yield from self.visit_default(node) yield from self.visit_default(node)
@ -405,7 +397,6 @@ def foo(a: int, b: float = 7): ...
def foo(a: (int), b: (float) = 7): ... def foo(a: (int), b: (float) = 7): ...
""" """
if Preview.parenthesize_long_type_hints in self.mode:
assert len(node.children) == 3 assert len(node.children) == 3
if maybe_make_parens_invisible_in_atom(node.children[2], parent=node): if maybe_make_parens_invisible_in_atom(node.children[2], parent=node):
wrap_in_parentheses(node, node.children[2], visible=False) wrap_in_parentheses(node, node.children[2], visible=False)
@ -514,13 +505,7 @@ def __post_init__(self) -> None:
self.visit_with_stmt = partial(v, keywords={"with"}, parens={"with"}) self.visit_with_stmt = partial(v, keywords={"with"}, parens={"with"})
self.visit_classdef = partial(v, keywords={"class"}, parens=Ø) self.visit_classdef = partial(v, keywords={"class"}, parens=Ø)
# When this is moved out of preview, add ":" directly to ASSIGNMENTS in nodes.py self.visit_expr_stmt = partial(v, keywords=Ø, parens=ASSIGNMENTS)
if Preview.parenthesize_long_type_hints in self.mode:
assignments = ASSIGNMENTS | {":"}
else:
assignments = ASSIGNMENTS
self.visit_expr_stmt = partial(v, keywords=Ø, parens=assignments)
self.visit_return_stmt = partial(v, keywords={"return"}, parens={"return"}) self.visit_return_stmt = partial(v, keywords={"return"}, parens={"return"})
self.visit_import_from = partial(v, keywords=Ø, parens={"import"}) self.visit_import_from = partial(v, keywords=Ø, parens={"import"})
self.visit_del_stmt = partial(v, keywords=Ø, parens={"del"}) self.visit_del_stmt = partial(v, keywords=Ø, parens={"del"})
@ -900,9 +885,8 @@ def _maybe_split_omitting_optional_parens(
# The RHSResult Omitting Optional Parens. # The RHSResult Omitting Optional Parens.
rhs_oop = _first_right_hand_split(line, omit=omit) rhs_oop = _first_right_hand_split(line, omit=omit)
if not ( if not (
Preview.prefer_splitting_right_hand_side_of_assignments in line.mode
# the split is right after `=` # the split is right after `=`
and len(rhs.head.leaves) >= 2 len(rhs.head.leaves) >= 2
and rhs.head.leaves[-2].type == token.EQUAL and rhs.head.leaves[-2].type == token.EQUAL
# the left side of assignment contains brackets # the left side of assignment contains brackets
and any(leaf.type in BRACKETS for leaf in rhs.head.leaves[:-1]) and any(leaf.type in BRACKETS for leaf in rhs.head.leaves[:-1])

View File

@ -550,8 +550,7 @@ def maybe_empty_lines(self, current_line: Line) -> LinesBlock:
lines (two on module-level). lines (two on module-level).
""" """
form_feed = ( form_feed = (
Preview.allow_form_feeds in self.mode current_line.depth == 0
and current_line.depth == 0
and bool(current_line.leaves) and bool(current_line.leaves)
and "\f\n" in current_line.leaves[0].prefix and "\f\n" in current_line.leaves[0].prefix
) )
@ -565,8 +564,7 @@ def maybe_empty_lines(self, current_line: Line) -> LinesBlock:
else before - previous_after else before - previous_after
) )
if ( if (
Preview.module_docstring_newlines in current_line.mode self.previous_block
and self.previous_block
and self.previous_block.previous_block is None and self.previous_block.previous_block is None
and len(self.previous_block.original_line.leaves) == 1 and len(self.previous_block.original_line.leaves) == 1
and self.previous_block.original_line.is_triple_quoted_string and self.previous_block.original_line.is_triple_quoted_string
@ -770,11 +768,7 @@ def _maybe_empty_lines_for_class_or_def( # noqa: C901
newlines = 1 if current_line.depth else 2 newlines = 1 if current_line.depth else 2
# If a user has left no space after a dummy implementation, don't insert # If a user has left no space after a dummy implementation, don't insert
# new lines. This is useful for instance for @overload or Protocols. # new lines. This is useful for instance for @overload or Protocols.
if ( if self.previous_line.is_stub_def and not user_had_newline:
Preview.dummy_implementations in self.mode
and self.previous_line.is_stub_def
and not user_had_newline
):
newlines = 0 newlines = 0
if comment_to_add_newlines is not None: if comment_to_add_newlines is not None:
previous_block = comment_to_add_newlines.previous_block previous_block = comment_to_add_newlines.previous_block
@ -831,13 +825,6 @@ def is_line_short_enough( # noqa: C901
width = str_width if mode.preview else len width = str_width if mode.preview else len
if Preview.multiline_string_handling not in mode:
return (
width(line_str) <= mode.line_length
and "\n" not in line_str # multiline strings
and not line.contains_standalone_comments()
)
if line.contains_standalone_comments(): if line.contains_standalone_comments():
return False return False
if "\n" not in line_str: if "\n" not in line_str:

View File

@ -168,21 +168,13 @@ def supports_feature(target_versions: Set[TargetVersion], feature: Feature) -> b
class Preview(Enum): class Preview(Enum):
"""Individual preview style features.""" """Individual preview style features."""
blank_line_after_nested_stub_class = auto()
hex_codes_in_unicode_sequences = auto() hex_codes_in_unicode_sequences = auto()
multiline_string_handling = auto()
prefer_splitting_right_hand_side_of_assignments = auto()
# NOTE: string_processing requires wrap_long_dict_values_in_parens # NOTE: string_processing requires wrap_long_dict_values_in_parens
# for https://github.com/psf/black/issues/3117 to be fixed. # for https://github.com/psf/black/issues/3117 to be fixed.
string_processing = auto() string_processing = auto()
parenthesize_conditional_expressions = auto() parenthesize_conditional_expressions = auto()
parenthesize_long_type_hints = auto()
wrap_long_dict_values_in_parens = auto() wrap_long_dict_values_in_parens = auto()
wrap_multiple_context_managers_in_parens = auto()
dummy_implementations = auto()
module_docstring_newlines = auto()
hug_parens_with_braces_and_square_brackets = auto() hug_parens_with_braces_and_square_brackets = auto()
allow_form_feeds = auto()
class Deprecated(UserWarning): class Deprecated(UserWarning):

View File

@ -121,6 +121,7 @@
">>=", ">>=",
"**=", "**=",
"//=", "//=",
":",
} }
IMPLICIT_TUPLE: Final = {syms.testlist, syms.testlist_star_expr, syms.exprlist} IMPLICIT_TUPLE: Final = {syms.testlist, syms.testlist_star_expr, syms.exprlist}