Coverage for src / dynapydantic / union_mode.py: 100%
12 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-06-13 20:14 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-06-13 20:14 +0000
1"""Union mode configuration types for dynapydantic."""
3import enum
4import typing as ty
5from collections.abc import Callable
7import pydantic
10class DiscriminatedConfig(pydantic.BaseModel, frozen=True, extra="forbid"):
11 """Configuration for a discriminated union.
13 Carries the discriminator field name and optional value generator that
14 are required when pydantic's discriminated-union validation strategy is
15 used.
16 """
18 discriminator_field: str = pydantic.Field(
19 description="Name of the field used to discriminate between subtypes.",
20 )
21 discriminator_value_generator: Callable[[type], str] | None = pydantic.Field(
22 None,
23 description=(
24 "A callable that produces default values for the discriminator "
25 "field when none is supplied via register()."
26 ),
27 )
30#: Literal type for the two non-discriminated union strategies.
31NonDiscriminatedMode = ty.Literal["smart", "left_to_right"]
33#: Full union-mode type. Either a structured `DiscriminatedMode`
34#: (the default) or one of the plain string literals for the two
35#: non-discriminated strategies.
36UnionMode = DiscriminatedConfig | NonDiscriminatedMode
39class UnionRealization(enum.Enum):
40 """When unions should be realized
42 Attributes
43 ----------
44 MODEL_CONSTRUCTION
45 Unions are realized at model construction time, during generation of
46 the schema. This approach has the lowest runtime overhead, but is
47 somewhat sensitive to ordering and may require model rebuilding for
48 recursive model.
49 VALIDATION
50 Unions are realized at validation time. This approach is robust to order
51 of operation, but carries additional runtime overhead.
52 """
54 MODEL_CONSTRUCTION = "model-construction"
55 VALIDATION = "validation"