You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The expected behavior is that variable should be considered used, since it is used to set the default argument for variable in the lambda. This is actually a fairly common idiom to get "early-binding" behavior in lambdas (though having *args in a lambda is considerably less common, so I'm not entirely shocked that — as far as I saw — this hasn't been reported before).
With some further explorations, I have determined:
Multiple non-*args arguments, or **kwargs alone is not enough to trigger this bug.
Replacing the lambda with the equivalent def statement also won't trigger the bug:
If you change the name of the variable that variable gets bound to, it also won't trigger the bug (lambda *args, var=variable: var + 1 works fine).
Here is a minimal Python file that shows all of these properties:
# pylint: disable=missing-module-docstring, missing-docstring# pylint: disable=invalid-name# These trigger the bugdeflambda_with_args():
variable=1returnlambda*_, variable=variable: variable+1deflambda_with_args_kwargs():
variable=1returnlambda*_, variable=variable, **_kwargs: variable+1deflambda_with_args_and_multi_args():
variable=1returnlambda*_, a, variable=variable: variable+a# The rest of these do not trigger the bugdeflambda_with_multi_args():
variable=1returnlambdaa, variable=variable: variable+adeflambda_without_args():
variable=1returnlambdavariable=variable: variable+1deflambda_with_kwargs():
variable=1returnlambdavariable=variable, **_: variable+1deffunc_def():
variable=1deff(*_, variable=variable):
returnvariable+1returnfdefdifferent_name():
variable=1returnlambda*args, var=variable: var+1
Doing a bit more digging, it seems that prior to pylint==2.6.0, this bug also affected the def f(*_, variable=variable) variant, and in earlier versions even more erroneous warnings were raised as well. Not entirely sure, but I suspect #3713 fixed the issue for the def version of this.
The text was updated successfully, but these errors were encountered:
When running
pylint
over the following function, it warns that the variablevariable
(the one defined in the function, not the parameter), is unused:I see the same issue when you add
**kwargs
and/or other parameters to the lambda:The expected behavior is that
variable
should be considered used, since it is used to set the default argument forvariable
in the lambda. This is actually a fairly common idiom to get "early-binding" behavior in lambdas (though having*args
in a lambda is considerably less common, so I'm not entirely shocked that — as far as I saw — this hasn't been reported before).With some further explorations, I have determined:
Multiple non-
*args
arguments, or**kwargs
alone is not enough to trigger this bug.Replacing the
lambda
with the equivalentdef
statement also won't trigger the bug:If you change the name of the variable that
variable
gets bound to, it also won't trigger the bug (lambda *args, var=variable: var + 1
works fine).Here is a minimal Python file that shows all of these properties:
pylint --version output
Doing a bit more digging, it seems that prior to
pylint==2.6.0
, this bug also affected thedef f(*_, variable=variable)
variant, and in earlier versions even more erroneous warnings were raised as well. Not entirely sure, but I suspect #3713 fixed the issue for thedef
version of this.The text was updated successfully, but these errors were encountered: