Skip to content

generic GenericAlias #13941

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

jorenham
Copy link
Contributor

@jorenham jorenham commented May 4, 2025

I suppose that theoretically speaking (i.e. ignoring type-checkers), this brings support for higher-kinded typing 🤔.

Copy link
Contributor

github-actions bot commented May 4, 2025

Diff from mypy_primer, showing the effect of this PR on open source code:

pydantic (https://github.com/pydantic/pydantic)
- pydantic/v1/typing.py:47: error: Incompatible types in assignment (expression has type "tuple[()]", variable has type "type[GenericAlias]")  [assignment]
+ pydantic/v1/typing.py:47: error: Incompatible types in assignment (expression has type "tuple[()]", variable has type "type[GenericAlias[_OriginT_co]]")  [assignment]

discord.py (https://github.com/Rapptz/discord.py)
- discord/ext/commands/converter.py:1119: error: Return type "Greedy[T]" of "__class_getitem__" incompatible with return type "GenericAlias" in supertype "list"  [override]
+ discord/ext/commands/converter.py:1119: error: Return type "Greedy[T]" of "__class_getitem__" incompatible with return type "GenericAlias[type | TypeAliasType]" in supertype "list"  [override]

@JelleZijlstra
Copy link
Member

What problem does this solve? The class is not generic at runtime and I think pretending it is is likely to cause more confusion than it's worth.

@jorenham
Copy link
Contributor Author

What problem does this solve?

It makes it possible to annotate typing.get_origin:

typeshed/stdlib/typing.pyi

Lines 936 to 937 in 8b877a6

@overload
def get_origin(tp: GenericAlias) -> type: ...

Runtime-typing libraries that only accept subscripted types could also benefit from this: If e.g. x=list[int] is allowed but x=list isn't, they are now able to enforce this by annotating x: GenericAlias[list[Any]] (assuming static type-checker support). If I remember correctly, pydantic requires this is some places, raising a runtime error otherwise.

The class is not generic at runtime and I think pretending it is is likely to cause more confusion than it's worth.

I suppose that's a bit of a chicken/egg story. And in my experience, typeshed usually takes the lead in these kinds of things, and cpython then follows (e.g. #13760).
Also note that GenericAlias is likely only used by people with thorough knowledge about typing in Python. So I don't expect them to become confused by it being optionally generic.

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.

2 participants