Closed
Description
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:
from typing import Optional
animals = ["cow"]
def find1(name: str) -> Optional[str]:
return next(
filter(
lambda animal: animal.upper() == name,
animals,
),
None,
)
Expected Behavior
No type errors should be returned
Actual Behavior
$ mypy bug.py
bug.py:9: error: Item "None" of "Optional[str]" has no attribute "upper"
NOTE: Same error with returning "Union[str, None]"
NOTE: Removing the Optional[str] stops the error, but that defaults the point
def find1(name: str):
return next(
filter(
lambda animal: animal.upper() == name,
animals,
),
None,
)
Your Environment
- Mypy version used: 0.812
- Mypy command-line flags: None
- Mypy configuration options from
mypy.ini
(and other config files): None - Python version used: 3.9.4
- Operating system and version: MacOS 10.15.7