Coverage for pydantic/dataclasses.py: 97.96%

109 statements  

« prev     ^ index     » next       coverage.py v7.10.0, created at 2025-07-26 11:49 +0000

1"""Provide an enhanced dataclass that performs validation.""" 

2 

3from __future__ import annotations as _annotations 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

4 

5import dataclasses 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

6import functools 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

7import sys 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

8import types 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

9from typing import TYPE_CHECKING, Any, Callable, Generic, Literal, NoReturn, TypeVar, overload 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

10from warnings import warn 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

11 

12from typing_extensions import TypeGuard, dataclass_transform 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

13 

14from ._internal import _config, _decorators, _namespace_utils, _typing_extra 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

15from ._internal import _dataclasses as _pydantic_dataclasses 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

16from ._migration import getattr_migration 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

17from .config import ConfigDict 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

18from .errors import PydanticUserError 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

19from .fields import Field, FieldInfo, PrivateAttr 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

20 

21if TYPE_CHECKING: 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

22 from ._internal._dataclasses import PydanticDataclass 

23 from ._internal._namespace_utils import MappingNamespace 

24 

25__all__ = 'dataclass', 'rebuild_dataclass' 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

26 

27_T = TypeVar('_T') 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

28 

29if sys.version_info >= (3, 10): 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

30 

31 @dataclass_transform(field_specifiers=(dataclasses.field, Field, PrivateAttr)) 1hiCDjkabtuvclmEFnodewxyPpqGHrsfgzAB

32 @overload 1hiCDjkabtuvclmEFnodewxyPpqGHrsfgzAB

33 def dataclass( 1hiCDjkabtuvclmEFnodewxyPpqGHrsfgzAB

34 *, 

35 init: Literal[False] = False, 1hiCDjkabtuvclmEFnodewxyPpqGHrsfgzAB

36 repr: bool = True, 1hiCDjkabtuvclmEFnodewxyPpqGHrsfgzAB

37 eq: bool = True, 1hiCDjkabtuvclmEFnodewxyPpqGHrsfgzAB

38 order: bool = False, 1hiCDjkabtuvclmEFnodewxyPpqGHrsfgzAB

39 unsafe_hash: bool = False, 1hiCDjkabtuvclmEFnodewxyPpqGHrsfgzAB

40 frozen: bool = False, 1hiCDjkabtuvclmEFnodewxyPpqGHrsfgzAB

41 config: ConfigDict | type[object] | None = None, 1hiCDjkabtuvclmEFnodewxyPpqGHrsfgzAB

42 validate_on_init: bool | None = None, 1hiCDjkabtuvclmEFnodewxyPpqGHrsfgzAB

43 kw_only: bool = ..., 1hiCDjkabtuvclmEFnodewxyPpqGHrsfgzAB

44 slots: bool = ..., 1hiCDjkabtuvclmEFnodewxyPpqGHrsfgzAB

45 ) -> Callable[[type[_T]], type[PydanticDataclass]]: # type: ignore 1jkabcnodePrsfg

46 ... 

47 

48 @dataclass_transform(field_specifiers=(dataclasses.field, Field, PrivateAttr)) 1hiCDjkabtuvclmEFnodewxyPpqGHrsfgzAB

49 @overload 1hiCDjkabtuvclmEFnodewxyPpqGHrsfgzAB

50 def dataclass( 1hiCDjkabtuvclmEFnodewxyPpqGHrsfgzAB

51 _cls: type[_T], # type: ignore 1jkabcnodePrsfg

52 *, 

53 init: Literal[False] = False, 1hiCDjkabtuvclmEFnodewxyPpqGHrsfgzAB

54 repr: bool = True, 1hiCDjkabtuvclmEFnodewxyPpqGHrsfgzAB

55 eq: bool = True, 1hiCDjkabtuvclmEFnodewxyPpqGHrsfgzAB

56 order: bool = False, 1hiCDjkabtuvclmEFnodewxyPpqGHrsfgzAB

57 unsafe_hash: bool = False, 1hiCDjkabtuvclmEFnodewxyPpqGHrsfgzAB

58 frozen: bool | None = None, 1hiCDjkabtuvclmEFnodewxyPpqGHrsfgzAB

59 config: ConfigDict | type[object] | None = None, 1hiCDjkabtuvclmEFnodewxyPpqGHrsfgzAB

60 validate_on_init: bool | None = None, 1hiCDjkabtuvclmEFnodewxyPpqGHrsfgzAB

61 kw_only: bool = ..., 1hiCDjkabtuvclmEFnodewxyPpqGHrsfgzAB

62 slots: bool = ..., 1hiCDjkabtuvclmEFnodewxyPpqGHrsfgzAB

63 ) -> type[PydanticDataclass]: ... 1jkabcnodePrsfg

