Skip to content

Commit

Permalink
Don't emit FURB120 when deleting default arg would change semantics (#…
Browse files Browse the repository at this point in the history
…289):

Closes #288.

Previously FURB120 would emit an error for any default arg it found, but in
some cases following Refurb's advice would cause an error. The issue is that
non-default positional args that follow default positional args cannot be
removed, so Refurb should ignore these default values that are "trapped" behind
a non-default one.
  • Loading branch information
dosisod authored Sep 12, 2023
1 parent cbcf3bf commit 706b451
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
13 changes: 12 additions & 1 deletion refurb/checks/function/use_implicit_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ def check_func(caller: CallExpr, func: FuncDef, errors: list[Error]) -> None:
if arg[1].kind == ArgKind.ARG_STAR:
caller_args = strip_caller_var_args(i, caller_args) # type: ignore

temp_errors: list[Error] = []

for i, (name, value, kind) in enumerate(caller_args):
if i >= len(args):
break
Expand All @@ -152,7 +154,16 @@ def check_func(caller: CallExpr, func: FuncDef, errors: list[Error]) -> None:
return # pragma: no cover

if default and is_equivalent(value, default):
errors.append(ErrorInfo.from_node(value))
temp_errors.append(ErrorInfo.from_node(value))

elif kind == ArgKind.ARG_POS:
# Since this arg is not a default value and cannot be deleted,
# deleting previous default args would cause this arg to become
# misaligned. If this was a kwarg it wouldn't be an issue because
# the position would not be affected during deletion.
temp_errors = []

errors.extend(temp_errors)


def check_symbol(
Expand Down
3 changes: 3 additions & 0 deletions test/data/err_120.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ def args(*args, x: int = 1):
C2.over(1)
C2.over(2)

f(0, 2)
f(1, b=3)


# these should not

Expand Down
2 changes: 2 additions & 0 deletions test/data/err_120.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ test/data/err_120.py:90:6 [FURB120]: Don't pass an argument if it is the same as
test/data/err_120.py:91:6 [FURB120]: Don't pass an argument if it is the same as the default value
test/data/err_120.py:93:9 [FURB120]: Don't pass an argument if it is the same as the default value
test/data/err_120.py:94:9 [FURB120]: Don't pass an argument if it is the same as the default value
test/data/err_120.py:96:6 [FURB120]: Don't pass an argument if it is the same as the default value
test/data/err_120.py:97:3 [FURB120]: Don't pass an argument if it is the same as the default value

0 comments on commit 706b451

Please sign in to comment.