Skip to content

support narrowing enum values using == and != #10915

Description

@DetachHead
fromtypingimportNoReturn, Literalfromenumimportauto, Enumdeffoo(value: Literal["foo","bar"]) ->None:
ifvalue=="foo":
reveal_type(value) #note: Revealed type is "Literal['foo']"elifvalue!="bar":
reveal_type(value) #error: Statement is unreachable [unreachable]classFoo(Enum):
foo=auto()
bar=auto()
defbar(value: Foo) ->None:
ifvalue==Foo.foo:
reveal_type(value) #Revealed type is "__main__.Foo"elifvalue!=Foo.bar:
reveal_type(value) #no error, even though this is unreachable

https://mypy-play.net/?mypy=latest&python=3.10&flags=show-column-numbers%2Cshow-error-codes%2Cstrict%2Ccheck-untyped-defs%2Cdisallow-any-decorated%2Cdisallow-any-expr%2Cdisallow-any-explicit%2Cdisallow-any-generics%2Cdisallow-any-unimported%2Cdisallow-incomplete-defs%2Cdisallow-subclassing-any%2Cdisallow-untyped-calls%2Cdisallow-untyped-decorators%2Cdisallow-untyped-defs%2Cwarn-incomplete-stub%2Cwarn-redundant-casts%2Cwarn-return-any%2Cwarn-unreachable%2Cwarn-unused-configs%2Cwarn-unused-ignores&gist=0f58b3bb7b93c7e3d917c18a7df2ed39

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions