Skip to content

Narrow the type of a when branching on a in b #2608

@grievejia

Description

@grievejia

Describe the Bug

Minimal repro:

# Return type is a union of tuples
def parse_resource_id() -> tuple[str, str] | tuple[None, None]:
    ...

# After destructuring, kind: str | None. The `not in` guard should narrow
# it to str, but pyrefly does not narrow via `not in`.
def lookup_resource(registry: dict[str, str]) -> str | None:
    kind, _obj_id = parse_resource_id()

    if kind not in registry:
        return None

    return registry[kind]  # Pyrefly: str | None not assignable to str key

Mypy, Pyright, and Ty all choose to remain silent on the error.

Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeSIAxAAQBKMALgK4BO61DpxM1Ec1qak3QRc7XGA5NisOAB0sMScVQs4MAPot4uVgGNNETAAoAlNQC0APikyYAbTgMWAGmpOWAXWoAfW7HsAOTEYN2D0GE9EBWpY6kIEhQUaAEEwBhgWakx4ZyY9ZhYIdABzNwBrYsxEd2dfanCYQmoAFQALHgADdFwGXnRO6hKmVUx3Nt0oMfRVFlwAd2TePoZcWtdqbCY%2Bsm0wKFJs3HhqHr6Zljn56gA3CEFu3v7OwgUcyShcXHLpLR19GDGbQlPjOUg1TAQAqOZxuDyeczWdb1RrRdhxSpYNwaXDYABWGiM1AAvNQVGpNNo4LoWAZCSZTEl0bEIJJMdMnsVqMDQSxwTE4oLtIV2I0mULGKx2DyPKR7OzvNQaAAxAAKNQ8KJCpyeqDgcAgJRm2FgHDWmvKMHILhA22gcBI5EQVGoAFUGNAIJxqGBhAVROh5Og3kofbgWABbVAMDToJgR7CZYz4GrFBiImweNESkU%2BuQgQLxxMsGrAfAAX3zChANt2SgOhFWEagFBoqtIewO7gwOAI1D0YkgwxY0YDr3QNAAyjAeG0GAxiHBEAB6Zd1-akQjhkrLmDoZeYXB6ODLgfoIesUdiZdhrKoG6oaCoE08M8XkcesTUXDET%2BB8dkAwEzoBYNyZAaX6kvmADMhAAIwAExVugIDljaqD%2BmByrQDAFBoFgeBEGQqFAA

(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

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions