pyprig 1.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of pyprig might be problematic. Click here for more details.

Files changed (112) hide show
  1. pyprig/__init__.py +464 -0
  2. pyprig/_internal/__init__.py +0 -0
  3. pyprig/_internal/_config.py +408 -0
  4. pyprig/_internal/_core_metadata.py +97 -0
  5. pyprig/_internal/_core_utils.py +156 -0
  6. pyprig/_internal/_dataclasses.py +327 -0
  7. pyprig/_internal/_decorators.py +906 -0
  8. pyprig/_internal/_decorators_v1.py +174 -0
  9. pyprig/_internal/_discriminated_union.py +510 -0
  10. pyprig/_internal/_docs_extraction.py +113 -0
  11. pyprig/_internal/_fields.py +883 -0
  12. pyprig/_internal/_forward_ref.py +23 -0
  13. pyprig/_internal/_generate_schema.py +2980 -0
  14. pyprig/_internal/_generics.py +553 -0
  15. pyprig/_internal/_git.py +27 -0
  16. pyprig/_internal/_import_utils.py +20 -0
  17. pyprig/_internal/_known_annotated_metadata.py +405 -0
  18. pyprig/_internal/_mock_val_ser.py +237 -0
  19. pyprig/_internal/_model_construction.py +833 -0
  20. pyprig/_internal/_namespace_utils.py +299 -0
  21. pyprig/_internal/_repr.py +122 -0
  22. pyprig/_internal/_schema_gather.py +49 -0
  23. pyprig/_internal/_schema_generation_shared.py +126 -0
  24. pyprig/_internal/_serializers.py +53 -0
  25. pyprig/_internal/_signature.py +193 -0
  26. pyprig/_internal/_type_refs.py +128 -0
  27. pyprig/_internal/_typing_extra.py +502 -0
  28. pyprig/_internal/_utils.py +433 -0
  29. pyprig/_internal/_validate_call.py +141 -0
  30. pyprig/_internal/_validators.py +541 -0
  31. pyprig/_migration.py +317 -0
  32. pyprig/alias_generators.py +62 -0
  33. pyprig/aliases.py +136 -0
  34. pyprig/annotated_handlers.py +123 -0
  35. pyprig/class_validators.py +5 -0
  36. pyprig/color.py +605 -0
  37. pyprig/config.py +1295 -0
  38. pyprig/dataclasses.py +385 -0
  39. pyprig/datetime_parse.py +5 -0
  40. pyprig/decorator.py +5 -0
  41. pyprig/deprecated/__init__.py +0 -0
  42. pyprig/deprecated/class_validators.py +257 -0
  43. pyprig/deprecated/config.py +72 -0
  44. pyprig/deprecated/copy_internals.py +224 -0
  45. pyprig/deprecated/decorator.py +286 -0
  46. pyprig/deprecated/json.py +142 -0
  47. pyprig/deprecated/parse.py +81 -0
  48. pyprig/deprecated/tools.py +104 -0
  49. pyprig/env_settings.py +5 -0
  50. pyprig/error_wrappers.py +5 -0
  51. pyprig/errors.py +185 -0
  52. pyprig/experimental/__init__.py +1 -0
  53. pyprig/experimental/arguments_schema.py +44 -0
  54. pyprig/experimental/missing_sentinel.py +29 -0
  55. pyprig/experimental/pipeline.py +684 -0
  56. pyprig/fields.py +2065 -0
  57. pyprig/functional_serializers.py +461 -0
  58. pyprig/functional_validators.py +920 -0
  59. pyprig/generics.py +5 -0
  60. pyprig/help.py +68 -0
  61. pyprig/json.py +5 -0
  62. pyprig/json_schema.py +3253 -0
  63. pyprig/main.py +1942 -0
  64. pyprig/mypy.py +1465 -0
  65. pyprig/networks.py +1417 -0
  66. pyprig/parse.py +5 -0
  67. pyprig/plugin/__init__.py +190 -0
  68. pyprig/plugin/_loader.py +58 -0
  69. pyprig/plugin/_schema_validator.py +152 -0
  70. pyprig/py.typed +0 -0
  71. pyprig/root_model.py +186 -0
  72. pyprig/schema.py +5 -0
  73. pyprig/tools.py +5 -0
  74. pyprig/type_adapter.py +776 -0
  75. pyprig/types.py +3405 -0
  76. pyprig/typing.py +5 -0
  77. pyprig/utils.py +5 -0
  78. pyprig/v1/__init__.py +131 -0
  79. pyprig/v1/_hypothesis_plugin.py +391 -0
  80. pyprig/v1/annotated_types.py +72 -0
  81. pyprig/v1/class_validators.py +361 -0
  82. pyprig/v1/color.py +494 -0
  83. pyprig/v1/config.py +191 -0
  84. pyprig/v1/dataclasses.py +500 -0
  85. pyprig/v1/datetime_parse.py +248 -0
  86. pyprig/v1/decorator.py +264 -0
  87. pyprig/v1/env_settings.py +350 -0
  88. pyprig/v1/error_wrappers.py +161 -0
  89. pyprig/v1/errors.py +646 -0
  90. pyprig/v1/fields.py +1253 -0
  91. pyprig/v1/generics.py +400 -0
  92. pyprig/v1/json.py +112 -0
  93. pyprig/v1/main.py +1130 -0
  94. pyprig/v1/mypy.py +949 -0
  95. pyprig/v1/networks.py +747 -0
  96. pyprig/v1/parse.py +66 -0
  97. pyprig/v1/py.typed +0 -0
  98. pyprig/v1/schema.py +1163 -0
  99. pyprig/v1/tools.py +92 -0
  100. pyprig/v1/types.py +1205 -0
  101. pyprig/v1/typing.py +627 -0
  102. pyprig/v1/utils.py +807 -0
  103. pyprig/v1/validators.py +768 -0
  104. pyprig/v1/version.py +38 -0
  105. pyprig/validate_call_decorator.py +121 -0
  106. pyprig/validators.py +5 -0
  107. pyprig/version.py +118 -0
  108. pyprig/warnings.py +130 -0
  109. pyprig-1.2.0.dist-info/METADATA +111 -0
  110. pyprig-1.2.0.dist-info/RECORD +112 -0
  111. pyprig-1.2.0.dist-info/WHEEL +4 -0
  112. pyprig-1.2.0.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,500 @@
1
+ """
2
+ The main purpose is to enhance stdlib dataclasses by adding validation
3
+ A pydantic dataclass can be generated from scratch or from a stdlib one.
4
+
5
+ Behind the scene, a pydantic dataclass is just like a regular one on which we attach
6
+ a `BaseModel` and magic methods to trigger the validation of the data.
7
+ `__init__` and `__post_init__` are hence overridden and have extra logic to be
8
+ able to validate input data.
9
+
10
+ When a pydantic dataclass is generated from scratch, it's just a plain dataclass
11
+ with validation triggered at initialization
12
+
13
+ The tricky part if for stdlib dataclasses that are converted after into pydantic ones e.g.
14
+
15
+ ```py
16
+ @dataclasses.dataclass
17
+ class M:
18
+ x: int
19
+
20
+ ValidatedM = pydantic.dataclasses.dataclass(M)
21
+ ```
22
+
23
+ We indeed still want to support equality, hashing, repr, ... as if it was the stdlib one!
24
+
25
+ ```py
26
+ assert isinstance(ValidatedM(x=1), M)
27
+ assert ValidatedM(x=1) == M(x=1)
28
+ ```
29
+
30
+ This means we **don't want to create a new dataclass that inherits from it**
31
+ The trick is to create a wrapper around `M` that will act as a proxy to trigger
32
+ validation without altering default `M` behaviour.
33
+ """
34
+ import copy
35
+ import dataclasses
36
+ import sys
37
+ from contextlib import contextmanager
38
+ from functools import wraps
39
+
40
+ try:
41
+ from functools import cached_property
42
+ except ImportError:
43
+ # cached_property available only for python3.8+
44
+ pass
45
+
46
+ from typing import TYPE_CHECKING, Any, Callable, ClassVar, Dict, Generator, Optional, Type, TypeVar, Union, overload
47
+
48
+ from typing_extensions import dataclass_transform
49
+
50
+ from pyprig.v1.class_validators import gather_all_validators
51
+ from pyprig.v1.config import BaseConfig, ConfigDict, Extra, get_config
52
+ from pyprig.v1.error_wrappers import ValidationError
53
+ from pyprig.v1.errors import DataclassTypeError
54
+ from pyprig.v1.fields import Field, FieldInfo, Required, Undefined
55
+ from pyprig.v1.main import create_model, validate_model
56
+ from pyprig.v1.utils import ClassAttribute
57
+
58
+ if TYPE_CHECKING:
59
+ from pyprig.v1.main import BaseModel
60
+ from pyprig.v1.typing import CallableGenerator, NoArgAnyCallable
61
+
62
+ DataclassT = TypeVar('DataclassT', bound='Dataclass')
63
+
64
+ DataclassClassOrWrapper = Union[Type['Dataclass'], 'DataclassProxy']
65
+
66
+ class Dataclass:
67
+ # stdlib attributes
68
+ __dataclass_fields__: ClassVar[Dict[str, Any]]
69
+ __dataclass_params__: ClassVar[Any] # in reality `dataclasses._DataclassParams`
70
+ __post_init__: ClassVar[Callable[..., None]]
71
+
72
+ # Added by pydantic
73
+ __pydantic_run_validation__: ClassVar[bool]
74
+ __post_init_post_parse__: ClassVar[Callable[..., None]]
75
+ __pydantic_initialised__: ClassVar[bool]
76
+ __pydantic_model__: ClassVar[Type[BaseModel]]
77
+ __pydantic_validate_values__: ClassVar[Callable[['Dataclass'], None]]
78
+ __pydantic_has_field_info_default__: ClassVar[bool] # whether a `pydantic.Field` is used as default value
79
+
80
+ def __init__(self, *args: object, **kwargs: object) -> None:
81
+ pass
82
+
83
+ @classmethod
84
+ def __get_validators__(cls: Type['Dataclass']) -> 'CallableGenerator':
85
+ pass
86
+
87
+ @classmethod
88
+ def __validate__(cls: Type['DataclassT'], v: Any) -> 'DataclassT':
89
+ pass
90
+
91
+
92
+ __all__ = [
93
+ 'dataclass',
94
+ 'set_validation',
95
+ 'create_pydantic_model_from_dataclass',
96
+ 'is_builtin_dataclass',
97
+ 'make_dataclass_validator',
98
+ ]
99
+
100
+ _T = TypeVar('_T')
101
+
102
+ if sys.version_info >= (3, 10):
103
+
104
+ @dataclass_transform(field_specifiers=(dataclasses.field, Field))
105
+ @overload
106
+ def dataclass(
107
+ *,
108
+ init: bool = True,
109
+ repr: bool = True,
110
+ eq: bool = True,
111
+ order: bool = False,
112
+ unsafe_hash: bool = False,
113
+ frozen: bool = False,
114
+ config: Union[ConfigDict, Type[object], None] = None,
115
+ validate_on_init: Optional[bool] = None,
116
+ use_proxy: Optional[bool] = None,
117
+ kw_only: bool = ...,
118
+ ) -> Callable[[Type[_T]], 'DataclassClassOrWrapper']:
119
+ ...
120
+
121
+ @dataclass_transform(field_specifiers=(dataclasses.field, Field))
122
+ @overload
123
+ def dataclass(
124
+ _cls: Type[_T],
125
+ *,
126
+ init: bool = True,
127
+ repr: bool = True,
128
+ eq: bool = True,
129
+ order: bool = False,
130
+ unsafe_hash: bool = False,
131
+ frozen: bool = False,
132
+ config: Union[ConfigDict, Type[object], None] = None,
133
+ validate_on_init: Optional[bool] = None,
134
+ use_proxy: Optional[bool] = None,
135
+ kw_only: bool = ...,
136
+ ) -> 'DataclassClassOrWrapper':
137
+ ...
138
+
139
+ else:
140
+
141
+ @dataclass_transform(field_specifiers=(dataclasses.field, Field))
142
+ @overload
143
+ def dataclass(
144
+ *,
145
+ init: bool = True,
146
+ repr: bool = True,
147
+ eq: bool = True,
148
+ order: bool = False,
149
+ unsafe_hash: bool = False,
150
+ frozen: bool = False,
151
+ config: Union[ConfigDict, Type[object], None] = None,
152
+ validate_on_init: Optional[bool] = None,
153
+ use_proxy: Optional[bool] = None,
154
+ ) -> Callable[[Type[_T]], 'DataclassClassOrWrapper']:
155
+ ...
156
+
157
+ @dataclass_transform(field_specifiers=(dataclasses.field, Field))
158
+ @overload
159
+ def dataclass(
160
+ _cls: Type[_T],
161
+ *,
162
+ init: bool = True,
163
+ repr: bool = True,
164
+ eq: bool = True,
165
+ order: bool = False,
166
+ unsafe_hash: bool = False,
167
+ frozen: bool = False,
168
+ config: Union[ConfigDict, Type[object], None] = None,
169
+ validate_on_init: Optional[bool] = None,
170
+ use_proxy: Optional[bool] = None,
171
+ ) -> 'DataclassClassOrWrapper':
172
+ ...
173
+
174
+
175
+ @dataclass_transform(field_specifiers=(dataclasses.field, Field))
176
+ def dataclass(
177
+ _cls: Optional[Type[_T]] = None,
178
+ *,
179
+ init: bool = True,
180
+ repr: bool = True,
181
+ eq: bool = True,
182
+ order: bool = False,
183
+ unsafe_hash: bool = False,
184
+ frozen: bool = False,
185
+ config: Union[ConfigDict, Type[object], None] = None,
186
+ validate_on_init: Optional[bool] = None,
187
+ use_proxy: Optional[bool] = None,
188
+ kw_only: bool = False,
189
+ ) -> Union[Callable[[Type[_T]], 'DataclassClassOrWrapper'], 'DataclassClassOrWrapper']:
190
+ """
191
+ Like the python standard lib dataclasses but with type validation.
192
+ The result is either a pydantic dataclass that will validate input data
193
+ or a wrapper that will trigger validation around a stdlib dataclass
194
+ to avoid modifying it directly
195
+ """
196
+ the_config = get_config(config)
197
+
198
+ def wrap(cls: Type[Any]) -> 'DataclassClassOrWrapper':
199
+ should_use_proxy = (
200
+ use_proxy
201
+ if use_proxy is not None
202
+ else (
203
+ is_builtin_dataclass(cls)
204
+ and (cls.__bases__[0] is object or set(dir(cls)) == set(dir(cls.__bases__[0])))
205
+ )
206
+ )
207
+ if should_use_proxy:
208
+ dc_cls_doc = ''
209
+ dc_cls = DataclassProxy(cls)
210
+ default_validate_on_init = False
211
+ else:
212
+ dc_cls_doc = cls.__doc__ or '' # needs to be done before generating dataclass
213
+ if sys.version_info >= (3, 10):
214
+ dc_cls = dataclasses.dataclass(
215
+ cls,
216
+ init=init,
217
+ repr=repr,
218
+ eq=eq,
219
+ order=order,
220
+ unsafe_hash=unsafe_hash,
221
+ frozen=frozen,
222
+ kw_only=kw_only,
223
+ )
224
+ else:
225
+ dc_cls = dataclasses.dataclass( # type: ignore
226
+ cls, init=init, repr=repr, eq=eq, order=order, unsafe_hash=unsafe_hash, frozen=frozen
227
+ )
228
+ default_validate_on_init = True
229
+
230
+ should_validate_on_init = default_validate_on_init if validate_on_init is None else validate_on_init
231
+ _add_pydantic_validation_attributes(cls, the_config, should_validate_on_init, dc_cls_doc)
232
+ dc_cls.__pydantic_model__.__try_update_forward_refs__(**{cls.__name__: cls})
233
+ return dc_cls
234
+
235
+ if _cls is None:
236
+ return wrap
237
+
238
+ return wrap(_cls)
239
+
240
+
241
+ @contextmanager
242
+ def set_validation(cls: Type['DataclassT'], value: bool) -> Generator[Type['DataclassT'], None, None]:
243
+ original_run_validation = cls.__pydantic_run_validation__
244
+ try:
245
+ cls.__pydantic_run_validation__ = value
246
+ yield cls
247
+ finally:
248
+ cls.__pydantic_run_validation__ = original_run_validation
249
+
250
+
251
+ class DataclassProxy:
252
+ __slots__ = '__dataclass__'
253
+
254
+ def __init__(self, dc_cls: Type['Dataclass']) -> None:
255
+ object.__setattr__(self, '__dataclass__', dc_cls)
256
+
257
+ def __call__(self, *args: Any, **kwargs: Any) -> Any:
258
+ with set_validation(self.__dataclass__, True):
259
+ return self.__dataclass__(*args, **kwargs)
260
+
261
+ def __getattr__(self, name: str) -> Any:
262
+ return getattr(self.__dataclass__, name)
263
+
264
+ def __setattr__(self, __name: str, __value: Any) -> None:
265
+ return setattr(self.__dataclass__, __name, __value)
266
+
267
+ def __instancecheck__(self, instance: Any) -> bool:
268
+ return isinstance(instance, self.__dataclass__)
269
+
270
+ def __copy__(self) -> 'DataclassProxy':
271
+ return DataclassProxy(copy.copy(self.__dataclass__))
272
+
273
+ def __deepcopy__(self, memo: Any) -> 'DataclassProxy':
274
+ return DataclassProxy(copy.deepcopy(self.__dataclass__, memo))
275
+
276
+
277
+ def _add_pydantic_validation_attributes( # noqa: C901 (ignore complexity)
278
+ dc_cls: Type['Dataclass'],
279
+ config: Type[BaseConfig],
280
+ validate_on_init: bool,
281
+ dc_cls_doc: str,
282
+ ) -> None:
283
+ """
284
+ We need to replace the right method. If no `__post_init__` has been set in the stdlib dataclass
285
+ it won't even exist (code is generated on the fly by `dataclasses`)
286
+ By default, we run validation after `__init__` or `__post_init__` if defined
287
+ """
288
+ init = dc_cls.__init__
289
+
290
+ @wraps(init)
291
+ def handle_extra_init(self: 'Dataclass', *args: Any, **kwargs: Any) -> None:
292
+ if config.extra == Extra.ignore:
293
+ init(self, *args, **{k: v for k, v in kwargs.items() if k in self.__dataclass_fields__})
294
+
295
+ elif config.extra == Extra.allow:
296
+ for k, v in kwargs.items():
297
+ self.__dict__.setdefault(k, v)
298
+ init(self, *args, **{k: v for k, v in kwargs.items() if k in self.__dataclass_fields__})
299
+
300
+ else:
301
+ init(self, *args, **kwargs)
302
+
303
+ if hasattr(dc_cls, '__post_init__'):
304
+ try:
305
+ post_init = dc_cls.__post_init__.__wrapped__ # type: ignore[attr-defined]
306
+ except AttributeError:
307
+ post_init = dc_cls.__post_init__
308
+
309
+ @wraps(post_init)
310
+ def new_post_init(self: 'Dataclass', *args: Any, **kwargs: Any) -> None:
311
+ if config.post_init_call == 'before_validation':
312
+ post_init(self, *args, **kwargs)
313
+
314
+ if self.__class__.__pydantic_run_validation__:
315
+ self.__pydantic_validate_values__()
316
+ if hasattr(self, '__post_init_post_parse__'):
317
+ self.__post_init_post_parse__(*args, **kwargs)
318
+
319
+ if config.post_init_call == 'after_validation':
320
+ post_init(self, *args, **kwargs)
321
+
322
+ setattr(dc_cls, '__init__', handle_extra_init)
323
+ setattr(dc_cls, '__post_init__', new_post_init)
324
+
325
+ else:
326
+
327
+ @wraps(init)
328
+ def new_init(self: 'Dataclass', *args: Any, **kwargs: Any) -> None:
329
+ handle_extra_init(self, *args, **kwargs)
330
+
331
+ if self.__class__.__pydantic_run_validation__:
332
+ self.__pydantic_validate_values__()
333
+
334
+ if hasattr(self, '__post_init_post_parse__'):
335
+ # We need to find again the initvars. To do that we use `__dataclass_fields__` instead of
336
+ # public method `dataclasses.fields`
337
+
338
+ # get all initvars and their default values
339
+ initvars_and_values: Dict[str, Any] = {}
340
+ for i, f in enumerate(self.__class__.__dataclass_fields__.values()):
341
+ if f._field_type is dataclasses._FIELD_INITVAR: # type: ignore[attr-defined]
342
+ try:
343
+ # set arg value by default
344
+ initvars_and_values[f.name] = args[i]
345
+ except IndexError:
346
+ initvars_and_values[f.name] = kwargs.get(f.name, f.default)
347
+
348
+ self.__post_init_post_parse__(**initvars_and_values)
349
+
350
+ setattr(dc_cls, '__init__', new_init)
351
+
352
+ setattr(dc_cls, '__pydantic_run_validation__', ClassAttribute('__pydantic_run_validation__', validate_on_init))
353
+ setattr(dc_cls, '__pydantic_initialised__', False)
354
+ setattr(dc_cls, '__pydantic_model__', create_pydantic_model_from_dataclass(dc_cls, config, dc_cls_doc))
355
+ setattr(dc_cls, '__pydantic_validate_values__', _dataclass_validate_values)
356
+ setattr(dc_cls, '__validate__', classmethod(_validate_dataclass))
357
+ setattr(dc_cls, '__get_validators__', classmethod(_get_validators))
358
+
359
+ if dc_cls.__pydantic_model__.__config__.validate_assignment and not dc_cls.__dataclass_params__.frozen:
360
+ setattr(dc_cls, '__setattr__', _dataclass_validate_assignment_setattr)
361
+
362
+
363
+ def _get_validators(cls: 'DataclassClassOrWrapper') -> 'CallableGenerator':
364
+ yield cls.__validate__
365
+
366
+
367
+ def _validate_dataclass(cls: Type['DataclassT'], v: Any) -> 'DataclassT':
368
+ with set_validation(cls, True):
369
+ if isinstance(v, cls):
370
+ v.__pydantic_validate_values__()
371
+ return v
372
+ elif isinstance(v, (list, tuple)):
373
+ return cls(*v)
374
+ elif isinstance(v, dict):
375
+ return cls(**v)
376
+ else:
377
+ raise DataclassTypeError(class_name=cls.__name__)
378
+
379
+
380
+ def create_pydantic_model_from_dataclass(
381
+ dc_cls: Type['Dataclass'],
382
+ config: Type[Any] = BaseConfig,
383
+ dc_cls_doc: Optional[str] = None,
384
+ ) -> Type['BaseModel']:
385
+ field_definitions: Dict[str, Any] = {}
386
+ for field in dataclasses.fields(dc_cls):
387
+ default: Any = Undefined
388
+ default_factory: Optional['NoArgAnyCallable'] = None
389
+ field_info: FieldInfo
390
+
391
+ if field.default is not dataclasses.MISSING:
392
+ default = field.default
393
+ elif field.default_factory is not dataclasses.MISSING:
394
+ default_factory = field.default_factory
395
+ else:
396
+ default = Required
397
+
398
+ if isinstance(default, FieldInfo):
399
+ field_info = default
400
+ dc_cls.__pydantic_has_field_info_default__ = True
401
+ else:
402
+ field_info = Field(default=default, default_factory=default_factory, **field.metadata)
403
+
404
+ field_definitions[field.name] = (field.type, field_info)
405
+
406
+ validators = gather_all_validators(dc_cls)
407
+ model: Type['BaseModel'] = create_model(
408
+ dc_cls.__name__,
409
+ __config__=config,
410
+ __module__=dc_cls.__module__,
411
+ __validators__=validators,
412
+ __cls_kwargs__={'__resolve_forward_refs__': False},
413
+ **field_definitions,
414
+ )
415
+ model.__doc__ = dc_cls_doc if dc_cls_doc is not None else dc_cls.__doc__ or ''
416
+ return model
417
+
418
+
419
+ if sys.version_info >= (3, 8):
420
+
421
+ def _is_field_cached_property(obj: 'Dataclass', k: str) -> bool:
422
+ return isinstance(getattr(type(obj), k, None), cached_property)
423
+
424
+ else:
425
+
426
+ def _is_field_cached_property(obj: 'Dataclass', k: str) -> bool:
427
+ return False
428
+
429
+
430
+ def _dataclass_validate_values(self: 'Dataclass') -> None:
431
+ # validation errors can occur if this function is called twice on an already initialised dataclass.
432
+ # for example if Extra.forbid is enabled, it would consider __pydantic_initialised__ an invalid extra property
433
+ if getattr(self, '__pydantic_initialised__'):
434
+ return
435
+ if getattr(self, '__pydantic_has_field_info_default__', False):
436
+ # We need to remove `FieldInfo` values since they are not valid as input
437
+ # It's ok to do that because they are obviously the default values!
438
+ input_data = {
439
+ k: v
440
+ for k, v in self.__dict__.items()
441
+ if not (isinstance(v, FieldInfo) or _is_field_cached_property(self, k))
442
+ }
443
+ else:
444
+ input_data = {k: v for k, v in self.__dict__.items() if not _is_field_cached_property(self, k)}
445
+ d, _, validation_error = validate_model(self.__pydantic_model__, input_data, cls=self.__class__)
446
+ if validation_error:
447
+ raise validation_error
448
+ self.__dict__.update(d)
449
+ object.__setattr__(self, '__pydantic_initialised__', True)
450
+
451
+
452
+ def _dataclass_validate_assignment_setattr(self: 'Dataclass', name: str, value: Any) -> None:
453
+ if self.__pydantic_initialised__:
454
+ d = dict(self.__dict__)
455
+ d.pop(name, None)
456
+ known_field = self.__pydantic_model__.__fields__.get(name, None)
457
+ if known_field:
458
+ value, error_ = known_field.validate(value, d, loc=name, cls=self.__class__)
459
+ if error_:
460
+ raise ValidationError([error_], self.__class__)
461
+
462
+ object.__setattr__(self, name, value)
463
+
464
+
465
+ def is_builtin_dataclass(_cls: Type[Any]) -> bool:
466
+ """
467
+ Whether a class is a stdlib dataclass
468
+ (useful to discriminated a pydantic dataclass that is actually a wrapper around a stdlib dataclass)
469
+
470
+ we check that
471
+ - `_cls` is a dataclass
472
+ - `_cls` is not a processed pydantic dataclass (with a basemodel attached)
473
+ - `_cls` is not a pydantic dataclass inheriting directly from a stdlib dataclass
474
+ e.g.
475
+ ```
476
+ @dataclasses.dataclass
477
+ class A:
478
+ x: int
479
+
480
+ @pydantic.dataclasses.dataclass
481
+ class B(A):
482
+ y: int
483
+ ```
484
+ In this case, when we first check `B`, we make an extra check and look at the annotations ('y'),
485
+ which won't be a superset of all the dataclass fields (only the stdlib fields i.e. 'x')
486
+ """
487
+ return (
488
+ dataclasses.is_dataclass(_cls)
489
+ and not hasattr(_cls, '__pydantic_model__')
490
+ and set(_cls.__dataclass_fields__).issuperset(set(getattr(_cls, '__annotations__', {})))
491
+ )
492
+
493
+
494
+ def make_dataclass_validator(dc_cls: Type['Dataclass'], config: Type[BaseConfig]) -> 'CallableGenerator':
495
+ """
496
+ Create a pydantic.dataclass from a builtin dataclass to add type validation
497
+ and yield the validators
498
+ It retrieves the parameters of the dataclass and forwards them to the newly created dataclass
499
+ """
500
+ yield from _get_validators(dataclass(dc_cls, config=config, use_proxy=True))