-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Deriving builtins from ABCs implies incorrect and problematic metaclass structure #1595
Comments
Possible solution, as discussed with @ilevkivskyi and @JukkaL in gitter: use
A caveat is that this does not compile with generic type, i.e. |
Maybe if Sequence were a Protocol this would be less of a problem?
|
I also think this is the right way forward. Although it will be better to first implement more advanced |
Avoid inconsistent metaclass structure for enum mixins due to python#1595 - e.g. mypy/python#2824
Avoid inconsistent metaclass structure for enum mixins due to python#1595 - e.g. mypy/python#2824
I have a similar problem in HypothesisWorks/hypothesis#858 - sampling from an Enum works, but Mypy complains about import enum
print(len(enum.Enum('A', 'a b c'))) # prints "3"
# error: Argument 1 to "len" has incompatible type "Enum"; expected "Sized" I think it belongs here, but happy to move to another issue or open on if that's more useful. |
@Zac-HD this seems to be a mypy-specific problem, since it works if you define the enum using the class syntax. Note that the type in the error message is incorrect - it should be a |
Yes, mypy has special treatment of enum definitions, so it doesn't detect it in this particular "in-place" definition. |
This will require type system features to fix, so five years on, is better discussed at https://github.com/python/typing |
typeshed claims that
str
derives fromSequence
and therefore indirectly fromcollections_abc.Sized
whose metaclass is declared to be ABCMeta. This is done as a type-checkable alternative toSequence.register(str)
which handles isinstance and issubclass calls.However derivation and register are not indistinguishable, since the metaclass structure is different.
issubclass(type(str), ABCMeta)
should be False, otherwise the definitionclass A(str, Enum): pass
would fail at runtime due to inconsistent metaclass structure:The error is that
type(Enum) is EnumMeta
andtype(Sized) is ABCMeta
and they are unrelated.This bug has at least the following implications:
a: ABCMeta = str
typechecks.class A(str, Enum)
appears to have inconsistent metaclass structure, even though this is the recommended way of making an enum of strings. It might also be the case with some other builtins.The text was updated successfully, but these errors were encountered: