Skip to content

Local type narrowing not preserved in closures #2513

@grievejia

Description

@grievejia

Describe the Bug

Minimal repro:

def process_name(name: str) -> None:
    ...

def handle_request(name: str | None, use_callback: bool) -> None:
    if name is None:
        return
    # name is narrowed to str here

    def callback() -> None:
        process_name(name)  # FP: str | None not assignable to str

    if use_callback:
        callback()

It may not always be sound to do this kind of narrowing (esp. if some other closures can attempt to mutate the value of name) so this could be a good thing to warn in strict mode . But by default we might want consider to be more silent to match all other type checkers' behavior.

Sandbox Link

No response

(Only applicable for extension issues) IDE Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    typecheckingusabilityUsability & readiness issues identified with running Pyrefly on top OSS projects

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions