Skip to content

TypeError when omitting a Protocol type argument with default #636

@jorenham

Description

@jorenham

This is very similar to python/cpython#137191, but with a slightly different error message:

from typing_extensions import Generic, Protocol, TypeVar

T1 = TypeVar("T1")
T2 = TypeVar("T2", default=object)

class A(Protocol[T1]): ...
class B1(A[T2], Protocol, Generic[T1, T2]): ...  # the workaround
class B2(A[T2], Protocol[T1, T2]): ...  # the problem

B1[str]  # ok
B2[str]  # TypeError

on 3.12.11:

Traceback (most recent call last):
  File "/home/joren/huh.py", line 11, in <module>
    B2[str]  # TypeError
    ~~^^^^^
  File "/home/joren/.pyenv/versions/3.12.11/lib/python3.12/typing.py", line 398, in inner
    return func(*args, **kwds)
           ^^^^^^^^^^^^^^^^^^^
  File "/home/joren/.pyenv/versions/3.12.11/lib/python3.12/typing.py", line 1110, in _generic_class_getitem
    _check_generic(cls, params, len(cls.__parameters__))
  File "/home/joren/.pyenv/versions/3.12.11/lib/python3.12/site-packages/typing_extensions.py", line 3085, in _check_generic
    raise TypeError(f"Too {'many' if alen > elen else 'few'} arguments"
TypeError: Too few arguments for <class '__main__.B2'>; actual 1, expected at least 1

on 3.9.23:

Traceback (most recent call last):
  File "/home/joren/huh.py", line 11, in <module>
    B2[str]  # TypeError
  File "/home/joren/.pyenv/versions/3.9.23/lib/python3.9/typing.py", line 277, in inner
    return func(*args, **kwds)
  File "/home/joren/.pyenv/versions/3.9.23/lib/python3.9/typing.py", line 1004, in __class_getitem__
    _check_generic(cls, params, len(cls.__parameters__))
  File "/home/joren/.pyenv/versions/3.9.23/lib/python3.9/site-packages/typing_extensions.py", line 3049, in _check_generic
    raise TypeError(f"Too {'many' if alen > elen else 'few'} {things}"
TypeError: Too few parameters for <class '__main__.B2'>; actual 1, expected at least 1

And since typing_extensions occurs in the traceback, I figured that this is not the same issue as python/cpython#137191.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions