Skip to content

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

Open

Description

@randolf-scholz

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

Activity

  1. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Oct 25, 2025
  2. randolf-scholz commented on Oct 27, 2025

    @randolf-scholz
    ContributorAuthor

    I think the root cause is that runtime does not correctly perform the substitution:

    class Bar[T, S=T]: ...
    
    print(Bar[int])  # __main__.Bar[int, T], but should be Bar[int, int]

    Opened #140665 with this more precise bug report, I would suspect when #140665 is fixed, the OP bug report evaporates automatically.

  3. RyanSaxe commented on Dec 23, 2025

    @RyanSaxe

    I think the root cause is that runtime does not correctly perform the substitution:

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

    print(Bar[int]) # main.Bar[int, T], but should be Bar[int, int]
    Opened #140665 with this more precise bug report, I would suspect when #140665 is fixed, the OP bug report evaporates automatically.

    I just ran into this and am using the following workaround.

    class RootGenericWithTypeVarDefaults[A, B=A, C=B, ...]:
        @classmethod
        def __class_getitem__(cls, items: tuple[type, ...]):
            num_params = len(cls.__parameters__)
            if len(items) == 0:
                raise TypeError("this solution requires at minimum the first type to be specified")
            if len(items) < num_params:
                items = (*items, *(items[-1],) * (num_params - len(items)))
            return super().__class_getitem__(items)

    Of course this does not handle all cases of possible defaults, it handles a common, simple, case. The point is, you can override __class_getitem__ and do the substitution yourself in order to bypass the error from cpython. And the reason I chose to do this is that most type checkers / LSPs properly implement the PEP, so using defaults with TypeVars still seemed to be useful as your editor can understand it and it provides a better user experience.

  4. KotlinIsland commented on May 23, 2026

    @KotlinIsland
    Contributor

    i'm working on this :)

  5. added 2 commits that reference this issue on Nov 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytopic-typingtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions