API Reference¶
dynapydantic
¶
dynapydantic - dynamic tracking of pydantic models
Polymorphic
¶
Annotation used to mark a type as having duck-typing behavior
This annotation is only valid for SubclassTrackingModel's.
Similar to SerializeAsAny, a field annotated with this shall serialize as according to its actual type, not the field annotation type. In addition, parsing will function as if the field annotation type were the union of all tracked subclasses.
If a UnionRealization (or the string value of one) is passed as the second argument, it will override the default value for the union realization that is stored in the class.
Source code in src/dynapydantic/annotations.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | |
__class_getitem__(item: type[ModelT] | tuple[type[ModelT], UnionRealization | str]) -> ty.Annotated[type[ModelT], ...]
¶
Get the annotation for the pydantic field
Source code in src/dynapydantic/annotations.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | |
Union
¶
Annotation used to get the union out of a dynapydantic entity
This annotation is primarily used for using the union of all models in
a TrackingGroup as a field annotation. It can be used with
SubclassTrackingModel, but in general, Polymorphic is preferable.
Source code in src/dynapydantic/annotations.py
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | |
__class_getitem__(item: TrackingGroup | type[ModelT]) -> object
¶
__class_getitem__(item: type[ModelT]) -> type[ModelT]
__class_getitem__(item: TrackingGroup) -> ty.Any
Return the union
Source code in src/dynapydantic/annotations.py
103 104 105 | |
AmbiguousDiscriminatorValueError
¶
Bases: Error
Occurs when the discriminator value is ambiguous
Source code in src/dynapydantic/exceptions.py
12 13 | |
ConfigurationError
¶
Bases: Error
Occurs when the user misconfigured a tracking setup
Source code in src/dynapydantic/exceptions.py
16 17 | |
Error
¶
Bases: Exception
Base class for all dynapydanitc errors
Source code in src/dynapydantic/exceptions.py
4 5 | |
NoRegisteredTypesError
¶
Bases: Error
Occurs when a union is requested from a tracking group with no members
Source code in src/dynapydantic/exceptions.py
20 21 | |
RegistrationError
¶
Bases: Error
Occurs when a model cannot be registered
Source code in src/dynapydantic/exceptions.py
8 9 | |
SubclassTrackingModel
pydantic-model
¶
Bases: BaseModel
Subclass-tracking BaseModel
This will inject a TrackingGroup into your
class and automate the registration of subclasses.
Similar to BaseModel, SubclassTrackingModel can take arguments in the
class declaration. Arguments from BaseModel will be forwarded.
Additionally, any fields from TrackingGroup will be forwarded to the
internal TrackingGroup instance. The following additional arguments are
supported:
exclude_from_union: This flag is intended to be used with descendents ofSubclassTrackingModel. IfTrue, this subclass will be omitted from tracking. The default for this flag isTruefor direct descendents ofSubclassTrackingModelandFalseotherwise.union_realization: When the union should be realized. SeeUnionRealizationfor more details on the various options. The default is to realize unions at model construction time.
Source code in src/dynapydantic/subclass_tracking_model.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | |
__init_subclass__(*args, **kwargs) -> None
¶
Subclass hook
Source code in src/dynapydantic/subclass_tracking_model.py
38 39 40 41 42 43 44 45 46 47 48 49 50 | |
__pydantic_init_subclass__(*args, exclude_from_union: bool | None = None, union_realization: str | UnionRealization | None = None, **kwargs) -> None
classmethod
¶
Pydantic subclass hook
Source code in src/dynapydantic/subclass_tracking_model.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | |
TrackingGroup
pydantic-model
¶
Bases: BaseModel
Tracker for pydantic models
Fields:
-
name(str) -
union_mode(UnionMode | None) -
discriminator_field(str | None) -
discriminator_value_generator(Callable[[type], str] | None) -
plugin_entry_point(str | None) -
models(dict[str, type[BaseModel]])
Validators:
-
_ensure_union_mode -
_coerce_union_mode
Source code in src/dynapydantic/tracking_group.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 | |
name: str
pydantic-field
¶
Name of the tracking group. This is for human display, so it doesn't technically need to be globally unique, but it should be meaningfully named, as it will be used in error messages.
union_mode: UnionMode | None = None
pydantic-field
¶
Union validation strategy. Pass a DiscriminatedConfig instance or one of the plain strings "smart" or "left_to_right". You can also just pass the fields for DiscriminatedConfig to this model and they will be forwarded.
discriminator_field: str | None = None
pydantic-field
¶
Name of the discriminator field. NOTE: This field is here as an alias for union_mode.discriminator_field. Passing both a discriminator_field and a union_mode will result in an error.
discriminator_value_generator: ty.Callable[[type], str] | None = None
pydantic-field
¶
A callable that produces default values for the discriminator field
plugin_entry_point: str | None = None
pydantic-field
¶
If given, then plugins packages will be supported through this Python entrypoint. The entrypoint can either be a function, which will be called, or simply a module, which will be imported. In either case, models found along the import path of the entrypoint will be registered. If the entrypoint is a function, additional models may be declared in the function.
models: dict[str, type[pydantic.BaseModel]] = {}
pydantic-field
¶
The tracked models
generation: int
property
¶
The generation of the tracking group.
This is a counter that increments every time a new registration occurs
type_adapter: pydantic.TypeAdapter
property
¶
Get the pydantic TypeAdapter for the union of all group members
load_plugins() -> None
¶
Load plugins to discover/register additional models
Source code in src/dynapydantic/tracking_group.py
200 201 202 203 204 205 206 207 208 209 210 | |
register(value: str | type[pydantic.BaseModel] | None = None) -> ty.Callable[[type], type] | type[pydantic.BaseModel]
¶
register(
value: str | None = None,
) -> ty.Callable[[type], type]
register(
value: type[pydantic.BaseModel],
) -> type[pydantic.BaseModel]
Register a model into this group (decorator)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str | type[BaseModel] | None
|
Value for the discriminator field. If not given, then discriminator_value_generator must be non-None or the discriminator field must be declared by hand. Can also be the type itself to register (if the ()'s are omitted from the decorator). |
None
|
Source code in src/dynapydantic/tracking_group.py
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | |
register_model(cls: type[pydantic.BaseModel], discriminator_value: str | None = None) -> None
¶
Register the given model into this group
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type[BaseModel]
|
The model to register |
required |
discriminator_value
|
str | None
|
Value for the discriminator field. If not given, then discriminator_value_generator must be non-None or the discriminator field must be declared by hand. |
None
|
Source code in src/dynapydantic/tracking_group.py
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 | |
union(*, plain: bool | None = None) -> ty.Any
¶
Return the union of all registered models
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
plain
|
bool | None
|
If set to |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
If there is 1 registered type, the type itself. If there is > 1, a
union of all registered types. This union may be annotated if
|
Raises:
| Type | Description |
|---|---|
NoRegisteredTypesError
|
If no types have been registered yet. |
Source code in src/dynapydantic/tracking_group.py
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 | |
DiscriminatedConfig
pydantic-model
¶
Bases: BaseModel
Configuration for a discriminated union.
Carries the discriminator field name and optional value generator that are required when pydantic's discriminated-union validation strategy is used.
Fields:
-
discriminator_field(str) -
discriminator_value_generator(Callable[[type], str] | None)
Source code in src/dynapydantic/union_mode.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | |
UnionRealization
¶
Bases: Enum
When unions should be realized
Attributes:
| Name | Type | Description |
|---|---|---|
MODEL_CONSTRUCTION |
Unions are realized at model construction time, during generation of the schema. This approach has the lowest runtime overhead, but is somewhat sensitive to ordering and may require model rebuilding for recursive model. |
|
VALIDATION |
Unions are realized at validation time. This approach is robust to order of operation, but carries additional runtime overhead. |
Source code in src/dynapydantic/union_mode.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | |
load_plugins(entity: TrackingGroup | type[SubclassTrackingModel]) -> None
¶
Load plugins to discover/register additional models
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity
|
TrackingGroup | type[SubclassTrackingModel]
|
The entity for which to load plugins |
required |
Source code in src/dynapydantic/free_funcs.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | |
registered_models(entity: TrackingGroup | type[ModelT]) -> dict[str, type[pydantic.BaseModel]] | dict[str, type[ModelT]]
¶
registered_models(
entity: TrackingGroup,
) -> dict[str, type[pydantic.BaseModel]]
registered_models(
entity: type[ModelT],
) -> dict[str, type[ModelT]]
Get the mapping of identifier -> model for all models tracked by the entity
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity
|
TrackingGroup | type[ModelT]
|
The entity for which to the registered models are desired |
required |
Returns:
| Type | Description |
|---|---|
dict[str, BaseModel]
|
A mapping of identifier to the registered model. This identifier will be the discriminator value for entities that produce discriminated unions. If the entity produces a non-discriminated union, the identifier will just be some unique string. |
Source code in src/dynapydantic/free_funcs.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | |
union(entity: TrackingGroup | type[SubclassTrackingModel], *, plain: bool | None = None) -> ty.Any
¶
Get the union of all tracked models in this entity
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity
|
TrackingGroup | type[SubclassTrackingModel]
|
The entity for which the union shall be computed. |
required |
plain
|
bool | None
|
If set to |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
If only 1 model is tracked, the model itself will be returned. If >1,
model is tracked a union of all models will be returned. This union may
be an |
See Also
Union[T] : Wrapper around this function for use
in type annotations.
Raises:
| Type | Description |
|---|---|
NoRegisteredTypesError
|
If no models are tracked by this entity. |
Source code in src/dynapydantic/free_funcs.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | |