Skip to content

in operation on tuple of Literal doesn't narrow type #3474

@Keymasterer44

Description

@Keymasterer44

Describe the Bug

from typing import *

SeverityLevel = Literal["light", "minor", "major"]
SEVERITY_LEVELS = cast(tuple[SeverityLevel, ...], get_args(SeverityLevel))

severity_level_str: str = "light"
if severity_level_str in SEVERITY_LEVELS:
    reveal_type(severity_level_str)

When severity_level_str is checked to be in SEVERITY_LEVELS, this should narrow the type to SeverityLevel since only SeverityLevels are present in the tuple. In practice however, Pyrefly doesn't perform any narrowing based on the condition, severity_level_str is still just a str instead of SeverityLevel within the condition body.

Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeS4ATrgLYAEALqcROgOZ0Q3G6UN0AqADroRAZRgA3GJQhMAMlJhQ6AXjry5M1FADaQkFAhsAFgwMAaOgZqtel6yBqoAVvZABdcQFEAat4AlAEkAFQBNAH15P295MTU6AGNUOAYACgYAV2JYXQlpWQUlKCtCMo8rNhgGCNRKNjg0-Jk5UkVpKABKTpEROCVC0gjYDojUykQ6cYSDI1NzEBEIMCmB1uHisYZKTnQ6MRjg8KiYuMQROku6SiUdCKZiGDT%2BgvWR5S3KHvQQCxAyG5gKCkQgMWhQCgAYjoAAVSIDgVMMDgCElcOhIGxMpRUAwIOjCCJoRIYHQzAxiHBEAB6akAmBAkG8NjUmDoamYXCJODUxLozHY3H49l0MC8OioSSoaCobCwNEY4yCvHoui4Ygq9BwQnoMgMEzogC0BTgwpmIAAzIQAIwAJgMIl0MmolDgXnQmXQXB4fBgmENmAgN0SeOkCQA5MZ0LwYOGRNGGIabgBHTJBv2GgDWMFIhtQiUS8DgEYA7nV0HGfgBfP750MwABi0BgFDQWDwRDIICrQA

(Only applicable for extension issues) IDE Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedLarger than "good first issue", but still well-defined and ready for someone to pick upnarrowingIssues with narrowing - root cause is usually narrowing, flow handling, or bothtypechecking
    No fields configured for Feature.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions