ParamSpec: No support for adding keyword-only argument
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ả
I've been looking through PEP 612 but I just wasn't able to find any way to implement behavior that would allow me to do this:
P = ParamSpec("P")
R = TypeVar("R")
def foo(f: Callable[P, R]) -> Callable[P, List[R]]: # Here, the parameters are actually P + optional kw-only int x = 3
def inner(*args: P.args, x: int = 3, **kwargs: P.kwargs) -> List[R]:
results = []
for _ in range(x):
ret = f(*args, **kwargs)
results.append(ret)
return results
return inner
@foo
def bar(p: int):
return p + 2
bar(5) # Acceptable
bar(5, x=8) # Should also be Acceptable
Proposed solution:
Callable[[P.args, x: int = 3, P.kwargs], List[R]]
EDIT: I didn't notice that this was already in rejected alternatives section (I didn't scroll that far), however I still think an implementation like this would be worth adding since I have already encountered the need for this twice and there's simply absolutely no way to handle it.
I'm fine with the proposed syntax suggestion of
Concatenate[("x", int), P]
Though it didn't mention anything about default values, but those could just be handled by a third value in that tuple. It would probably be a lot easier to implement than my proposed suggestion, though to be fair, at least to me, my suggestion looks a bit cleaner.
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 bằng cách đọc các phương án bị từ chối của PEP 612 và các đặc tả ParamSpec và Concatenate hiện có. Issue không nêu tên tệp triển khai hay bài kiểm thử nào, vì vậy trước tiên hãy xác định các điểm vào liên quan trong typing-spec; công việc được xem là hoàn tất khi đã thống nhất cú pháp và xác định hành vi cho các tham số keyword-only tùy chọn.
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
- tooling
- Loại issue
- Tính năng
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- 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
- 25/100