Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
788944b72f7f491e404fa032ab5d3c16eead8a7e77b421be3a1d61e75dca0e2029fd32File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
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): | ||
Member 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.) ContributorAuthor 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 ContributorAuthor 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 Collaborator 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 | ||
Member 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
EnumMetaas a metaclass, to check those classes are still found to beEnums for all intents and purposes.Also perhaps a test of a
EnumMetaderiving 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
Enumitself.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 :)