split long del statements into multiple lines (#698)

Fixes #693
This commit is contained in:
Jelle Zijlstra 2019-02-22 22:00:40 -08:00 committed by GitHub
parent 2ae5ce1e6e
commit f5b14b1afd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 3 deletions

View File

@ -944,10 +944,12 @@ More details can be found in [CONTRIBUTING](CONTRIBUTING.md).
### 19.2b0 ### 19.2b0
* *Black* no longer normalizes numeric literals to include `_` separators. * long `del` statements are now split into multiple lines (#698)
* *Black* no longer normalizes numeric literals to include `_` separators (#696)
* new option `--target-version` to control which Python versions * new option `--target-version` to control which Python versions
*Black*-formatted code should target *Black*-formatted code should target (#618)
### 18.9b0 ### 18.9b0

View File

@ -1646,6 +1646,7 @@ def __attrs_post_init__(self) -> None:
self.visit_expr_stmt = partial(v, keywords=Ø, parens=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_async_funcdef = self.visit_async_stmt self.visit_async_funcdef = self.visit_async_stmt
self.visit_decorated = self.visit_decorators self.visit_decorated = self.visit_decorators
@ -3350,7 +3351,16 @@ def _v(node: ast.AST, depth: int = 0) -> Iterator[str]:
if isinstance(value, list): if isinstance(value, list):
for item in value: for item in value:
if isinstance(item, ast.AST): # Ignore nested tuples within del statements, because we may insert
# parentheses and they change the AST.
if (
field == "targets"
and isinstance(node, ast.Delete)
and isinstance(item, ast.Tuple)
):
for item in item.elts:
yield from _v(item, depth + 2)
elif isinstance(item, ast.AST):
yield from _v(item, depth + 2) yield from _v(item, depth + 2)
elif isinstance(value, ast.AST): elif isinstance(value, ast.AST):

View File

@ -36,6 +36,7 @@
if key in self.connect_kwargs: if key in self.connect_kwargs:
raise ValueError(err.format(key)) raise ValueError(err.format(key))
concatenated_strings = "some strings that are" "concatenated implicitly, so if you put them on separate" "lines it will fit" concatenated_strings = "some strings that are" "concatenated implicitly, so if you put them on separate" "lines it will fit"
del concatenated_strings, string_variable_name, normal_function_name, normal_name, need_more_to_make_the_line_long_enough
# output # output
@ -91,3 +92,10 @@
"concatenated implicitly, so if you put them on separate" "concatenated implicitly, so if you put them on separate"
"lines it will fit" "lines it will fit"
) )
del (
concatenated_strings,
string_variable_name,
normal_function_name,
normal_name,
need_more_to_make_the_line_long_enough,
)