Skip to content
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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

rchen152
Copy link
Collaborator

  • Protocol[T] behaves like Generic[T] when defining a generic class.
  • It's an error to include both Generic[T] and Protocol[T].
  • When 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.

docs/spec/generics.rst Outdated Show resolved Hide resolved
docs/spec/generics.rst Outdated Show resolved Hide resolved
docs/spec/protocol.rst Outdated Show resolved Hide resolved

Type checkers may warn when the type variable order is inconsistent::
Copy link
Member

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.

Copy link
Collaborator Author

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.

Copy link
Member

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 a Parent[int, str] and a Parent[str, int] due to the double inheritance.

To make sure I'm understanding: you meant Grandparent here? There is no double inheritance from Parent.

Copy link
Collaborator Author

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.

docs/spec/generics.rst Show resolved Hide resolved
docs/spec/generics.rst Show resolved Hide resolved
docs/spec/generics.rst Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants