Skip to content

TypeError: Some type variables (+T) are not listed in Generic[~T] #94607

Closed
@antonagestam

Description

@antonagestam

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

from typing import Generic
from typing import TypeVar

# --- base
T_co = TypeVar("T_co", covariant=True)


class Base(Generic[T_co]):
    ...


# --- non-generic subclass, this doesn't throw an error
class NonGenericSub(Base):
    ...


# --- subclass generic, in _another_ variable
T = TypeVar("T")


# this throws an error
class Sub(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.

class Sub(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

Labels

3.11only security fixes3.12only security fixestopic-typingtype-bugAn unexpected behavior, bug, or error

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions