Pydantic Dataclasses
Provide an enhanced dataclass that performs validation.
dataclass ¶
dataclass(
_cls=None,
*,
init=False,
repr=True,
eq=True,
order=False,
unsafe_hash=False,
frozen=False,
config=None,
validate_on_init=None,
kw_only=False,
slots=False
)
Usage Documentation
A decorator used to create a Pydantic-enhanced dataclass, similar to the standard Python dataclass
,
but with added validation.
This function should be used similarly to dataclasses.dataclass
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
_cls |
type[_T] | None
|
The target |
None
|
init |
Literal[False]
|
Included for signature compatibility with |
False
|
repr |
bool
|
A boolean indicating whether or not to include the field in the |
True
|
eq |
bool
|
Determines if a |
True
|
order |
bool
|
Determines if comparison magic methods should be generated, such as |
False
|
unsafe_hash |
bool
|
Determines if an unsafe hashing function should be included in the class. |
False
|
frozen |
bool
|
Determines if the generated class should be a 'frozen' |
False
|
config |
ConfigDict | type[object] | None
|
A configuration for the |
None
|
validate_on_init |
bool | None
|
A deprecated parameter included for backwards compatibility; in V2, all Pydantic dataclasses are validated on init. |
None
|
kw_only |
bool
|
Determines if |
False
|
slots |
bool
|
Determines if the generated class should be a 'slots' |
False
|
Returns:
Type | Description |
---|---|
Callable[[type[_T]], type[PydanticDataclass]] | type[PydanticDataclass]
|
A decorator that accepts a class as its argument and returns a Pydantic |
Raises:
Type | Description |
---|---|
AssertionError
|
Raised if |
Source code in pydantic/dataclasses.py
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 |
|
rebuild_dataclass ¶
rebuild_dataclass(
cls,
*,
force=False,
raise_errors=True,
_parent_namespace_depth=2,
_types_namespace=None
)
Try to rebuild the pydantic-core schema for the dataclass.
This may be necessary when one of the annotations is a ForwardRef which could not be resolved during the initial attempt to build the schema, and automatic rebuilding fails.
This is analogous to BaseModel.model_rebuild
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls |
type[PydanticDataclass]
|
The class to build the dataclass core schema for. |
required |
force |
bool
|
Whether to force the rebuilding of the model schema, defaults to |
False
|
raise_errors |
bool
|
Whether to raise errors, defaults to |
True
|
_parent_namespace_depth |
int
|
The depth level of the parent namespace, defaults to 2. |
2
|
_types_namespace |
dict[str, Any] | None
|
The types namespace, defaults to |
None
|
Returns:
Type | Description |
---|---|
bool | None
|
Returns |
bool | None
|
If rebuilding was required, returns |
Source code in pydantic/dataclasses.py
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 |
|
is_pydantic_dataclass ¶
is_pydantic_dataclass(__cls)
Whether a class is a pydantic dataclass.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
__cls |
type[Any]
|
The class. |
required |
Returns:
Type | Description |
---|---|
TypeGuard[type[PydanticDataclass]]
|
|
Source code in pydantic/dataclasses.py
303 304 305 306 307 308 309 310 311 312 |
|