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

Fix: false negative in FURB111 #313

Merged
merged 2 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 4 additions & 1 deletion refurb/checks/readability/use_func_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
LambdaExpr,
ListExpr,
NameExpr,
RefExpr,
ReturnStmt,
TupleExpr,
)

from refurb.checks.common import stringify
from refurb.error import Error


Expand Down Expand Up @@ -59,13 +61,14 @@ def check(node: LambdaExpr, errors: list[Error]) -> None:
arguments=lambda_args,
body=Block(
body=[
ReturnStmt(expr=CallExpr(callee=NameExpr(name=func_name)) as func),
ReturnStmt(expr=CallExpr(callee=RefExpr() as ref) as func),
]
),
) if (
get_lambda_arg_names(lambda_args) == get_func_arg_names(func.args)
and all(kind == ArgKind.ARG_POS for kind in func.arg_kinds)
):
func_name = stringify(ref)
arg_names = get_lambda_arg_names(lambda_args)
arg_names = ", ".join(arg_names) if arg_names else ""

Expand Down
4 changes: 4 additions & 0 deletions test/data/err_111.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
def f(x, y):
pass

mod = object


lambda: print()
lambda x: bool(x)
Expand All @@ -12,6 +14,8 @@ def f(x, y):
lambda: {}
lambda: ()

lambda x: mod.cast(x)


# these will not

Expand Down
13 changes: 7 additions & 6 deletions test/data/err_111.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
test/data/err_111.py:7:1 [FURB111]: Replace `lambda: print()` with `print`
test/data/err_111.py:8:1 [FURB111]: Replace `lambda x: bool(x)` with `bool`
test/data/err_111.py:9:1 [FURB111]: Replace `lambda x, y: f(x, y)` with `f`
test/data/err_111.py:11:1 [FURB111]: Replace `lambda: []` with `list`
test/data/err_111.py:12:1 [FURB111]: Replace `lambda: {}` with `dict`
test/data/err_111.py:13:1 [FURB111]: Replace `lambda: ()` with `tuple`
test/data/err_111.py:9:1 [FURB111]: Replace `lambda: print()` with `print`
test/data/err_111.py:10:1 [FURB111]: Replace `lambda x: bool(x)` with `bool`
test/data/err_111.py:11:1 [FURB111]: Replace `lambda x, y: f(x, y)` with `f`
test/data/err_111.py:13:1 [FURB111]: Replace `lambda: []` with `list`
test/data/err_111.py:14:1 [FURB111]: Replace `lambda: {}` with `dict`
test/data/err_111.py:15:1 [FURB111]: Replace `lambda: ()` with `tuple`
test/data/err_111.py:17:1 [FURB111]: Replace `lambda x: mod.cast(x)` with `mod.cast`