Use EnumMeta instead of Enum to mark enum classes#4319
Use EnumMeta instead of Enum to mark enum classes#4319msullivan merged 10 commits intopython:masterfrom
Conversation
58d94f3 to
788944b
Compare
emmatyping
left a comment
There was a problem hiding this comment.
Looks good, just a couple of requests.
| main:7: error: Incompatible types in assignment (expression has type "int", variable has type "Medal") | ||
| m = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "Medal") | ||
|
|
||
| [case testEnumFromEnumMetaBasics] |
There was a problem hiding this comment.
I think it would be helpful to have a testcase for something that inherits from a class that has EnumMeta as a metaclass, to check those classes are still found to be Enums for all intents and purposes.
Also perhaps a test of a EnumMeta deriving class that tries to use generics?
There was a problem hiding this comment.
I think it would be helpful to have a testcase for something that inherits from a class that has EnumMeta as a metaclass, to check those classes are still found to be Enums for all intents and purposes.
Will do, but note that this is already handled by classes that inherit Enum itself.
I'm not sure I understand the second suggestion. Should mypy support something like that?
There was a problem hiding this comment.
Good point on the first, but just to be safe. Also on the second, I mean to check that it fails correctly :)
|
Hm, I think you need to |
b8c46cf to
3c16eea
Compare
|
|
test-data/unit/check-enum.test
Outdated
There was a problem hiding this comment.
Needs a space between "in" and "assignment"
There was a problem hiding this comment.
Yep. Sorry about that.
d6ad59d to
d8a7e77
Compare
emmatyping
left a comment
There was a problem hiding this comment.
LGTM. Thanks for the fast fixes.
gvanrossum
left a comment
There was a problem hiding this comment.
Pretty cool how much code can be deleted!
mypy/semanal.py
Outdated
There was a problem hiding this comment.
So if the metaclass is a subclass of EnumMeta, it's not an enum? (I'm not sure I care, but I wonder if this is an intentional choice.)
|
|
||
| [case testEnumFromEnumMetaBasics] | ||
| from enum import EnumMeta | ||
| class Medal(metaclass=EnumMeta): |
There was a problem hiding this comment.
This code doesn't actually work -- you need to also inherit from enum.Enum, else the class definition fails at runtime with
TypeError: Medal().__init__() takes no arguments
(Note: I'm not asking that mypy enforce this -- but I still think the test should work as an example.)
There was a problem hiding this comment.
I'm not sure what to do about it. If Medal inherits from Enum, the test doesn't check for what I want it to check. We might as well drop it.
There was a problem hiding this comment.
I can add a def __init__(self, *args): pass constructor, if you deem it better. It works, though might be considered an implementation detail.
There was a problem hiding this comment.
I'd say add a comment explaining that it doesn't work at runtime and what you are testing for.
|
|
||
| [case testEnumFromEnumMetaSubclass] | ||
| from enum import EnumMeta | ||
| class Achievement(metaclass=EnumMeta): pass |
There was a problem hiding this comment.
This must also inherit from enum.Enum.
|
This seems to have broken typeshed CI. https://travis-ci.org/python/typeshed/jobs/364931878 I'll look into it now. |
This fixes an error in Travis that seems to have been caused by python/mypy#4319. The fix was taken from the stdlib/3.4/enum.pyi stub. Mypy no longer assumes that classes whose metaclass is EnumMeta are subclasses of Enum, so we can't bound the typevar on Enum.
This fixes an error in Travis that seems to have been caused by python/mypy#4319. The fix was taken from the stdlib/3.4/enum.pyi stub. Mypy no longer assumes that classes whose metaclass is EnumMeta are subclasses of Enum, so we can't bound the typevar on Enum.
This fixes an error in Travis that seems to have been caused by python/mypy#4319. The fix was taken from the stdlib/3.4/enum.pyi stub. Mypy no longer assumes that classes whose metaclass is EnumMeta are subclasses of Enum, so we can't bound the typevar on Enum.
This fixes an error in Travis that seems to have been caused by python/mypy#4319. The fix was taken from the stdlib/3.4/enum.pyi stub. Mypy no longer assumes that classes whose metaclass is EnumMeta are subclasses of Enum, so we can't bound the typevar on Enum.
Implement #4311.
The tests will fail until python/typeshed#1770 is synched.