You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[PEP 696] TypeError when subclassing and omitting TypeVar with default. #140596
The following example raises a TypeError: Some type variables (T) are not listed in Generic[U]:
classBar[T, S=T]: ...
classBaz[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]
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.
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.
classRootGenericWithTypeVarDefaults[A, B=A, C=B, ...]:
@classmethoddef__class_getitem__(cls, items: tuple[type, ...]):
num_params=len(cls.__parameters__)
iflen(items) ==0:
raiseTypeError("this solution requires at minimum the first type to be specified")
iflen(items) <num_params:
items= (*items, *(items[-1],) * (num_params-len(items)))
returnsuper().__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.
Activity
randolf-scholz commentedon Oct 27, 2025
I think the root cause is that runtime does not correctly perform the substitution:
Opened #140665 with this more precise bug report, I would suspect when #140665 is fixed, the OP bug report evaporates automatically.
RyanSaxe commentedon Dec 23, 2025
I just ran into this and am using the following workaround.
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 fromcpython. 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.KotlinIsland commentedon May 23, 2026
i'm working on this :)
pythongh-140596: Substitute earlier type parameters into PEP 696 defa…
fixup! pythongh-140596: Substitute earlier type parameters into PEP 6…