Merging/Modifying Types
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 have a decorator that modifies a class at runtime, kind of like a dataclass:
@modify
class Something:
...
Let's say modify adds foo onto Something, so after calling modify you could use it like so:
@modify
class Something
...
bar = Something.foo()
dataclass_transform doesn't support this kind of behavior at all. So, how would an API like this work?
I don't see why it would be difficult to implement some sort of Merge type, that takes in a type and "applies"" a Protocol to it. For the above example, it would work like this:
T = TypeVar("T")
class FooProtocol(Protocol):
@classmethod
def foo() -> None:
...
def modify(tp: type[T]) -> Merge[T, FooProtocol]:
tp.foo = ... # type: ignore
return tp
@modify
class Something:
...
bar = Something.foo()
In the above example, ideally it would just sort of mark that the T is now compatible with FooProtocol, but a type: ignore is used inside of modify because it would probably be a mess to try and come up with some sort of API for actually converting a T to Merge[T, ...].
TL;DR A type that marks another type as being compatible with a protocol could be handy for making dataclass-like decorators.
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 việc xem xét kiểu Merge được đề xuất và hạn chế của dataclass_transform được mô tả trong issue. Xác định xem đặc tả typing có vị trí thích hợp cho hành vi này hay không, cũng như cần định nghĩa những ngữ nghĩa nào về tính tương thích và decorator; issue không cung cấp tệp, bài kiểm thử hay điểm bắt đầu triển khai cụ thể nào.
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
- Cần làm rõ
- Mức phù hợp với người mới
- 20/100