python / python/typing_extensions

TypeError when omitting a Protocol type argument with default

Open
#636 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
583
Forks
146
Avg merge
10h 11m
Merged PRs (30d)
5

Description

This is very similar to https://github.com/python/cpython/issues/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 https://github.com/python/cpython/issues/137191.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the example on the supported Python versions, then inspect typing_extensions.py around _check_generic, which appears in the traceback. Compare B2[str] with the working B1[str] workaround; done means the reported TypeError is resolved without regressing generic argument validation.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.