-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Use EnumMeta instead of Enum to mark enum classes #4319
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
Changes from all commits
788944b
72f7f49
1e404fa
032ab5d
3c16eea
d8a7e77
b421be3
a1d61e7
5dca0e2
029fd32
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,10 +6,42 @@ class Medal(Enum): | |
gold = 1 | ||
silver = 2 | ||
bronze = 3 | ||
reveal_type(Medal.bronze) # E: Revealed type is '__main__.Medal' | ||
m = Medal.gold | ||
m = 1 | ||
[out] | ||
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] | ||
from enum import EnumMeta | ||
class Medal(metaclass=EnumMeta): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code doesn't actually work -- you need to also inherit from
(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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure what to do about it. If Medal inherits from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can add a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd say add a comment explaining that it doesn't work at runtime and what you are testing for. |
||
gold = 1 | ||
silver = "hello" | ||
bronze = None | ||
# Without __init__ the definition fails at runtime, but we want to verify that mypy | ||
# uses `enum.EnumMeta` and not `enum.Enum` as the definition of what is enum. | ||
def __init__(self, *args): pass | ||
reveal_type(Medal.bronze) # E: Revealed type is '__main__.Medal' | ||
m = Medal.gold | ||
m = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "Medal") | ||
|
||
[case testEnumFromEnumMetaSubclass] | ||
from enum import EnumMeta | ||
class Achievement(metaclass=EnumMeta): pass | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This must also inherit from |
||
class Medal(Achievement): | ||
gold = 1 | ||
silver = "hello" | ||
bronze = None | ||
# See comment in testEnumFromEnumMetaBasics | ||
def __init__(self, *args): pass | ||
reveal_type(Medal.bronze) # E: Revealed type is '__main__.Medal' | ||
m = Medal.gold | ||
m = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "Medal") | ||
|
||
[case testEnumFromEnumMetaGeneric] | ||
from enum import EnumMeta | ||
from typing import Generic, TypeVar | ||
T = TypeVar("T") | ||
class Medal(Generic[T], metaclass=EnumMeta): # E: Enum class cannot be generic | ||
q = None | ||
|
||
[case testEnumNameAndValue] | ||
from enum import Enum | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 beEnum
s 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point on the first, but just to be safe. Also on the second, I mean to check that it fails correctly :)