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

Alias that is generic over a ParamSpec cannot be further specialized #124445

Open
JelleZijlstra opened this issue Sep 24, 2024 · 1 comment
Open
Labels
topic-typing type-bug An unexpected behavior, bug, or error

Comments

@JelleZijlstra
Copy link
Member

JelleZijlstra commented Sep 24, 2024

A type alias that is generic over a ParamSpec can be specialized with a list of types, and if this list includes a TypeVar, the resulting alias is still generic to type checkers, but it cannot be subscripted again at runtime.

>>> from collections.abc import Callable
>>> type X[**P] = Callable[P, int]
>>> T = TypeVar("T")
>>> X[[T]]
X[[~T]]
>>> X[[T]][str]
Traceback (most recent call last):
  File "<python-input-14>", line 1, in <module>
    X[[T]][str]
    ~~~~~~^^^^^
TypeError: X[[~T]] is not a generic class

Pyright accepts this code, and I think it's right: https://pyright-play.net/?strict=true&code=GYJw9gtgBAxmA28CmMAuBLMA7AzgOgEMAjGKdCABzBFSgGEDFjkAoUSKVATwvSwHMylarQAqPJADUCIFi1FQAvFHEUpMgBQAiUVoCUc7mqgANANoAqCwAUAukvqN4zJGesAaMllS2WJhcrmZqK2viwAJkjAUMAaAB4AXKaiZjioILZ6CSxQuVAgSABuSIwA%2BkZI8Xq5AMRQGmkg1QC0AHxeqCxAA

Reported by @Daraan in https://github.com/python/typing_extensions/pull/449/files#r1771594266.

I believe this could be fixed by making _Py_make_parameters in genericaliasobject.c recurse into lists.

Linked PRs

@tomasr8
Copy link
Member

tomasr8 commented Sep 25, 2024

I'm working on a fix if that's ok :) After wrapping my head around the code a bit I think we'll also need to make _Py_subs_parameters recursive in order to get the correct substitution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-typing type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants