Evolving a Struct into a Union
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 35/100
- Loại issue
- Tính năng
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Ít trao đổi
- Công nghệ
- python
- Lĩnh vực
- backend, backend-api-design
Hướng nghiên cứu
Bắt đầu với việc xử lý tag của msgspec.Struct và đường dẫn union-dispatch của msgspec.json.decode được mô tả trong issue, bao gồm cả ghi chú rằng dec_hook không được gọi. Xem lại phần thảo luận về việc tiến hóa schema được liên kết từ issue và #720 trước khi chọn một hướng tiếp cận. Được xem là hoàn tất khi một Request legacy không có tag được giải mã thành RequestV0, trong khi các thành viên union có tag tiếp tục được giải mã chính xác và khả năng tương thích với array_like đã được xử lý.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Description
I'd like to evolve a Struct to a union of Structs without breaking users of the original Struct. As far as I can tell, this is not supported by the existing schema evolution mechanism. I propose to do this by allowing Structs to define tag=msgspec.UNSET.
Example
Let's say I have a service that accepts a Request of the following shape:
class Request(msgspec.Struct):
a: int
# server decoding logic
req = msgspec.json.decode(buf, type=Request)
Now I want to allow this service to accept a union of two different request types, so I modify my service to look like:
class RequestV0(msgspec.Struct, tag=True):
a: int
class RequestV1(msgspec.Struct, tag=True):
b: str
c: str
# server decoding logic
req = msgspec.json.decode(buf, type=RequestV0 | RequestV1)
What I would like is the ability to decode the old Request as the new RequestV0. It's possible to discriminate correctly, because all of the old Request messages will have an unset tag field while all of the new union members will have the tag field set.
buf = msgspec.json.encode(Request(a=4))
req_v0 = msgspec.json.decode(buf, type=RequestV0 | RequestV1)
# raises msgspec.ValidationError: Object missing required field `type`
Existing solutions
One way to avoid this situation today is to defensively set tag=True on all Requests, so that they can be evolved in the future. That's fine in this small example, but if you have something like a long list of Structs, then this will needlessly inflate the wire size.
As far as I can tell dec_hook is not called in this situation.
Proposal
My proposed feature is to allow tag=msgspec.UNSET. The new server logic looks like:
class RequestV0(msgspec.Struct, tag=msgspec.UNSET): # today, raises TypeError: `tag` must be a `str` or an `int`
a: int
class RequestV1(msgspec.Struct, tag=True):
b: str
c: str
# server decoding logic
req = msgspec.json.decode(buf, type=RequestV0 | RequestV1)
Now the server's decoding logic, upon receiving JSON without the type field set, knows to decode it as a RequestV0.
buf = msgspec.json.encode(Request(a=4))
req_v0 = msgspec.json.decode(buf, type=RequestV0 | RequestV1)
# successfully returns a RequestV0
Given the discussion on #720, I would propose to make tag=msgspec.UNSET exclusive with array_like=True. Depending on the approach chosen there, it may be possible to support both options by dispatching on the length of the array.
- Ngôn ngữ chính
- Python
- Star
- 4.2k
- Fork
- 209
- Merge trung bình
- 2 ngày 3 giờ
- Pull request đã merge (30 ngày)
- 51
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
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.
Issue khác của msgspec/msgspec
-
msgpack can't encode a frozendict dict key that it decodes (Python 3.15)Có thể đã có người làm @digit50 đã nhận 3 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 80/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Segfault reading decimal_format after a failed Encoder.__init__ re-callCó thể đã có người làm @digit50 đã nhận 3 ngày trước. Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 62/100
msgspec/msgspec#1279 · 4 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Segfault decoding a TypedDict that references a recursive TypedDict which failed to buildCó thể đã có người làm @Tiancheng-Xu đã nhận 6 ngày trước. Đang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 48/100
msgspec/msgspec#1251 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Fix Free-Threading supportCó thể đã có người làm Có pull request liên kết đang mở hoặc đã được merge. Đang mởenhancement
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 25/100
msgspec/msgspec#1249 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
convert: mapping fields named like mapping methods (keys, get, ...) resolve to the bound methodCó thể đã có người làm @MbappeWU đã nhận 5 ngày trước. Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của msgspec/msgspec
Issue tương tự
-
bug Size: XS
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
oposs/repo-infra#93 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 69/100
Graphify-Labs/graphify#4546 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 65/100
521xueweihan/HelloGitHub#4025 ·