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
TypeError: Some type variables (+T) are not listed in Generic[~T] #94607
In Python 3.11.0b3 subclasses of generic classes seems to have started requiring explicitly filling in all the super-classes type vars, even though the subclass isn't supposed to be generic in the same variable as the super type.
The behavior is inconsistent and only throws an error if the subclass is also generic.
Minimal reproduction
fromtypingimportGenericfromtypingimportTypeVar# --- baseT_co=TypeVar("T_co", covariant=True)
classBase(Generic[T_co]):
...
# --- non-generic subclass, this doesn't throw an errorclassNonGenericSub(Base):
...
# --- subclass generic, in _another_ variableT=TypeVar("T")
# this throws an errorclassSub(Base, Generic[T]):
...
This gives this error:
Traceback (most recent call last):
File "/Users/annevoab/projects/phantom-types/reproduce.py", line 22, in <module>
class Sub(Base, Generic[T]):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/annevoab/.pyenv/versions/3.11.0b3/lib/python3.11/typing.py", line 1869, in __init_subclass__
raise TypeError(f"Some type variables ({s_vars}) are"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Some type variables (+T_co) are not listed in Generic[~T]
Workaround
By explicitly passing object to fill in the typevar of the super class, the error is surpressed.
classSub(Base[object], Generic[T]):
...
Your environment
CPython versions tested on: Python 3.11.0b3
Operating system and architecture: Macos 12.3.1 / Darwin Kernel Version 21.4.0: Fri Mar 18 00:45:05 PDT 2022; root:xnu-8020.101.4~15/RELEASE_X86_64 x86_64
Confirmed, and marking as a release blocker since this is a regression. I'll take a look later today. Also cc @Fidget-Spinner@serhiy-storchaka in case you have any insights.
Reacted by Ken Jin, Daniël van Noord, Anton Agestam and Mehdi Drissi
Thanks @Fidget-Spinner! I haven't done anything yet; I was planning to try and come up with a solution after work today, but now I'll get too review your PR instead.
Activity
JelleZijlstra commentedon Jul 6, 2022
Confirmed, and marking as a release blocker since this is a regression. I'll take a look later today. Also cc @Fidget-Spinner @serhiy-storchaka in case you have any insights.
Fidget-Spinner commentedon Jul 6, 2022
@JelleZijlstra bisect points me to commit b6a5d85.
CC @serhiy-storchaka
Fidget-Spinner commentedon Jul 6, 2022
Sorry @JelleZijlstra I hope I didn't create duplicate work, but I have a PR open at #94610.
JelleZijlstra commentedon Jul 6, 2022
Thanks @Fidget-Spinner! I haven't done anything yet; I was planning to try and come up with a solution after work today, but now I'll get too review your PR instead.
gh-94607: Fix subclassing generics (GH-94610)
pythongh-94607: Fix subclassing generics (pythonGH-94610)
gh-94607: Fix subclassing generics (GH-94610)