Skip to content
This repository has been archived by the owner on Jan 22, 2024. It is now read-only.

Commit

Permalink
Fix psf#1202: remove trailing comma from function arguments list
Browse files Browse the repository at this point in the history
  • Loading branch information
lg-kialo committed Jan 23, 2020
1 parent be49ac7 commit 5fa824b
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 14 deletions.
38 changes: 34 additions & 4 deletions black.py
Expand Up @@ -1460,9 +1460,9 @@ def maybe_remove_trailing_comma(self, closing: Leaf) -> bool:
if not (self.leaves and self.leaves[-1].type == token.COMMA):
return False

# We remove trailing commas only in the case of importing a
# Remove trailing commas in the case of importing a
# single name from a module.
if not (
if (
self.leaves
and self.is_import
and len(self.leaves) > 4
Expand All @@ -1487,10 +1487,40 @@ def maybe_remove_trailing_comma(self, closing: Leaf) -> bool:
)
and closing.type == token.RPAR
):
self.remove_trailing_comma()
return True

# Otherwise, if the trailing one is the only one, we might mistakenly
# change a tuple into a different type by removing the comma.
if hasattr(closing, "bracket_depth"):
depth = closing.bracket_depth + 1
else:
return False
if hasattr(closing, "opening_bracket"):
opening = closing.opening_bracket
else:
return False

self.remove_trailing_comma()
return True
try:
_opening_index = self.leaves.index(opening)
except ValueError:
return False

for leaf in self.leaves[_opening_index + 1 :]:
# stop if we're at the closing bracket
if leaf is closing:
break

if (
leaf.bracket_depth == depth
and leaf.type == token.COMMA
and leaf.parent
and leaf.parent.type in {syms.arglist, syms.typedargslist}
):
self.remove_trailing_comma()
return True

return False

def append_comment(self, comment: Leaf) -> bool:
"""Add an inline or standalone comment to the line."""
Expand Down
2 changes: 1 addition & 1 deletion blib2to3/pgen2/driver.py
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
4 changes: 2 additions & 2 deletions tests/data/collections.py
Expand Up @@ -154,8 +154,8 @@
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,}
)
2 changes: 1 addition & 1 deletion tests/data/comments7.py
Expand Up @@ -93,7 +93,7 @@ def func():


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

# The type: ignore exception only applies to line length, not
# other types of formatting.
Expand Down
2 changes: 1 addition & 1 deletion tests/data/expression.diff
Expand Up @@ -212,7 +212,7 @@
+ .filter(
+ models.Customer.account_id == account_id, models.Customer.email == email_address
+ )
+ .order_by(models.Customer.id.asc(),)
+ .order_by(models.Customer.id.asc())
+ .all()
+)
Ø = set()
Expand Down
2 changes: 1 addition & 1 deletion tests/data/expression.py
Expand Up @@ -459,7 +459,7 @@ async def f():
.filter(
models.Customer.account_id == account_id, models.Customer.email == email_address
)
.order_by(models.Customer.id.asc(),)
.order_by(models.Customer.id.asc())
.all()
)
Ø = set()
Expand Down
2 changes: 1 addition & 1 deletion tests/data/function.py
Expand Up @@ -230,7 +230,7 @@ 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
2 changes: 1 addition & 1 deletion tests/data/function2.py
Expand Up @@ -25,7 +25,7 @@ def inner():

# output

def f(a, **kwargs,) -> A:
def f(a, **kwargs) -> A:
with cache_dir():
if something:
result = CliRunner().invoke(
Expand Down
17 changes: 15 additions & 2 deletions tests/data/function_trailing_comma.py
Expand Up @@ -9,17 +9,30 @@ def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> Set[
]:
pass

def _gopass_process() -> Popen:
"""Spawn a Gopass process"""
return Popen(
['gopass', 'jsonapi', 'listen'],
stdout=PIPE,
stdin=PIPE,
)

# output

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


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


def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> Set[
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
]:
pass


def _gopass_process() -> Popen:
"""Spawn a Gopass process"""
return Popen(["gopass", "jsonapi", "listen"], stdout=PIPE, stdin=PIPE)

0 comments on commit 5fa824b

Please sign in to comment.