Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork3.1k
Closed
Description
Bug Report
It looks like the enum plugin incorrectly assumes that__bool__ on enum literals will always returnLiteral[True], while this is the default behavior, it is not correct when someone explicitly overrides__bool__. Moreover this special casing is only applied to enum literals, but not the base enum type, which should behave the same as individual members.
To Reproduce
https://mypy-play.net/?mypy=master&python=3.12&gist=dc60c5c82ba6589e5f1d03654439b05e
importenumfromtypingimportAny,LiteralclassFoo(enum.Enum):X=0classBar(enum.Enum):X=0def__bool__(self)->Literal[False]:returnFalsedefa(x:Any|Literal[Foo.X]):reveal_type(x)ifx:reveal_type(x)else:reveal_type(x)defb(x:Any|Foo):reveal_type(x)ifx:reveal_type(x)else:reveal_type(x)# wrong, doesn't match literal behaviordefc(x:Any|Literal[Bar.X]):reveal_type(x)ifx:reveal_type(x)# wrong, it's backwardselse:reveal_type(x)# wrong, it's backwardsdefd(x:Any|Bar):reveal_type(x)ifx:reveal_type(x)else:reveal_type(x)
Your Environment
- Mypy version used: Playground with master branch
- Mypy command-line flags: Default
- Mypy configuration options from
mypy.ini(and other config files): Default - Python version used: 3.12