refurb
refurb copied to clipboard
[Bug]: FURB111 false positive with lambda late binding
Has your issue already been fixed?
- [x] Have you checked to see if your issue still exists on the
masterbranch? See the docs for instructions on how to setup a local build of Refurb. - [x] Have you looked at the open/closed issues to see if anyone has already reported your issue?
- [x] If reporting a false positive/incorrect suggestion, have you double checked that the suggested fix changes the code semantics?
The Bug
The following code:
def f(par_i):
return par_i
functions = [
lambda bound_i=loop_i: f(bound_i)
for loop_i in range(3)
]
print([f() for f in functions])
correctly prints
[0, 1, 2]
and emits the following error:
$ refurb file.py
[FURB111]: Replace `x` with `f`
But it should not be emitting an error because if I fix as suggested:
def f(par_i):
return par_i
functions = [
lambda: f(loop_i)
for loop_i in range(3)
]
print([f() for f in functions])
it prints
[2, 2, 2]
that is incorrect. This happens because of late binding, see https://docs.python-guide.org/writing/gotchas/#late-binding-closures.
Version Info
Refurb: v2.0.0
Mypy: v1.15.0
Python Version
Python 3.13.0
Config File
Extra Info
No response