Skip to content

in narrowing not working for frozenset created with literal iterable #2744

@grievejia

Description

@grievejia

Describe the Bug

Minimal repro:

CLEAN_OP_CHANGED_STR = dict(
    autoremove="The following packages will be REMOVED",
    autoclean="Del ",
)

def cleanup(operation: str | None = None) -> bool:
    if operation not in frozenset(["autoremove", "autoclean"]):
        raise AssertionError(f"Bad operation: {operation}")
    # After the guard + raise, operation is narrowed to str by pyright.
    # Pyrefly still sees str | None and reports:
    #   Invalid key for TypedDict `<anonymous>`, got `None`
    return CLEAN_OP_CHANGED_STR[operation] in "some output"  # FP

No other type checker emit the error.

Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeSIAwgDICiAggHID6A8gArOUASTA4rQAizAMoAVAEoACALxTMEAMYAXABQAddFO1TUAV2W4ATjAC2uAG4wZ6kGIAWMKWFxQouAO4R0AcynFURQBrVB94KS83KWwnCVoAWVYANSFbABpNHV0DXEVYDBsQQRgoKXTNAEpNTUwYMCk8mAw9YlVcYhgjVGUIXHREKThlIykAHylGPqc5SfQYCqkAWgA%2BaNxXREydCHr2zu7erXRcZSlvZyNcAC8YdDgYNQBtW31DE3MrdLKQV9z89FsAF0KpstFkdF0IPcpPQ4PcjD0%2BrQjJcjKowLYAEKoTBSPZdRH9KTAfEHPoAX1sVTB2gAxDCwMpOlJlI4pD49KgjLiANRSSH3NJ4joEw5nOBSdBcy4eGC4wyDYbRUj%2BUhGCA%2BezKQhbOlSdhqupQFVDaCle7hIYjcazJwYXEmYjGZRwUFZenaACS6AsqCgEFxQRgKpcIzEpA6mEESlOAAMADwYPqkcx6ODLWNCnwnKSx22x3X8h56IxaGgMFgcLi8RgCYTiCSPUmEwFnLS2OC4UxOXAGYgGWx6gBi7BAaRABmgcBI5EQIHpAFUev7lCG9OgVIc4NUsHVnMZTN1mOg9KYYmj8ANvMoFitFUY3RDi6XnLZGKfzwNgPhKSBNGOQDIEwwGNQhDFMKAKHpA1gONQZ7TwfAGj6SAOVFPodXQekRBgJwtWUYhXQAeiIoCjVIQhjB8IjbiIzBcjgIjFBQjUSzJdAiP3EZUF9aBUGwWBkPQVC2MJYVCWnTQyFZPpFisIw4DFORbAAZkIABGAAmWx-3JcdAh6Kwh2gGAKDQLBEJnEBySAA

(Only applicable for extension issues) IDE Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    narrowingIssues with narrowing - root cause is usually narrowing, flow handling, or bothtypecheckingusabilityUsability & 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