python / python/cpython

[PEP 696] `TypeError` when subclassing and omitting `TypeVar` with default.

Open
#140,596 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug report

Bug description:

The following example raises a TypeError: Some type variables (T) are not listed in Generic[U]:

class Bar[T, S=T]: ...

class Baz[U](Bar[U]): ...
Full Traceback
Traceback (most recent call last):
  File "/home/rscholz/Downloads/tmp.py", line 3, in <module>
    class Baz[U](Bar[U]): ...
  File "/home/rscholz/Downloads/tmp.py", line 3, in <generic parameters of Baz>
    class Baz[U](Bar[U]): ...
  File "/usr/lib/python3.13/typing.py", line 1288, in _generic_init_subclass
    raise TypeError(f"Some type variables ({s_vars}) are"
                    f" not listed in Generic[{s_args}]")
TypeError: Some type variables (T) are not listed in Generic[U]

The PEP states that

non-overridden defaults should be substituted in, and type parameters with such defaults can be further specialised down the line.

And the typing spec uses similar phrasing, which makes me believe this is a bug. In the example above, I'd expect that Baz[U] is a Bar[U, U] by substitution + default.

CPython versions tested on:

3.14

Operating systems tested on:

Linux

Linked PRs
  • gh-155646

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

Start in /usr/lib/python3.13/typing.py at _generic_init_subclass, where the reported TypeError is raised, and compare the behavior with the PEP 696 and typing specification subclassing sections. Done means the shown Bar/Baz example follows the documented default-substitution behavior without raising the reported error.

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
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.