python / python/typing_extensions

TypeError when omitting a Protocol type argument with default

Đang mở
#636 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Ngôn ngữ chính
Python
Star
583
Fork
146
Merge trung bình
10 giờ 11 phút
Pull request đã merge (30 ngày)
5

Mô tả

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.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Tái hiện ví dụ trên các phiên bản Python được hỗ trợ, sau đó kiểm tra trong typing_extensions.py phần xung quanh _check_generic, vốn xuất hiện trong traceback. So sánh B2[str] với workaround B1[str] đang hoạt động; được coi là hoàn tất khi TypeError đã được báo cáo được khắc phục mà không gây hồi quy trong việc xác thực đối số generic.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
devtools
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
45/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.