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
From the title I thought it was going to be a dupe of #2608, but it's not!
The dumb workaround is:
from typing import Optional
animals = ["cow"]
def find1(name: str) -> Optional[str]:
ret = next(
filter(
lambda animal: animal.upper() == name,
animals,
),
None,
)
return ret
I think this boils down to the fact that mypy does complicated things to infer types for lambdas. If there's an annotation, it takes into account what type the expression is expected to be to try and understand the lambda. E.g., if you were to change ret = next(...) to ret: Optional[str] = next(...) you'd also get an error.
Related Bug
Could be a duplicate of:
Bug Report
When running mypy with a function returning an Optional the contains of a lambda expression is interpreted as optional and a type error is raised
To Reproduce
Run
mypy
against the following file:Expected Behavior
No type errors should be returned
Actual Behavior
NOTE: Same error with returning "Union[str, None]"
NOTE: Removing the Optional[str] stops the error, but that defaults the point
Your Environment
mypy.ini
(and other config files): NoneThe text was updated successfully, but these errors were encountered: