Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Evolving a Struct into a Union

Đang mở
#968 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

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

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

Mở trong Codespaces

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.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của msgspec/msgspec

Tất cả issue của msgspec/msgspec

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.