Skip to content

Narrowed down types sometimes not propagated to lambda #4297

Description

@JukkaL

The errors generated for the following code are invalid (when using --strict-optional):

fromtypingimportOptional, Callabledeff1(key: Callable[[], str]) ->None: ...
deff2(key: object) ->None: ...
defg(b: Optional[str]) ->None:
ifb:
f1(lambda: b.upper()) # Item "None" of "Optional[str]" has no attribute "upper"z: Callable[[], str] =lambda: b.upper() # Item "None" of "Optional[str]" has no attribute "upper"f2(lambda: b.upper()) # No errorlambda: b.upper() # No error

My hypothesis is that callable type context for lambda somehow affects how type narrowing behaves.

This was reported by @tueda on Gitter.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions