python / python/typing_extensions

TypeError when omitting a Protocol type argument with default

オープン
#636 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

主要言語
Python
スター
583
フォーク
146
平均マージ
10時間 11分
マージ済み PR(30日)
5

説明

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.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

サポートされている Python バージョンで例を再現し、その後 traceback に表示される _check_generic の周辺を typing_extensions.py で調べてください。B2[str] と、動作する回避策である B1[str] を比較してください。報告された TypeError が解消され、ジェネリック引数の検証にリグレッションがないことを確認できれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
devtools
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
45/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。