Does `field_specifier` support non-keyword arguments?
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 was reading through the specification of dataclass_transform and it doesn't really specify what field_specifiers looks like. It mostly just says:
field_specifiers (tuple[Callable[..., Any], ...]) – Specifies a static list of supported classes or functions that describe fields, similar to dataclasses.field(). Defaults to ().
But dataclasses.field takes keyword-only arguments, which leaves it a bit ambiguous about whether positional arguments are ok. For example:
from typing import dataclass_transform, Any
def custom_field(default: object, *, init: bool = True) -> Any: ...
@dataclass_transform(field_specifiers=(custom_field,))
def build(x): ...
@build
class A:
x: int = custom_field(default=0)
@build
class B:
x: int = custom_field(0)
A()
B()
pyrefly, pyright, and mypy all flag B() as an error while ty accepts it without complaint.
My (uninformed) take is that this should be allowed -- custom_field(default=0) and custom_field(0) have the same runtime effect, so it feels like they should have the same type-checking effect too. This also matches how pydantic and attrs works (e.g. you can do pydantic.Field(1) and attr.ib(0)) -- today I suspect the type-checkers are special-casing this to make pydantic and attrs (since I've definitely used both without running into static type-checking errors about missing default values before).
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 đặc tả dataclass_transform và định nghĩa field_specifiers của nó; so sánh các ví dụ về đối số vị trí và đối số từ khóa trong pyrefly, pyright, mypy và ty. Xác định xem có nên cho phép các đối số vị trí hay không, sau đó ghi lại quyết định trong typing specification và thêm một ví dụ tương ứng hoặc một bài kiểm tra tính tuân thủ nếu dự án có.
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ó
- 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
- 35/100