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
{{ message }}
Repository navigation
“Cannot infer type of lambda” using default parameter to recapture loop variable #15459
One of the most common gotchas in Python is that a loop reassigns its iteration variable rather than creating a new binding for each iteration, and a lambda closure created in the loop observes this reassigned value rather than the value from each iteration. A typical workaround is to add a “redundant” default parameter to the lambda to recapture the value from each iteration—for example, the i=i from the above link (reproduced below). But mypy rejects such a lambda with error: Cannot infer type of lambda [misc].
mypy ought to be able to realize that since the lambda is coerced to Callable[[int], int], its second parameter i=i can only ever receive the default value, so its type should be successfully inferred as (x: int, i: int = ...) -> int.
Actual Behavior
main.py:4: error: Cannot infer type of lambda [misc]
Found 1 error in 1 file (checked 1 source file)
Your Environment
Mypy version used: 1.3.0
Mypy command-line flags: none
Mypy configuration options from mypy.ini (and other config files): none
To address this issue, you can provide an explicit type annotation for the lambda expression. This helps MyPy understand the intended types and avoid the type inference problem.
@myselfAbdullah007 No, you can’t: the Python syntax does not allow lambda parameters to be annotated. Anyway, I’m not looking for support; I’m plenty capable of working around this in my own code. I filed this issue so it can be fixed in mypy for everyone.
Activity
Improved type inference for lambdas in the case where a parameter inc…
django_api: Extract send_event_on_commit helper.
Improved type inference for lambdas in the case where a parameter inc…
django_api: Extract send_event_on_commit helper.
django_api: Extract send_event_on_commit helper.
Merge upstream changes
myselfAbdullah007 commentedon Jun 26, 2023
To address this issue, you can provide an explicit type annotation for the lambda expression. This helps MyPy understand the intended types and avoid the type inference problem.
andersk commentedon Jun 26, 2023
@myselfAbdullah007 No, you can’t: the Python syntax does not allow
lambdaparameters to be annotated. Anyway, I’m not looking for support; I’m plenty capable of working around this in my own code. I filed this issue so it can be fixed in mypy for everyone.django_api: Extract send_event_on_commit helper.
django_api: Extract send_event_on_commit helper.