Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
antonagestam opened this issue Jul 6, 2022 · 4 comments
Closed
Assignees
Labels
3.11 only security fixes 3.12 bugs and security fixes topic-typing type-bug An unexpected behavior, bug, or error

Comments

@antonagestam
Copy link

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
@JelleZijlstra
Copy link
Member

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
Copy link
Member

Fidget-Spinner commented Jul 6, 2022

@JelleZijlstra bisect points me to commit b6a5d85.

CC @serhiy-storchaka

@Fidget-Spinner
Copy link
Member

Sorry @JelleZijlstra I hope I didn't create duplicate work, but I have a PR open at #94610.

@JelleZijlstra
Copy link
Member

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.

@AlexWaygood AlexWaygood moved this from Todo to In Progress in Release and Deferred blockers 🚫 Jul 7, 2022
@JelleZijlstra JelleZijlstra removed their assignment Jul 7, 2022
Fidget-Spinner added a commit that referenced this issue Jul 9, 2022
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jul 9, 2022
Co-authored-by: Serhiy Storchaka <[email protected]>
(cherry picked from commit 6442a9d)

Co-authored-by: Ken Jin <[email protected]>
miss-islington added a commit that referenced this issue Jul 9, 2022
Co-authored-by: Serhiy Storchaka <[email protected]>
(cherry picked from commit 6442a9d)

Co-authored-by: Ken Jin <[email protected]>
Repository owner moved this from In Progress to Done in Release and Deferred blockers 🚫 Jul 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.11 only security fixes 3.12 bugs and security fixes topic-typing type-bug An unexpected behavior, bug, or error
Projects
Development

No branches or pull requests

5 participants