Many uncontroverisal preview changes

This commit is contained in:
Jelle Zijlstra 2023-11-20 21:24:26 -08:00
parent 129349ce66
commit 51bb901a8a
6 changed files with 30 additions and 90 deletions

View File

@ -376,8 +376,7 @@ def _contains_fmt_skip_comment(comment_line: str, mode: Mode) -> bool:
# noqa:XXX # fmt:skip # a nice line <-- multiple comments (Preview) # noqa:XXX # fmt:skip # a nice line <-- multiple comments (Preview)
# pylint:XXX; fmt:skip <-- list of comments (; separated, Preview) # pylint:XXX; fmt:skip <-- list of comments (; separated, Preview)
""" """
semantic_comment_blocks = ( semantic_comment_blocks = [
[
comment_line, comment_line,
*[ *[
_COMMENT_PREFIX + comment.strip() _COMMENT_PREFIX + comment.strip()
@ -390,8 +389,5 @@ def _contains_fmt_skip_comment(comment_line: str, mode: Mode) -> bool:
) )
], ],
] ]
if Preview.single_line_format_skip_with_multiple_comments in mode
else [comment_line]
)
return any(comment in FMT_SKIP for comment in semantic_comment_blocks) return any(comment in FMT_SKIP for comment in semantic_comment_blocks)

View File

