-
-
Notifications
You must be signed in to change notification settings - Fork 3k
[1.16 regression] no longer recognizes enum singleton as only possible case #19271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Wow, and this is rather important - such enums are commonly used as sentinel values, like |
This is only for things named
No, you cannot subclass enums with members. Enums are implicitly final. |
@A5rocks Ough, I haven't noticed that this only happens for members named literally |
I have an enum class
Raise
that has a single member,Raise._
. I use this as a pattern to indicate that a function argument, if not specified, will default to raising an exception. Essentially it serves as an alternative toNone
, which a caller might want to explicitly pass. The following used to work in 1.15 but 1.16 complains.In 1.15 it appears that that mypy understood that
Raise._
was the only value of typeRaise
. In 1.16 that returnserror: Incompatible return value type (got "str | Raise", expected "str") [return-value]
.Note that originally I did not have the
@final
decoration. After some reading I am unsure whether or not it should be required for this example to work, but it does not seem to affect the behavior of either 1.15 or 1.16. It would seem that the @Final should be necessary for mypy to recognize thatRaise._
is the only possible value of typeRaise
, unless it was proving to itself that there is no subclass ofRaise
.Obviously, I can change the implementation to
if is instance(Raise._, Raise): raise
, but there is an apparent loss of sophistication in 1.16 so I thought it was worth reporting.The text was updated successfully, but these errors were encountered: