-
Notifications
You must be signed in to change notification settings - Fork 237
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
Clarify some behavior around user-defined generic classes #1879
base: main
Are you sure you want to change the base?
Conversation
docs/spec/generics.rst
Outdated
|
||
Type checkers may warn when the type variable order is inconsistent:: |
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.
Should we mandate an error here instead? This code doesn't make sense, because a Child[int, str]
is both a Parent[int, str]
and a Parent[str, int]
due to the double inheritance. So a type checker should error here, because it can't build up this class in a correct way.
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.
The reason I didn't mandate an error is that mypy currently doesn't error on this. But I'm happy to change it if we think that would be better.
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 don't know if there is a rationale behind mypy's behavior, or how hard it would be to change, but it seems to me that this should be mandated as an error; I don't see any way to make sense of this definition.
This code doesn't make sense, because a
Child[int, str]
is both aParent[int, str]
and aParent[str, int]
due to the double inheritance.
To make sure I'm understanding: you meant Grandparent
here? There is no double inheritance from Parent
.
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.
Ok, I updated this section to mandate an error.
Protocol[T]
behaves likeGeneric[T]
when defining a generic class.Generic[T]
andProtocol[T]
.Generic[T]
is omitted, type variables are taken in order of first appearance in generic base classes.Discussion: https://discuss.python.org/t/clarifying-the-rules-for-subclassing-generic-classes/69698.