Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 1155: Handle LHS and RHS values differently #1164

Closed
wants to merge 30 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
59c9ebb
Fix tuple closing leaf lookup
vemel Nov 19, 2019
8189391
Fix behavior for lines with only invisible parentheses
vemel Nov 19, 2019
c924ba3
WIP: new is_collection_with_optional_trailing_comma logic
vemel Nov 19, 2019
0dd5a0d
Fix top-level collection check and function defs
vemel Nov 19, 2019
e834921
Rewrite comments
vemel Nov 19, 2019
5cf899a
Use LeafID instead of int type
vemel Nov 19, 2019
f4eb91d
Add separate handling for LHS and RHS
vemel Nov 20, 2019
caa0f5a
Reverted some changes to be as close as possible to current behavior
vemel Nov 20, 2019
de02a61
Final version
vemel Nov 20, 2019
73c621c
Move `should_be_rendered_as_single_line` to `Line` as a method
vemel Nov 20, 2019
2758bab
Remove line_str argument from is_line_short_enough
vemel Nov 20, 2019
efa31fe
Fix undefined argument usage
vemel Nov 20, 2019
a570e46
Prevent unwanted lines yield from right_hand_split
vemel Nov 20, 2019
53d0d02
Fix flake8 warnings
vemel Nov 20, 2019
3bca90a
Remove accidental debug changes
vemel Nov 20, 2019
cd3ce50
Add last line length handling and fix unsplittable type ignore behavior
vemel Nov 20, 2019
f869216
Flake8
vemel Nov 20, 2019
ffae728
Fix line too long
vemel Nov 20, 2019
7bca179
FIx another line too long
vemel Nov 20, 2019
f35d1f7
Revert accidental change
vemel Nov 20, 2019
65a4a50
Fix mypy warnings
vemel Nov 20, 2019
96a4852
Make _get_assignment_index private
vemel Nov 20, 2019
aff3273
Update docstrings
vemel Nov 20, 2019
7ab3fec
Update docstrings
vemel Nov 20, 2019
46fc89d
Make trailing comma behavior consistent across LHS and RHS
vemel Nov 20, 2019
42923a2
Update driver to pass tests
vemel Nov 20, 2019
7c6d774
Fix typo
vemel Nov 23, 2019
f147295
Address reviews comments
vemel Nov 23, 2019
c11855e
Revert VERSION_TO_FEATURES formatting
vemel Nov 23, 2019
7e516d9
Reverted driver formatting
vemel Nov 23, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
681 changes: 515 additions & 166 deletions black.py

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion blib2to3/pgen2/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def parse_stream(self, stream: IO[Text], debug: bool = False) -> NL:
return self.parse_stream_raw(stream, debug)

def parse_file(
self, filename: Path, encoding: Optional[Text] = None, debug: bool = False,
self, filename: Path, encoding: Optional[Text] = None, debug: bool = False
) -> NL:
"""Parse a file and return the syntax tree."""
with io.open(filename, "r", encoding=encoding) as stream:
Expand Down
40 changes: 35 additions & 5 deletions tests/data/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@
division_result_tuple = (6/2,)
print("foo %r", (foo.bar,))

subscript_one_item[1]
subscript_multiple_items[1, 2, {3: 4}]
subscript_one_item = subscript_one_item[1]
subscript_multiple_items = subscript_multiple_items[1, 2, {3: 4}]
subscript_one_item[1,]
subscript_multiple_items[1, 2, {3: 4},]
subscript_one_item = subscript_one_item[1,]
subscript_multiple_items = subscript_multiple_items[1, 2, {3: 4},]

if True:
IGNORED_TYPES_FOR_ATTRIBUTE_CHECKING = (
Config.IGNORED_TYPES_FOR_ATTRIBUTE_CHECKING
Expand All @@ -60,11 +69,11 @@
ec2client.get_waiter('instance_stopped').wait(
InstanceIds=[instance.id],
WaiterConfig={
'Delay': 5,
'Delay': 5
})
ec2client.get_waiter("instance_stopped").wait(
InstanceIds=[instance.id],
WaiterConfig={"Delay": 5,},
WaiterConfig={"Delay": 5},
)
ec2client.get_waiter("instance_stopped").wait(
InstanceIds=[instance.id], WaiterConfig={"Delay": 5,},
Expand Down Expand Up @@ -143,6 +152,23 @@
division_result_tuple = (6 / 2,)
print("foo %r", (foo.bar,))

subscript_one_item[1]
subscript_multiple_items[1, 2, {3: 4}]
subscript_one_item = subscript_one_item[1]
subscript_multiple_items = subscript_multiple_items[1, 2, {3: 4}]
subscript_one_item[1,]
subscript_multiple_items[
1,
2,
{3: 4},
]
subscript_one_item = subscript_one_item[1,]
subscript_multiple_items = subscript_multiple_items[
1,
2,
{3: 4},
]

if True:
IGNORED_TYPES_FOR_ATTRIBUTE_CHECKING = (
Config.IGNORED_TYPES_FOR_ATTRIBUTE_CHECKING
Expand All @@ -151,11 +177,15 @@

if True:
ec2client.get_waiter("instance_stopped").wait(
InstanceIds=[instance.id], WaiterConfig={"Delay": 5,}
InstanceIds=[instance.id], WaiterConfig={"Delay": 5}
)
ec2client.get_waiter("instance_stopped").wait(
InstanceIds=[instance.id], WaiterConfig={"Delay": 5,},
InstanceIds=[instance.id],
WaiterConfig={"Delay": 5},
)
ec2client.get_waiter("instance_stopped").wait(
InstanceIds=[instance.id], WaiterConfig={"Delay": 5,},
InstanceIds=[instance.id],
WaiterConfig={
"Delay": 5,
},
)
7 changes: 6 additions & 1 deletion tests/data/comments7.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,12 @@ def func():

def func():
c = call(
0.0123, 0.0456, 0.0789, 0.0123, 0.0789, a[-1], # type: ignore
0.0123,
0.0456,
0.0789,
0.0123,
0.0789,
a[-1], # type: ignore
)

# The type: ignore exception only applies to line length, not
Expand Down
25 changes: 6 additions & 19 deletions tests/data/expression.diff
Original file line number Diff line number Diff line change
Expand Up @@ -130,37 +130,25 @@
call(**self.screen_kwargs)
call(b, **self.screen_kwargs)
lukasz.langa.pl
@@ -94,23 +127,25 @@
@@ -94,11 +127,16 @@
1.0 .real
....__class__
list[str]
dict[str, int]
tuple[str, ...]
-tuple[str, int, float, dict[str, int],]
+tuple[
+ str, int, float, dict[str, int],
+ str,
+ int,
+ float,
+ dict[str, int],
+]
very_long_variable_name_filters: t.List[
t.Tuple[str, t.Union[str, t.List[t.Optional[str]]]],
]
xxxx_xxxxx_xxxx_xxx: Callable[..., List[SomeClass]] = classmethod( # type: ignore
sync(async_xxxx_xxx_xxxx_xxxxx_xxxx_xxx.__func__)
)
xxxx_xxx_xxxx_xxxxx_xxxx_xxx: Callable[..., List[SomeClass]] = classmethod( # type: ignore
sync(async_xxxx_xxx_xxxx_xxxxx_xxxx_xxx.__func__)
)
-xxxx_xxx_xxxx_xxxxx_xxxx_xxx: Callable[
- ..., List[SomeClass]
-] = classmethod(sync(async_xxxx_xxx_xxxx_xxxxx_xxxx_xxx.__func__)) # type: ignore
+xxxx_xxx_xxxx_xxxxx_xxxx_xxx: Callable[..., List[SomeClass]] = classmethod(
+ sync(async_xxxx_xxx_xxxx_xxxxx_xxxx_xxx.__func__)
+) # type: ignore
slice[0]
slice[0:1]
slice[0:1:2]
slice[:]
slice[:-1]
@@ -134,113 +169,171 @@
@@ -132,113 +170,171 @@
numpy[-(c + 1) :, d]
numpy[:, l[-2]]
numpy[:, ::-1]
Expand Down Expand Up @@ -404,4 +392,3 @@
return True
last_call()
# standalone comment at ENDMARKER

13 changes: 6 additions & 7 deletions tests/data/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,7 @@
xxxx_xxx_xxxx_xxxxx_xxxx_xxx: Callable[..., List[SomeClass]] = classmethod( # type: ignore
sync(async_xxxx_xxx_xxxx_xxxxx_xxxx_xxx.__func__)
)
xxxx_xxx_xxxx_xxxxx_xxxx_xxx: Callable[
..., List[SomeClass]
] = classmethod(sync(async_xxxx_xxx_xxxx_xxxxx_xxxx_xxx.__func__)) # type: ignore
xxxx_xxx_xxxx_xxxxx_xxxx_xxx: Callable[..., List[SomeClass]] = classmethod(sync(async_xxxx_xxx_xxxx_xxxxx_xxxx_xxx.__func__)) # type: ignore
slice[0]
slice[0:1]
slice[0:1:2]
Expand Down Expand Up @@ -380,7 +378,10 @@ async def f():
dict[str, int]
tuple[str, ...]
tuple[
str, int, float, dict[str, int],
str,
int,
float,
dict[str, int],
]
very_long_variable_name_filters: t.List[
t.Tuple[str, t.Union[str, t.List[t.Optional[str]]]],
Expand All @@ -391,9 +392,7 @@ async def f():
xxxx_xxx_xxxx_xxxxx_xxxx_xxx: Callable[..., List[SomeClass]] = classmethod( # type: ignore
sync(async_xxxx_xxx_xxxx_xxxxx_xxxx_xxx.__func__)
)
xxxx_xxx_xxxx_xxxxx_xxxx_xxx: Callable[..., List[SomeClass]] = classmethod(
sync(async_xxxx_xxx_xxxx_xxxxx_xxxx_xxx.__func__)
) # type: ignore
xxxx_xxx_xxxx_xxxxx_xxxx_xxx: Callable[..., List[SomeClass]] = classmethod(sync(async_xxxx_xxx_xxxx_xxxxx_xxxx_xxx.__func__)) # type: ignore
slice[0]
slice[0:1]
slice[0:1:2]
Expand Down
8 changes: 5 additions & 3 deletions tests/data/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ def trailing_comma():
D: 0.1 * (10.0 / 12),
}
def f(
a,
**kwargs,
a, **kwargs,
) -> A:
return (
yield from A(
Expand Down Expand Up @@ -230,7 +229,10 @@ def trailing_comma():
}


def f(a, **kwargs,) -> A:
def f(
a,
**kwargs,
) -> A:
return (
yield from A(
very_long_argument_name1=very_long_value_for_the_argument,
Expand Down
5 changes: 4 additions & 1 deletion tests/data/function2.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ def inner():

# output

def f(a, **kwargs,) -> A:
def f(
a,
**kwargs,
) -> A:
with cache_dir():
if something:
result = CliRunner().invoke(
Expand Down
8 changes: 6 additions & 2 deletions tests/data/function_trailing_comma.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> Set[

# output

def f(a,):
def f(
a,
):
...


def f(a: int = 1,):
def f(
a: int = 1,
):
...


Expand Down
27 changes: 27 additions & 0 deletions tests/data/remove_parens.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ def example8():
return (((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((None)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))


def example_tuple():
return ((((1, 2, 3))))


def example_tuple_trailing_comma():
return (((1, 2, 3,)))


def example_list():
return ((([1, 2, 3])))


# output
x = 1
x = 1.2
Expand Down Expand Up @@ -142,3 +154,18 @@ def example7():
def example8():
return None


def example_tuple():
return (1, 2, 3)


def example_tuple_trailing_comma():
return (
1,
2,
3,
)


def example_list():
return [1, 2, 3]
2 changes: 1 addition & 1 deletion tests/data/tupleassign.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
sdfjsdfjlksdljkfsdlkf,
sdfsdjfklsdfjlksdljkf,
sdsfsdfjskdflsfsdf,
) = (1, 2, 3)
) = 1, 2, 3

# This is as well.
(this_will_be_wrapped_in_parens,) = struct.unpack(b"12345678901234567890")
Expand Down