-
Notifications
You must be signed in to change notification settings - Fork 216
Open
astral-sh/ruff
#22799Labels
bugSomething isn't workingSomething isn't workingenumsnarrowingrelated to flow-sensitive type narrowingrelated to flow-sensitive type narrowingruntime semanticsAccurate modeling of how Python's semantics work at runtimeAccurate modeling of how Python's semantics work at runtime
Milestone
Description
Summary
Originally reported in https://discuss.python.org/t/amend-pep-586-to-make-enum-values-subtypes-of-literal/59456/9.
At runtime, case statements that use literal patterns or value patterns dispatch based on equality, and Color.GREEN == "g" in the following example (since Color has str in its MRO as well as Enum, and inherits its __eq__ method from str). We don't recognise this correctly in ty currently:
from enum import StrEnum
from typing import Literal, assert_never, reveal_type
class Color(StrEnum):
RED = "r"
GREEN = "g"
BLUE = "b"
def test_literal_as_enum(x: Literal["g"]) -> None:
match x:
case Color.RED:
assert_never(x)
case Color.GREEN:
reveal_type(x) # this branch is taken at runtime
case Color.BLUE:
assert_never(x)
case _:
assert_never(x) # false-positive error here
def test_enum_as_literal(y: Literal[Color.BLUE]) -> None:
match y:
case "r":
assert_never(y)
case "g":
assert_never(y)
case "b":
reveal_type(y) # this branch is taken at runtime
case _:
assert_never(y) # false-positive error hereVersion
No response
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't workingenumsnarrowingrelated to flow-sensitive type narrowingrelated to flow-sensitive type narrowingruntime semanticsAccurate modeling of how Python's semantics work at runtimeAccurate modeling of how Python's semantics work at runtime