64 

65else: 

66 

67 @dataclass_transform(field_specifiers=(dataclasses.field, Field, PrivateAttr)) 1JKILMNO

68 @overload 1JKILMNO

69 def dataclass( 1JKILMNO

70 *, 

71 init: Literal[False] = False, 1JKILMNO

72 repr: bool = True, 1JKILMNO

73 eq: bool = True, 1JKILMNO

74 order: bool = False, 1JKILMNO

75 unsafe_hash: bool = False, 1JKILMNO

76 frozen: bool | None = None, 1JKILMNO

77 config: ConfigDict | type[object] | None = None, 1JKILMNO

78 validate_on_init: bool | None = None, 1JKILMNO

79 ) -> Callable[[type[_T]], type[PydanticDataclass]]: # type: ignore 1I

80 ... 

81 

82 @dataclass_transform(field_specifiers=(dataclasses.field, Field, PrivateAttr)) 1JKILMNO

83 @overload 1JKILMNO

84 def dataclass( 1JKILMNO

85 _cls: type[_T], # type: ignore 1I

86 *, 

87 init: Literal[False] = False, 1JKILMNO

88 repr: bool = True, 1JKILMNO

89 eq: bool = True, 1JKILMNO

90 order: bool = False, 1JKILMNO

91 unsafe_hash: bool = False, 1JKILMNO

92 frozen: bool | None = None, 1JKILMNO

93 config: ConfigDict | type[object] | None = None, 1JKILMNO

94 validate_on_init: bool | None = None, 1JKILMNO

95 ) -> type[PydanticDataclass]: ... 1I

96 

97 

98@dataclass_transform(field_specifiers=(dataclasses.field, Field, PrivateAttr)) 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

99def dataclass( 1JKhiCDjkabtuvLMlmEFnodewxyPNOpqGHrsfgzAB

100 _cls: type[_T] | None = None, 

101 *, 

102 init: Literal[False] = False, 

103 repr: bool = True, 

104 eq: bool = True, 

105 order: bool = False, 

106 unsafe_hash: bool = False, 

107 frozen: bool | None = None, 

108 config: ConfigDict | type[object] | None = None, 

109 validate_on_init: bool | None = None, 

110 kw_only: bool = False, 

111 slots: bool = False, 

112) -> Callable[[type[_T]], type[PydanticDataclass]] | type[PydanticDataclass]: 

113 """!!! abstract "Usage Documentation" 

114 [`dataclasses`](../concepts/dataclasses.md) 

115 

116 A decorator used to create a Pydantic-enhanced dataclass, similar to the standard Python `dataclass`, 

117 but with added validation. 

118 

119 This function should be used similarly to `dataclasses.dataclass`. 

120 

121 Args: 

122 _cls: The target `dataclass`. 

123 init: Included for signature compatibility with `dataclasses.dataclass`, and is passed through to 

124 `dataclasses.dataclass` when appropriate. If specified, must be set to `False`, as pydantic inserts its 

125 own `__init__` function. 

126 repr: A boolean indicating whether to include the field in the `__repr__` output. 

127 eq: Determines if a `__eq__` method should be generated for the class. 

128 order: Determines if comparison magic methods should be generated, such as `__lt__`, but not `__eq__`. 

129 unsafe_hash: Determines if a `__hash__` method should be included in the class, as in `dataclasses.dataclass`. 

130 frozen: Determines if the generated class should be a 'frozen' `dataclass`, which does not allow its 

131 attributes to be modified after it has been initialized. If not set, the value from the provided `config` argument will be used (and will default to `False` otherwise). 

132 config: The Pydantic config to use for the `dataclass`. 

133 validate_on_init: A deprecated parameter included for backwards compatibility; in V2, all Pydantic dataclasses 

134 are validated on init. 

135 kw_only: Determines if `__init__` method parameters must be specified by keyword only. Defaults to `False`. 

136 slots: Determines if the generated class should be a 'slots' `dataclass`, which does not allow the addition of 

137 new attributes after instantiation. 

138 

139 Returns: 

140 A decorator that accepts a class as its argument and returns a Pydantic `dataclass`. 

141 

142 Raises: 

143 AssertionError: Raised if `init` is not `False` or `validate_on_init` is `False`. 

144 """ 

