Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
Closed
Description
Bug report
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 passingobject 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
Metadata
Metadata
Assignees
Labels
Projects
Status
Done