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 d6e573f
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 21 deletions.
51 changes: 40 additions & 11 deletions black.py
Expand Up @@ -1455,19 +1455,12 @@ def contains_multiline_strings(self) -> bool:

return False

def maybe_remove_trailing_comma(self, closing: Leaf) -> bool:
"""Remove trailing comma if there is one and it's safe."""
if not (self.leaves and self.leaves[-1].type == token.COMMA):
return False

# We remove trailing commas only in the case of importing a
# single name from a module.
if not (
def is_single_name_module_import(self, closing: Leaf) -> bool:
if (
self.leaves
and self.is_import
and len(self.leaves) > 4
and self.leaves[-1].type == token.COMMA
and closing.type in CLOSING_BRACKETS
and self.leaves[-4].type == token.NAME
and (
# regular `from foo import bar,`
Expand All @@ -1487,10 +1480,46 @@ def maybe_remove_trailing_comma(self, closing: Leaf) -> bool:
)
and closing.type == token.RPAR
):
return True

return False

def is_function_argument_list(self, closing: Leaf) -> bool:
depth = closing.bracket_depth + 1
opening = closing.opening_bracket

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

self.remove_trailing_comma()
return True
for leaf in self.leaves[_opening_index + 1 :]:
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}
):
return True

return False

def maybe_remove_trailing_comma(self, closing: Leaf) -> bool:
"""Remove trailing comma if there is one and it's safe."""
if not (self.leaves and self.leaves[-1].type == token.COMMA):
return False

if closing.type in CLOSING_BRACKETS and (
self.is_single_name_module_import(closing)
or self.is_function_argument_list(closing)
):
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 d6e573f

Please sign in to comment.