You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It looks like the enum plugin incorrectly assumes that __bool__ on enum literals will always return Literal[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.
Since Enum literals have a string value and are disambiguated from actual string literals, based on whether or not the fallback type is an Enum, they will always implicitly be True, since the Enum members can't have an empty name.
Fixes: #17333
This ensures `can_be_true` and `can_be_false` on enum literals depends
on the specific `Enum` fallback type behind the `Literal`, since
`__bool__` can be overriden like on any other type.
Additionally typeops `true_only` and `false_only` now respect the
metaclass when looking up the return values of `__bool__` and `__len__`,
which ensures that a default `Enum` that doesn't override `__bool__` is
still considered always truthy.
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
Your Environment
mypy.ini
(and other config files): DefaultThe text was updated successfully, but these errors were encountered: