Skip to content

Narrowed down types sometimes not propagated to lambda #4297

Closed
@JukkaL

Description

@JukkaL

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

from typing import Optional, Callable

def f1(key: Callable[[], str]) -> None: ...
def f2(key: object) -> None: ...

def g(b: Optional[str]) -> None:
    if b:
        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 error
        lambda: 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