Skip to content

False positive for lambda within a function #10426

Description

@michaelbrewer

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:

fromtypingimportOptionalanimals= ["cow"]
deffind1(name: str) ->Optional[str]:
returnnext(
filter(
lambdaanimal: 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

deffind1(name: str):
returnnext(
filter(
lambdaanimal: 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions