prefect.utilities.pydantic
¶
PartialModel
¶
Bases: Generic[M]
A utility for creating a Pydantic model in several steps.
Fields may be set at initialization, via attribute assignment, or at finalization when the concrete model is returned.
Pydantic validation does not occur until finalization.
Each field can only be set once and a ValueError
will be raised on assignment if
a field already has a value.
Example
class MyModel(pydantic_v1.BaseModel): x: int y: str z: float
partial_model = PartialModel(MyModel, x=1) partial_model.y = "two" model = partial_model.finalize(z=3.0)
Source code in prefect/utilities/pydantic.py
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 |
|
add_cloudpickle_reduction
¶
add_cloudpickle_reduction
add_cloudpickle_reduction
Adds a __reducer__
to the given class that ensures it is cloudpickle compatible.
Workaround for issues with cloudpickle when using cythonized pydantic which throws exceptions when attempting to pickle the class which has "compiled" validator methods dynamically attached to it.
We cannot define this utility in the model class itself because the class is the type that contains unserializable methods.
Any model using some features of Pydantic (e.g. Path
validation) with a Cython
compiled Pydantic installation may encounter pickling issues.
See related issue at https://github.com/cloudpipe/cloudpickle/issues/408
Source code in prefect/utilities/pydantic.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 |
|
add_type_dispatch
¶
Extend a Pydantic model to add a 'type' field that is used as a discriminator field to dynamically determine the subtype that when deserializing models.
This allows automatic resolution to subtypes of the decorated model.
If a type field already exists, it should be a string literal field that has a constant value for each subclass. The default value of this field will be used as the dispatch key.
If a type field does not exist, one will be added. In this case, the value of the
field will be set to the value of the __dispatch_key__
. The base class should
define a __dispatch_key__
class method that is used to determine the unique key
for each subclass. Alternatively, each subclass can define the __dispatch_key__
as a string literal.
The base class must not define a 'type' field. If it is not desirable to add a field
to the model and the dispatch key can be tracked separately, the lower level
utilities in prefect.utilities.dispatch
should be used directly.
Source code in prefect/utilities/pydantic.py
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 |
|
get_class_fields_only
¶
Gets all the field names defined on the model class but not any parent classes. Any fields that are on the parent but redefined on the subclass are included.
Source code in prefect/utilities/pydantic.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
|