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

Support subset of tagged union strategy

Đang mở
#348 0 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ó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
45/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
python
Lĩnh vực
backend

Hướng nghiên cứu

Start at the configure_tagged_union entry point and reproduce the reported Foo and Bar examples with cattrs 23.1.0.dev0. Trace how configured tagged unions are matched and add coverage for a subset such as A | B; done means Bar(A(1)) uses the tagged-union strategy without requiring every combination to be configured.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

  • cattrs version: 23.1.0.dev0
  • Python version: 3.11.1
  • Operating System: Manjaro KDE
Description

Subsets of tagged unions should also be able to use the configured strategy.

What I Did
import attrs
import cattrs
from cattrs.strategies import configure_tagged_union


@attrs.define
class A:
    x: int


@attrs.define
class B:
    x: str


@attrs.define
class C:
    x: list[float]


@attrs.define
class Foo:
    y: A | B | C


@attrs.define
class Bar:
    y: A | B


c = cattrs.Converter()
configure_tagged_union(A | B | C, converter=c)
c.unstructure(Foo(A(1)))  # {'y': {'x': 1, '_type': 'A'}}
c.unstructure(Bar(A(1)))  # {'y': {'x': 1}}

Because Bar uses only A | B, and I've only configured A | B | C, it doesn't use the strategy.
I think it should be possible to use the strategy for A | B | C when I only have A | B, a subset of it. If not by default, this should be a configurable option, like configure_tagged_union(A | B | C, converter=c, allow_subset=True).

Otherwise, I'd need to repeatedly define all possible combinations of A, B, and C, like:

configure_tagged_union(A | B, converter=c)
configure_tagged_union(A | C, converter=c)
configure_tagged_union(B | C, converter=c)
configure_tagged_union(A | B | C, converter=c)
Ngôn ngữ chính
Python
Star
1.1k
Fork
168
Merge trung bình
2 ngày 13 giờ
Pull request đã merge (30 ngày)
7

Chuẩn bị môi trường

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 python-attrs/cattrs

Tất cả issue của python-attrs/cattrs

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.