python / python/cpython

Alias that is generic over a ParamSpec cannot be further specialized

Open
#124,445 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

topic-typing type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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
  • gh-124512

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read _Py_make_parameters in genericaliasobject.c and reproduce the reported X[[T]][str] failure from the issue. Check the linked gh-124512 work before starting. Done means an alias specialized with a list containing a TypeVar can be subscripted again at runtime without the TypeError.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.