Clarify the valid use locations of `typing.Concatenate`
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 1.8k
- Fork
- 302
- Merge trung bình
- 23 giờ
- Pull request đã merge (30 ngày)
- 8
Mô tả
In python/cpython#142965, it was reported that the documentation of typing.Concatenate is "incorrect" (documentation that originates back to python/cpython#24000).
@A5rocks, in your example, Concatenate[int, P_2] somewhat finally lands as a first argument to Callable, just indirectly.
This snippet previously defined:
P_2 = ParamSpec("P_2")
class X(Generic[T, P]):
f: Callable[P, int]
x: T
Similar example is included in the typing spec:
https://typing.python.org/en/latest/spec/generics.html#user-defined-generic-classes
class X[T, **P]:
f: Callable[P, int]
x: T
# (...)
def accept_concatenate[**P](x: X[int, Concatenate[int, P]]) -> str: ... # Accepted
However, it seems that the current situation (confirmed with mypy, pyright and ty) is that you can use Concatenate in all valid locations of ParamSpec except directly in a Concatenate. I.e., you can't do Concatenate[int, Concatenate[str, P]]), but you can (besides passing Concatenate form as the first argument to Callable):
- Accumulate
Concatenates asParamSpecs
from collections.abc import Callable
from typing import Concatenate
type Y[**P] = Callable[Concatenate[int, P], None]
type X[**P] = Y[Concatenate[int, P]]
def foo(f: X[str]) -> None:
reveal_type(f)
# mypy: def (builtins.int, builtins.int, builtins.str)
# pyright: (int, int, str) -> None
# ty: (...) -> None
- Bind
Concatenates to user-defined generics asParamSpecs
class X[T, **P]:
f: Callable[P, int]
x: T
def accept_concatenate[**P](x: X[int, Concatenate[int, P]]) -> str: ...
- Use
Concatenateas a type argument totuple-- is this correct?
from typing import Concatenate
def c(t: tuple[Concatenate[int, ...]]) -> None:
reveal_type(c)
# mypy: def (t: tuple[[builtins.int, *Any, **Any]])
# pyright: (t: tuple[Concatenate[int, ...]]) -> None
# ty: def c(t: tuple[@Todo]) -> None
I've found this mostly by poking around -- I haven't analyzed the actual implementations (yet).
I'll continue to investigate this from these searches.
I think that the valid use locations of Concatenate should be clarified in the typing spec and then in the CPython docs.
Is there anything else I overlooked? CC @JelleZijlstra @AlexWaygood
Hướng dẫn đóng góp
Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu với phần về các lớp generic do người dùng định nghĩa trong đặc tả typing và tài liệu CPython dành cho typing.Concatenate, sau đó so sánh các ví dụ và hành vi được báo cáo của mypy, pyright và ty. Xem lại các ghi chú tìm kiếm Concatenate được liên kết và xác định những vị trí sử dụng nào là hợp lệ; công việc được xem là hoàn tất khi đặc tả typing và tài liệu CPython mô tả rõ các dạng được hỗ trợ và hạn chế đối với Concatenate lồng nhau.
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
- documentation
- Loại issue
- Tài liệu
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 38/100