145 assert init is False, 'pydantic.dataclasses.dataclass only supports init=False' 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

146 assert validate_on_init is not False, 'validate_on_init=False is no longer supported' 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

147 

148 if sys.version_info >= (3, 10): 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

149 kwargs = {'kw_only': kw_only, 'slots': slots} 1hiCDjkabtuvclmEFnodewxyPpqGHrsfgzAB

150 else: 

151 kwargs = {} 1JKILMNO

152 

153 def create_dataclass(cls: type[Any]) -> type[PydanticDataclass]: 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

154 """Create a Pydantic dataclass from a regular dataclass. 

155 

156 Args: 

157 cls: The class to create the Pydantic dataclass from. 

158 

159 Returns: 

160 A Pydantic dataclass. 

161 """ 

162 from ._internal._utils import is_model_class 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

163 

164 if is_model_class(cls): 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

165 raise PydanticUserError( 1JKhiCDjkabtuvIcLMlmEFnodewxyNOpqGHrsfgzAB

166 f'Cannot create a Pydantic dataclass from {cls.__name__} as it is already a Pydantic model', 

167 code='dataclass-on-model', 

168 ) 

169 

170 original_cls = cls 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

171 

172 # we warn on conflicting config specifications, but only if the class doesn't have a dataclass base 

173 # because a dataclass base might provide a __pydantic_config__ attribute that we don't want to warn about 

174 has_dataclass_base = any(dataclasses.is_dataclass(base) for base in cls.__bases__) 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

175 if not has_dataclass_base and config is not None and hasattr(cls, '__pydantic_config__'): 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

176 warn( 1JKhiCDjkabtuvIcLMlmEFnodewxyNOpqGHrsfgzAB

177 f'`config` is set via both the `dataclass` decorator and `__pydantic_config__` for dataclass {cls.__name__}. ' 

178 f'The `config` specification from `dataclass` decorator will take priority.', 

179 category=UserWarning, 

180 stacklevel=2, 

181 ) 

182 

183 # if config is not explicitly provided, try to read it from the type 

184 config_dict = config if config is not None else getattr(cls, '__pydantic_config__', None) 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

185 config_wrapper = _config.ConfigWrapper(config_dict) 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

186 decorators = _decorators.DecoratorInfos.build(cls) 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

187 decorators.update_from_config(config_wrapper) 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

188 

189 # Keep track of the original __doc__ so that we can restore it after applying the dataclasses decorator 

190 # Otherwise, classes with no __doc__ will have their signature added into the JSON schema description, 

191 # since dataclasses.dataclass will set this as the __doc__ 

192 original_doc = cls.__doc__ 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

193 

194 if _pydantic_dataclasses.is_stdlib_dataclass(cls): 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

195 # Vanilla dataclasses include a default docstring (representing the class signature), 

196 # which we don't want to preserve. 

197 original_doc = None 1JKhiCDjkabtuvIcLMlmEFnodewxyNOpqGHrsfgzAB

198 

199 # We don't want to add validation to the existing std lib dataclass, so we will subclass it 

200 # If the class is generic, we need to make sure the subclass also inherits from Generic 

201 # with all the same parameters. 

202 bases = (cls,) 1JKhiCDjkabtuvIcLMlmEFnodewxyNOpqGHrsfgzAB

203 if issubclass(cls, Generic): 1JKhiCDjkabtuvIcLMlmEFnodewxyNOpqGHrsfgzAB

204 generic_base = Generic[cls.__parameters__] # type: ignore 1JKhiCDjkabtuvIcLMlmEFnodewxyNOpqGHrsfgzAB

205 bases = bases + (generic_base,) 1JKhiCDjkabtuvIcLMlmEFnodewxyNOpqGHrsfgzAB

206 cls = types.new_class(cls.__name__, bases) 1JKhiCDjkabtuvIcLMlmEFnodewxyNOpqGHrsfgzAB

207 

208 # Respect frozen setting from dataclass constructor and fallback to config setting if not provided 

209 if frozen is not None: 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

210 frozen_ = frozen 1JKhiCDjkabtuvIcLMlmEFnodewxyNOpqGHrsfgzAB

211 if config_wrapper.frozen: 1JKhiCDjkabtuvIcLMlmEFnodewxyNOpqGHrsfgzAB

212 # It's not recommended to define both, as the setting from the dataclass decorator will take priority. 

213 warn( 1JKhiCDjkabtuvIcLMlmEFnodewxyNOpqGHrsfgzAB

214 f'`frozen` is set via both the `dataclass` decorator and `config` for dataclass {cls.__name__!r}.' 

215 'This is not recommended. The `frozen` specification on `dataclass` will take priority.', 

216 category=UserWarning, 

217 stacklevel=2, 

218 ) 

219 else: 

220 frozen_ = config_wrapper.frozen or False 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

221 

222 # Make Pydantic's `Field()` function compatible with stdlib dataclasses. As we'll decorate 

223 # `cls` with the stdlib `@dataclass` decorator first, there are two attributes, `kw_only` and 

224 # `repr` that need to be understood *during* the stdlib creation. We do so in two steps: 

225 

226 # 1. On the decorated class, wrap `Field()` assignment with `dataclass.field()`, with the 

227 # two attributes set (done in `as_dataclass_field()`) 

228 cls_anns = _typing_extra.safe_get_annotations(cls) 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

229 for field_name in cls_anns: 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

230 # We should look for assignments in `__dict__` instead, but for now we follow 

231 # the same behavior as stdlib dataclasses (see https://github.com/python/cpython/issues/88609) 

232 field_value = getattr(cls, field_name, None) 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

233 if isinstance(field_value, FieldInfo): 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

234 setattr(cls, field_name, _pydantic_dataclasses.as_dataclass_field(field_value)) 1JKhiCDjkabtuvIcLMlmEFnodewxyNOpqGHrsfgzAB

235 

236 # 2. For bases of `cls` that are stdlib dataclasses, we temporarily patch their fields 

237 # (see the docstring of the context manager): 

238 with _pydantic_dataclasses.patch_base_fields(cls): 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

239 cls = dataclasses.dataclass( # pyright: ignore[reportCallIssue] 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

240 cls, 

241 # the value of init here doesn't affect anything except that it makes it easier to generate a signature 

242 init=True, 

243 repr=repr, 

244 eq=eq, 

245 order=order, 

246 unsafe_hash=unsafe_hash, 

247 frozen=frozen_, 

248 **kwargs, 

249 ) 

250 

251 if config_wrapper.validate_assignment: 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

252 

253 @functools.wraps(cls.__setattr__) 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

254 def validated_setattr(instance: Any, field: str, value: str, /) -> None: 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

255 type(instance).__pydantic_validator__.validate_assignment(instance, field, value) 1JKhiCDjkabtuvIcLMlmEFnodewxyNOpqGHrsfgzAB

256 

257 cls.__setattr__ = validated_setattr.__get__(None, cls) # type: ignore 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

258 

259 if slots and not hasattr(cls, '__setstate__'): 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

260 # If slots is set, `pickle` (relied on by `copy.copy()`) will use 

261 # `__setattr__()` to reconstruct the dataclass. However, the custom 

262 # `__setattr__()` set above relies on `validate_assignment()`, which 

263 # in turn expects all the field values to be already present on the 

264 # instance, resulting in attribute errors. 

265 # As such, we make use of `object.__setattr__()` instead. 

266 # Note that we do so only if `__setstate__()` isn't already set (this is the 

267 # case if on top of `slots`, `frozen` is used). 

268 

269 # Taken from `dataclasses._dataclass_get/setstate()`: 

270 def _dataclass_getstate(self: Any) -> list[Any]: 1JKhiCDjkabtuvIcLMlmEFnodewxyNOpqGHrsfgzAB

271 return [getattr(self, f.name) for f in dataclasses.fields(self)] 1hiCDjkabtuvclmEFnodewxypqGHrsfgzAB

272 

273 def _dataclass_setstate(self: Any, state: list[Any]) -> None: 1JKhiCDjkabtuvIcLMlmEFnodewxyNOpqGHrsfgzAB

274 for field, value in zip(dataclasses.fields(self), state): 1hiCDjkabtuvclmEFnodewxypqGHrsfgzAB

275 object.__setattr__(self, field.name, value) 1hiCDjkabtuvclmEFnodewxypqGHrsfgzAB

276 

277 cls.__getstate__ = _dataclass_getstate # pyright: ignore[reportAttributeAccessIssue] 1JKhiCDjkabtuvIcLMlmEFnodewxyNOpqGHrsfgzAB

278 cls.__setstate__ = _dataclass_setstate # pyright: ignore[reportAttributeAccessIssue] 1JKhiCDjkabtuvIcLMlmEFnodewxyNOpqGHrsfgzAB

279 

280 # This is an undocumented attribute to distinguish stdlib/Pydantic dataclasses. 

281 # It should be set as early as possible: 

282 cls.__is_pydantic_dataclass__ = True 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

283 cls.__pydantic_decorators__ = decorators # type: ignore 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

284 cls.__doc__ = original_doc 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

285 # Can be non-existent for dynamically created classes: 

286 firstlineno = getattr(original_cls, '__firstlineno__', None) 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

287 cls.__module__ = original_cls.__module__ 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

288 if sys.version_info >= (3, 13) and firstlineno is not None: 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

289 # As per https://docs.python.org/3/reference/datamodel.html#type.__firstlineno__: 

290 # Setting the `__module__` attribute removes the `__firstlineno__` item from the type’s dictionary. 

291 original_cls.__firstlineno__ = firstlineno 1abtuvdewxyPfgzAB

292 cls.__firstlineno__ = firstlineno 1abtuvdewxyPfgzAB

293 cls.__qualname__ = original_cls.__qualname__ 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

294 cls.__pydantic_fields_complete__ = classmethod(_pydantic_fields_complete) 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

295 cls.__pydantic_complete__ = False # `complete_dataclass` will set it to `True` if successful. 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

296 # TODO `parent_namespace` is currently None, but we could do the same thing as Pydantic models: 

297 # fetch the parent ns using `parent_frame_namespace` (if the dataclass was defined in a function), 

298 # and possibly cache it (see the `__pydantic_parent_namespace__` logic for models). 

299 _pydantic_dataclasses.complete_dataclass(cls, config_wrapper, raise_errors=False) 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

300 return cls 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

301 

302 return create_dataclass if _cls is None else create_dataclass(_cls) 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

303 

304 

305def _pydantic_fields_complete(cls: type[PydanticDataclass]) -> bool: 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

306 """Return whether the fields where successfully collected (i.e. type hints were successfully resolves). 

307 

308 This is a private property, not meant to be used outside Pydantic. 

309 """ 

310 return all(field_info._complete for field_info in cls.__pydantic_fields__.values()) 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

311 

312 

313__getattr__ = getattr_migration(__name__) 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

314 

315if sys.version_info < (3, 11): 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

316 # Monkeypatch dataclasses.InitVar so that typing doesn't error if it occurs as a type when evaluating type hints 

317 # Starting in 3.11, typing.get_type_hints will not raise an error if the retrieved type hints are not callable. 

318 

319 def _call_initvar(*args: Any, **kwargs: Any) -> NoReturn: 1JKhiIcLMlmNOpq

320 """This function does nothing but raise an error that is as similar as possible to what you'd get 

321 if you were to try calling `InitVar[int]()` without this monkeypatch. The whole purpose is just 

322 to ensure typing._type_check does not error if the type hint evaluates to `InitVar[<parameter>]`. 

323 """ 

324 raise TypeError("'InitVar' object is not callable") 1JKhiIcLMlmNOpq

325 

326 dataclasses.InitVar.__call__ = _call_initvar 1JKhiIcLMlmNOpq

327 

328 

329def rebuild_dataclass( 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

330 cls: type[PydanticDataclass], 

331 *, 

332 force: bool = False, 

333 raise_errors: bool = True, 

334 _parent_namespace_depth: int = 2, 

335 _types_namespace: MappingNamespace | None = None, 

336) -> bool | None: 