@ -114,10 +114,8 @@ def line(self, indent: int = 0) -> Iterator[Line]:
self.current_line.depth += indent self.current_line.depth += indent
return # Line is empty, don't emit. Creating a new one unnecessary. return # Line is empty, don't emit. Creating a new one unnecessary.
if ( if len(self.current_line.leaves) == 1 and is_async_stmt_or_funcdef(
Preview.improved_async_statements_handling in self.mode self.current_line.leaves[0]
and len(self.current_line.leaves) == 1
and is_async_stmt_or_funcdef(self.current_line.leaves[0])
): ):
# Special case for async def/for/with statements. `visit_async_stmt` # Special case for async def/for/with statements. `visit_async_stmt`
# adds an `ASYNC` leaf then visits the child def/for/with statement # adds an `ASYNC` leaf then visits the child def/for/with statement
@ -333,11 +331,7 @@ def visit_async_stmt(self, node: Node) -> Iterator[Line]:
break break
internal_stmt = next(children) internal_stmt = next(children)
if Preview.improved_async_statements_handling in self.mode:
yield from self.visit(internal_stmt) yield from self.visit(internal_stmt)
else:
for child in internal_stmt.children:
yield from self.visit(child)
def visit_decorators(self, node: Node) -> Iterator[Line]: def visit_decorators(self, node: Node) -> Iterator[Line]:
"""Visit decorators.""" """Visit decorators."""
@ -567,9 +561,7 @@ def transform_line(
# We need the line string when power operators are hugging to determine if we should # We need the line string when power operators are hugging to determine if we should
# split the line. Default to line_str, if no power operator are present on the line. # split the line. Default to line_str, if no power operator are present on the line.
line_str_hugging_power_ops = ( line_str_hugging_power_ops = (
(_hugging_power_ops_line_to_string(line, features, mode) or line_str) _hugging_power_ops_line_to_string(line, features, mode) or line_str
if Preview.fix_power_op_line_length in mode
else line_str
) )
ll = mode.line_length ll = mode.line_length
@ -679,9 +671,6 @@ def should_split_funcdef_with_rhs(line: Line, mode: Mode) -> bool:
"""If a funcdef has a magic trailing comma in the return type, then we should first """If a funcdef has a magic trailing comma in the return type, then we should first
split the line with rhs to respect the comma. split the line with rhs to respect the comma.
""" """
if Preview.respect_magic_trailing_comma_in_return_type not in mode:
return False
return_type_leaves: List[Leaf] = [] return_type_leaves: List[Leaf] = []
in_return_type = False in_return_type = False
@ -1191,11 +1180,7 @@ def append_to_line(leaf: Leaf) -> Iterator[Line]:
trailing_comma_safe and Feature.TRAILING_COMMA_IN_CALL in features trailing_comma_safe and Feature.TRAILING_COMMA_IN_CALL in features
) )
if ( if last_leaf.type == STANDALONE_COMMENT and leaf_idx == last_non_comment_leaf:
Preview.add_trailing_comma_consistently in mode
and last_leaf.type == STANDALONE_COMMENT
and leaf_idx == last_non_comment_leaf
):
current_line = _safe_add_trailing_comma( current_line = _safe_add_trailing_comma(
trailing_comma_safe, delimiter_priority, current_line trailing_comma_safe, delimiter_priority, current_line
) )
@ -1282,11 +1267,7 @@ def normalize_invisible_parens( # noqa: C901
# Fixes a bug where invisible parens are not properly wrapped around # Fixes a bug where invisible parens are not properly wrapped around
# case blocks. # case blocks.
if ( if isinstance(child, Node) and child.type == syms.case_block:
isinstance(child, Node)
and child.type == syms.case_block
and Preview.long_case_block_line_splitting in mode
):
normalize_invisible_parens( normalize_invisible_parens(
child, parens_after={"case"}, mode=mode, features=features child, parens_after={"case"}, mode=mode, features=features
) )
@ -1341,7 +1322,6 @@ def normalize_invisible_parens( # noqa: C901
and child.next_sibling is not None and child.next_sibling is not None
and child.next_sibling.type == token.COLON and child.next_sibling.type == token.COLON
and child.value == "case" and child.value == "case"
and Preview.long_case_block_line_splitting in mode
): ):
# A special patch for "case case:" scenario, the second occurrence # A special patch for "case case:" scenario, the second occurrence
# of case will be not parsed as a Python keyword. # of case will be not parsed as a Python keyword.
@ -1415,7 +1395,6 @@ def _maybe_wrap_cms_in_parens(
""" """
if ( if (
Feature.PARENTHESIZED_CONTEXT_MANAGERS not in features Feature.PARENTHESIZED_CONTEXT_MANAGERS not in features
or Preview.wrap_multiple_context_managers_in_parens not in mode
or len(node.children) <= 2 or len(node.children) <= 2
# If it's an atom, it's already wrapped in parens. # If it's an atom, it's already wrapped in parens.
or node.children[1].type == syms.atom or node.children[1].type == syms.atom

View File

@ -203,9 +203,7 @@ def is_triple_quoted_string(self) -> bool:
value = self.leaves[0].value value = self.leaves[0].value
if value.startswith(('"""', "'''")): if value.startswith(('"""', "'''")):
return True return True
if Preview.accept_raw_docstrings in self.mode and value.startswith( if value.startswith(("r'''", 'r"""', "R'''", 'R"""')):
("r'''", 'r"""', "R'''", 'R"""')
):
return True return True
return False return False
@ -628,11 +626,7 @@ def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
if depth and not current_line.is_def and self.previous_line.is_def: if depth and not current_line.is_def and self.previous_line.is_def:
# Empty lines between attributes and methods should be preserved. # Empty lines between attributes and methods should be preserved.
before = 1 if user_had_newline else 0 before = 1 if user_had_newline else 0
elif ( elif previous_def.is_class and not previous_def.is_stub_class:
Preview.blank_line_after_nested_stub_class in self.mode
and previous_def.is_class
and not previous_def.is_stub_class
):
before = 1 before = 1
elif depth: elif depth:
before = 0 before = 0
@ -680,14 +674,13 @@ def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
and self.previous_line.is_class and self.previous_line.is_class
and current_line.is_triple_quoted_string and current_line.is_triple_quoted_string
): ):
if Preview.no_blank_line_before_class_docstring in current_line.mode:
return 0, 1 return 0, 1
return before, 1
# In preview mode, always allow blank lines, except right before a function docstring if (
is_empty_first_line_ok = ( self.previous_line
Preview.allow_empty_first_line_in_block in current_line.mode and self.previous_line.opens_block
and ( # Always allow blank lines, except right before a function docstring
and not (
not is_docstring(current_line.leaves[0]) not is_docstring(current_line.leaves[0])
or ( or (
self.previous_line self.previous_line
@ -696,12 +689,6 @@ def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
and not is_funcdef(self.previous_line.leaves[0].parent) and not is_funcdef(self.previous_line.leaves[0].parent)
) )
) )
)
if (
self.previous_line
and self.previous_line.opens_block
and not is_empty_first_line_ok
): ):
return 0, 0 return 0, 0
return before, 0 return before, 0
@ -762,10 +749,7 @@ def _maybe_empty_lines_for_class_or_def( # noqa: C901
# Don't inspect the previous line if it's part of the body of the previous # Don't inspect the previous line if it's part of the body of the previous
# statement in the same level, we always want a blank line if there's # statement in the same level, we always want a blank line if there's
# something with a body preceding. # something with a body preceding.
elif ( elif self.previous_line.depth > current_line.depth:
Preview.blank_line_between_nested_and_def_stub_file in current_line.mode
and self.previous_line.depth > current_line.depth
):
newlines = 1 newlines = 1
elif ( elif (
current_line.is_def or current_line.is_decorator current_line.is_def or current_line.is_decorator
@ -1001,11 +985,7 @@ def can_omit_invisible_parens(
return False return False
if delimiter_count == 1: if delimiter_count == 1:
if ( if max_priority == COMMA_PRIORITY and rhs.head.is_with_or_async_with_stmt:
Preview.wrap_multiple_context_managers_in_parens in line.mode
and max_priority == COMMA_PRIORITY
and rhs.head.is_with_or_async_with_stmt
):
# For two context manager with statements, the optional parentheses read # For two context manager with statements, the optional parentheses read
# better. In this case, `rhs.body` is the context managers part of # better. In this case, `rhs.body` is the context managers part of
# the with statement. `rhs.head` is the `with (` part on the previous # the with statement. `rhs.head` is the `with (` part on the previous

View File

@ -168,32 +168,20 @@ def supports_feature(target_versions: Set[TargetVersion], feature: Feature) -> b
class Preview(Enum): class Preview(Enum):
"""Individual preview style features.""" """Individual preview style features."""
add_trailing_comma_consistently = auto()
blank_line_after_nested_stub_class = auto() blank_line_after_nested_stub_class = auto()
blank_line_between_nested_and_def_stub_file = auto()
hex_codes_in_unicode_sequences = auto() hex_codes_in_unicode_sequences = auto()
improved_async_statements_handling = auto()
multiline_string_handling = auto() multiline_string_handling = auto()
no_blank_line_before_class_docstring = auto()
prefer_splitting_right_hand_side_of_assignments = 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() parenthesize_long_type_hints = auto()
respect_magic_trailing_comma_in_return_type = auto()
skip_magic_trailing_comma_in_subscript = auto()
wrap_long_dict_values_in_parens = auto() wrap_long_dict_values_in_parens = auto()
wrap_multiple_context_managers_in_parens = auto() wrap_multiple_context_managers_in_parens = auto()
dummy_implementations = auto() dummy_implementations = auto()
walrus_subscript = auto()
module_docstring_newlines = auto() module_docstring_newlines = auto()
accept_raw_docstrings = auto()
fix_power_op_line_length = auto()
hug_parens_with_braces_and_square_brackets = auto() hug_parens_with_braces_and_square_brackets = auto()
allow_empty_first_line_in_block = auto()
single_line_format_skip_with_multiple_comments = auto()
long_case_block_line_splitting = auto()
allow_form_feeds = auto() allow_form_feeds = auto()

View File

@ -346,9 +346,7 @@ def whitespace(leaf: Leaf, *, complex_subscript: bool, mode: Mode) -> str: # no
return NO return NO
elif Preview.walrus_subscript in mode and ( elif t == token.COLONEQUAL or prev.type == token.COLONEQUAL:
t == token.COLONEQUAL or prev.type == token.COLONEQUAL
):
return SPACE return SPACE
elif not complex_subscript: elif not complex_subscript:

View File

@ -1,4 +1,3 @@
# flags: --preview
def line_before_docstring(): def line_before_docstring():
"""Please move me up""" """Please move me up"""