API Reference¶
dynapydantic - dynamic tracking of pydantic models
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 |
|
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.
Inheriting from this class will augment your class with the following members functions: 1. registered_subclasses() -> dict[str, type[cls]]: This will return a mapping of discriminator value to the corresponding sublcass. See TrackingGroup.models for details. 2. union() -> typing.GenericAlias: This will return an (optionally) annotated subclass union. See TrackingGroup.union() for details. 3. load_plugins() -> None: If plugin_entry_point was specified, then this method will load plugin packages to discover additional subclasses. See TrackingGroup.load_plugins for more details.
Source code in src/dynapydantic/subclass_tracking_model.py
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 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 |
|
__init_subclass__(*args, exclude_from_union=None, **kwargs)
¶
Subclass hook
Source code in src/dynapydantic/subclass_tracking_model.py
48 49 50 51 52 53 54 55 56 57 58 59 |
|
__pydantic_init_subclass__(*args, exclude_from_union=None, **kwargs)
classmethod
¶
Pydantic subclass hook
Source code in src/dynapydantic/subclass_tracking_model.py
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 |
|
TrackingGroup
pydantic-model
¶
Bases: BaseModel
Tracker for pydantic models
Fields:
-
name
(str
) -
discriminator_field
(str
) -
plugin_entry_point
(str | None
) -
discriminator_value_generator
(Callable[[type], str] | None
) -
models
(dict[str, type[BaseModel]]
)
Source code in src/dynapydantic/tracking_group.py
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 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 |
|
discriminator_field
pydantic-field
¶
Name of the discriminator field
discriminator_value_generator = None
pydantic-field
¶
A callable that produces default values for the discriminator field
models = {}
pydantic-field
¶
The tracked models
name
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.
plugin_entry_point = 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.
load_plugins()
¶
Load plugins to discover/register additional models
Source code in src/dynapydantic/tracking_group.py
72 73 74 75 76 77 78 79 80 81 82 |
|
register(discriminator_value=None)
¶
Register a model into this group (decorator)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
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
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 |
|
register_model(cls)
¶
Register the given model into this group
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls
|
type[BaseModel]
|
The model to register |
required |
Source code in src/dynapydantic/tracking_group.py
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 |
|
union(*, annotated=True)
¶
Return the union of all registered models
Source code in src/dynapydantic/tracking_group.py
190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
|