337 """Try to rebuild the pydantic-core schema for the dataclass. 

338 

339 This may be necessary when one of the annotations is a ForwardRef which could not be resolved during 

340 the initial attempt to build the schema, and automatic rebuilding fails. 

341 

342 This is analogous to `BaseModel.model_rebuild`. 

343 

344 Args: 

345 cls: The class to rebuild the pydantic-core schema for. 

346 force: Whether to force the rebuilding of the schema, defaults to `False`. 

347 raise_errors: Whether to raise errors, defaults to `True`. 

348 _parent_namespace_depth: The depth level of the parent namespace, defaults to 2. 

349 _types_namespace: The types namespace, defaults to `None`. 

350 

351 Returns: 

352 Returns `None` if the schema is already "complete" and rebuilding was not required. 

353 If rebuilding _was_ required, returns `True` if rebuilding was successful, otherwise `False`. 

354 """ 

355 if not force and cls.__pydantic_complete__: 1JKhiCDjkabtuvIcLMlmEFnodewxyNOpqGHrsfgzAB

356 return None 1JKhiCDjkabtuvIcLMlmEFnodewxyNOpqGHrsfgzAB

357 

358 for attr in ('__pydantic_core_schema__', '__pydantic_validator__', '__pydantic_serializer__'): 1JKhiCDjkabtuvIcLMlmEFnodewxyNOpqGHrsfgzAB

359 if attr in cls.__dict__: 359 ↛ 358line 359 didn't jump to line 358 because the condition on line 359 was always true1JKhiCDjkabtuvIcLMlmEFnodewxyNOpqGHrsfgzAB

360 # Deleting the validator/serializer is necessary as otherwise they can get reused in 

361 # pydantic-core. Same applies for the core schema that can be reused in schema generation. 

362 delattr(cls, attr) 1JKhiCDjkabtuvIcLMlmEFnodewxyNOpqGHrsfgzAB

363 

364 cls.__pydantic_complete__ = False 1JKhiCDjkabtuvIcLMlmEFnodewxyNOpqGHrsfgzAB

365 

366 if _types_namespace is not None: 1JKhiCDjkabtuvIcLMlmEFnodewxyNOpqGHrsfgzAB

367 rebuild_ns = _types_namespace 1JKhiCDjkabtuvIcLMlmEFnodewxyNOpqGHrsfgzAB

368 elif _parent_namespace_depth > 0: 1JKhiCDjkabtuvIcLMlmEFnodewxyNOpqGHrsfgzAB

369 rebuild_ns = _typing_extra.parent_frame_namespace(parent_depth=_parent_namespace_depth, force=True) or {} 1JKhiCDjkabtuvIcLMlmEFnodewxyNOpqGHrsfgzAB

370 else: 

371 rebuild_ns = {} 1JKhiCDjkabtuvIcLMlmEFnodewxyNOpqGHrsfgzAB

372 

373 ns_resolver = _namespace_utils.NsResolver( 1JKhiCDjkabtuvIcLMlmEFnodewxyNOpqGHrsfgzAB

374 parent_namespace=rebuild_ns, 

375 ) 

376 

377 return _pydantic_dataclasses.complete_dataclass( 1JKhiCDjkabtuvIcLMlmEFnodewxyNOpqGHrsfgzAB

378 cls, 

379 _config.ConfigWrapper(cls.__pydantic_config__, check=False), 

380 raise_errors=raise_errors, 

381 ns_resolver=ns_resolver, 

382 # We could provide a different config instead (with `'defer_build'` set to `True`) 

383 # of this explicit `_force_build` argument, but because config can come from the 

384 # decorator parameter or the `__pydantic_config__` attribute, `complete_dataclass` 

385 # will overwrite `__pydantic_config__` with the provided config above: 

386 _force_build=True, 

387 ) 

388 

389 

390def is_pydantic_dataclass(class_: type[Any], /) -> TypeGuard[type[PydanticDataclass]]: 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

391 """Whether a class is a pydantic dataclass. 

392 

393 Args: 

394 class_: The class. 

395 

396 Returns: 

397 `True` if the class is a pydantic dataclass, `False` otherwise. 

398 """ 

399 try: 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

400 return '__is_pydantic_dataclass__' in class_.__dict__ and dataclasses.is_dataclass(class_) 1JKhiCDjkabtuvIcLMlmEFnodewxyPNOpqGHrsfgzAB

401 except AttributeError: 

402 return False