Coverage for pydantic / __init__.py: 88.24%

26 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-11-21 11:56 +0000

1from importlib import import_module 1abcdefghijklsmnopqr

2from typing import TYPE_CHECKING 1abcdefghijklsmnopqr

3 

4from .version import VERSION, _ensure_pydantic_core_version 1abcdefghijklsmnopqr

5 

6_ensure_pydantic_core_version() 1abcdefghijklsmnopqr

7del _ensure_pydantic_core_version 1abcdefghijklsmnopqr

8 

9if TYPE_CHECKING: 1abcdefghijklsmnopqr

10 # import of virtually everything is supported via `__getattr__` below, 

11 # but we need them here for type checking and IDE support 

12 import pydantic_core 

13 from pydantic_core.core_schema import ( 

14 FieldSerializationInfo, 

15 SerializationInfo, 

16 SerializerFunctionWrapHandler, 

17 ValidationInfo, 

18 ValidatorFunctionWrapHandler, 

19 ) 

20 

21 from . import dataclasses 

22 from .aliases import AliasChoices, AliasGenerator, AliasPath 

23 from .annotated_handlers import GetCoreSchemaHandler, GetJsonSchemaHandler 

24 from .config import ConfigDict, with_config 

25 from .errors import * 

26 from .fields import Field, PrivateAttr, computed_field 

27 from .functional_serializers import ( 

28 PlainSerializer, 

29 SerializeAsAny, 

30 WrapSerializer, 

31 field_serializer, 

32 model_serializer, 

33 ) 

34 from .functional_validators import ( 

35 AfterValidator, 

36 BeforeValidator, 

37 InstanceOf, 

38 ModelWrapValidatorHandler, 

39 PlainValidator, 

40 SkipValidation, 

41 ValidateAs, 

42 WrapValidator, 

43 field_validator, 

44 model_validator, 

45 ) 

46 from .json_schema import WithJsonSchema 

47 from .main import * 

48 from .networks import * 

49 from .type_adapter import TypeAdapter 

50 from .types import * 

51 from .validate_call_decorator import validate_call 

52 from .warnings import ( 

53 PydanticDeprecatedSince20, 

54 PydanticDeprecatedSince211, 

55 PydanticDeprecationWarning, 

56 PydanticExperimentalWarning, 

57 ) 

58 

59 MISSING = pydantic_core.MISSING 

60 ValidationError = pydantic_core.ValidationError 

61 from .root_model import RootModel 

62 

63__version__ = VERSION 1abcdefghijklsmnopqr

64__all__ = ( 1abcdefghijklsmnopqr

65 # dataclasses 

66 'dataclasses', 

67 # functional validators 

68 'field_validator', 

69 'model_validator', 

70 'AfterValidator', 

71 'BeforeValidator', 

72 'PlainValidator', 

73 'WrapValidator', 

74 'SkipValidation', 

75 'ValidateAs', 

76 'InstanceOf', 

77 'ModelWrapValidatorHandler', 

78 # JSON Schema 

79 'WithJsonSchema', 

80 # functional serializers 

81 'field_serializer', 

82 'model_serializer', 

83 'PlainSerializer', 

84 'SerializeAsAny', 

85 'WrapSerializer', 

86 # config 

87 'ConfigDict', 

88 'with_config', 

89 # validate_call 

90 'validate_call', 

91 # errors 

92 'PydanticErrorCodes', 

93 'PydanticUserError', 

94 'PydanticSchemaGenerationError', 

95 'PydanticUndefinedAnnotation', 

96 'PydanticInvalidForJsonSchema', 

97 'PydanticForbiddenQualifier', 

98 # fields 

99 'Field', 

100 'computed_field', 

101 'PrivateAttr', 

102 # alias 

103 'AliasChoices', 

104 'AliasGenerator', 

105 'AliasPath', 

106 # main 

107 'BaseModel', 

108 'create_model', 

109 # network 

110 'AnyUrl', 

111 'AnyHttpUrl', 

112 'FileUrl', 

113 'HttpUrl', 

114 'FtpUrl', 

115 'WebsocketUrl', 

116 'AnyWebsocketUrl', 

117 'UrlConstraints', 

118 'EmailStr', 

119 'NameEmail', 

120 'IPvAnyAddress', 

121 'IPvAnyInterface', 

122 'IPvAnyNetwork', 

123 'PostgresDsn', 

124 'CockroachDsn', 

125 'AmqpDsn', 

126 'RedisDsn', 

127 'MongoDsn', 

128 'KafkaDsn', 

129 'NatsDsn', 

130 'MySQLDsn', 

131 'MariaDBDsn', 

132 'ClickHouseDsn', 

133 'SnowflakeDsn', 

134 'validate_email', 

135 # root_model 

136 'RootModel', 

137 # types 

138 'Strict', 

139 'StrictStr', 

140 'conbytes', 

141 'conlist', 

142 'conset', 

143 'confrozenset', 

144 'constr', 

145 'StringConstraints', 

146 'ImportString', 

147 'conint', 

148 'PositiveInt', 

149 'NegativeInt', 

150 'NonNegativeInt', 

151 'NonPositiveInt', 

152 'confloat', 

153 'PositiveFloat', 

154 'NegativeFloat', 

155 'NonNegativeFloat', 

156 'NonPositiveFloat', 

157 'FiniteFloat', 

158 'condecimal', 

159 'condate', 

160 'UUID1', 

161 'UUID3', 

162 'UUID4', 

163 'UUID5', 

164 'UUID6', 

165 'UUID7', 

166 'UUID8', 

167 'FilePath', 

168 'DirectoryPath', 

169 'NewPath', 

170 'Json', 

171 'Secret', 

172 'SecretStr', 

173 'SecretBytes', 

174 'SocketPath', 

175 'StrictBool', 

176 'StrictBytes', 

177 'StrictInt', 

178 'StrictFloat', 

179 'ByteSize', 

180 'PastDate', 

181 'FutureDate', 

182 'PastDatetime', 

183 'FutureDatetime', 

184 'AwareDatetime', 

185 'NaiveDatetime', 

186 'AllowInfNan', 

187 'EncoderProtocol', 

188 'EncodedBytes', 

189 'EncodedStr', 

190 'Base64Encoder', 

191 'Base64Bytes', 

192 'Base64Str', 

193 'Base64UrlBytes', 

194 'Base64UrlStr', 

195 'GetPydanticSchema', 

196 'Tag', 

197 'Discriminator', 

198 'JsonValue', 

199 'FailFast', 

200 # type_adapter 

201 'TypeAdapter', 

202 # version 

203 '__version__', 

204 'VERSION', 

205 # warnings 

206 'PydanticDeprecatedSince20', 

207 'PydanticDeprecatedSince211', 

208 'PydanticDeprecationWarning', 

209 'PydanticExperimentalWarning', 

210 # annotated handlers 

211 'GetCoreSchemaHandler', 

212 'GetJsonSchemaHandler', 

213 # pydantic_core 

214 'MISSING', 

215 'ValidationError', 

216 'ValidationInfo', 

217 'SerializationInfo', 

218 'ValidatorFunctionWrapHandler', 

219 'FieldSerializationInfo', 

220 'SerializerFunctionWrapHandler', 

221 'OnErrorOmit', 

222) 

223 

224# A mapping of {<member name>: (package, <module name>)} defining dynamic imports 

225_dynamic_imports: 'dict[str, tuple[str, str]]' = { 1abcdefghijklsmnopqr

226 'dataclasses': (__spec__.parent, '__module__'), 

227 # functional validators 

228 'field_validator': (__spec__.parent, '.functional_validators'), 

229 'model_validator': (__spec__.parent, '.functional_validators'), 

230 'AfterValidator': (__spec__.parent, '.functional_validators'), 

231 'BeforeValidator': (__spec__.parent, '.functional_validators'), 

232 'PlainValidator': (__spec__.parent, '.functional_validators'), 

233 'WrapValidator': (__spec__.parent, '.functional_validators'), 

234 'SkipValidation': (__spec__.parent, '.functional_validators'), 

235 'InstanceOf': (__spec__.parent, '.functional_validators'), 

236 'ValidateAs': (__spec__.parent, '.functional_validators'), 

237 'ModelWrapValidatorHandler': (__spec__.parent, '.functional_validators'), 

238 # JSON Schema 

239 'WithJsonSchema': (__spec__.parent, '.json_schema'), 

240 # functional serializers 

241 'field_serializer': (__spec__.parent, '.functional_serializers'), 

242 'model_serializer': (__spec__.parent, '.functional_serializers'), 

243 'PlainSerializer': (__spec__.parent, '.functional_serializers'), 

244 'SerializeAsAny': (__spec__.parent, '.functional_serializers'), 

245 'WrapSerializer': (__spec__.parent, '.functional_serializers'), 

246 # config 

247 'ConfigDict': (__spec__.parent, '.config'), 

248 'with_config': (__spec__.parent, '.config'), 

249 # validate call 

250 'validate_call': (__spec__.parent, '.validate_call_decorator'), 

251 # errors 

252 'PydanticErrorCodes': (__spec__.parent, '.errors'), 

253 'PydanticUserError': (__spec__.parent, '.errors'), 

254 'PydanticSchemaGenerationError': (__spec__.parent, '.errors'), 

255 'PydanticUndefinedAnnotation': (__spec__.parent, '.errors'), 

256 'PydanticInvalidForJsonSchema': (__spec__.parent, '.errors'), 

257 'PydanticForbiddenQualifier': (__spec__.parent, '.errors'), 

258 # fields 

259 'Field': (__spec__.parent, '.fields'), 

260 'computed_field': (__spec__.parent, '.fields'), 

261 'PrivateAttr': (__spec__.parent, '.fields'), 

262 # alias 

263 'AliasChoices': (__spec__.parent, '.aliases'), 

264 'AliasGenerator': (__spec__.parent, '.aliases'), 

265 'AliasPath': (__spec__.parent, '.aliases'), 

266 # main 

267 'BaseModel': (__spec__.parent, '.main'), 

268 'create_model': (__spec__.parent, '.main'), 

269 # network 

270 'AnyUrl': (__spec__.parent, '.networks'), 

271 'AnyHttpUrl': (__spec__.parent, '.networks'), 

272 'FileUrl': (__spec__.parent, '.networks'), 

273 'HttpUrl': (__spec__.parent, '.networks'), 

274 'FtpUrl': (__spec__.parent, '.networks'), 

275 'WebsocketUrl': (__spec__.parent, '.networks'), 

276 'AnyWebsocketUrl': (__spec__.parent, '.networks'), 

277 'UrlConstraints': (__spec__.parent, '.networks'), 

278 'EmailStr': (__spec__.parent, '.networks'), 

279 'NameEmail': (__spec__.parent, '.networks'), 

280 'IPvAnyAddress': (__spec__.parent, '.networks'), 

281 'IPvAnyInterface': (__spec__.parent, '.networks'), 

282 'IPvAnyNetwork': (__spec__.parent, '.networks'), 

283 'PostgresDsn': (__spec__.parent, '.networks'), 

284 'CockroachDsn': (__spec__.parent, '.networks'), 

285 'AmqpDsn': (__spec__.parent, '.networks'), 

286 'RedisDsn': (__spec__.parent, '.networks'), 

287 'MongoDsn': (__spec__.parent, '.networks'), 

288 'KafkaDsn': (__spec__.parent, '.networks'), 

289 'NatsDsn': (__spec__.parent, '.networks'), 

290 'MySQLDsn': (__spec__.parent, '.networks'), 

291 'MariaDBDsn': (__spec__.parent, '.networks'), 

292 'ClickHouseDsn': (__spec__.parent, '.networks'), 

293 'SnowflakeDsn': (__spec__.parent, '.networks'), 

294 'validate_email': (__spec__.parent, '.networks'), 

295 # root_model 

296 'RootModel': (__spec__.parent, '.root_model'), 

297 # types 

298 'Strict': (__spec__.parent, '.types'), 

299 'StrictStr': (__spec__.parent, '.types'), 

300 'conbytes': (__spec__.parent, '.types'), 

301 'conlist': (__spec__.parent, '.types'), 

302 'conset': (__spec__.parent, '.types'), 

303 'confrozenset': (__spec__.parent, '.types'), 

304 'constr': (__spec__.parent, '.types'), 

305 'StringConstraints': (__spec__.parent, '.types'), 

306 'ImportString': (__spec__.parent, '.types'), 

307 'conint': (__spec__.parent, '.types'), 

308 'PositiveInt': (__spec__.parent, '.types'), 

309 'NegativeInt': (__spec__.parent, '.types'), 

310 'NonNegativeInt': (__spec__.parent, '.types'), 

311 'NonPositiveInt': (__spec__.parent, '.types'), 

312 'confloat': (__spec__.parent, '.types'), 

313 'PositiveFloat': (__spec__.parent, '.types'), 

314 'NegativeFloat': (__spec__.parent, '.types'), 

315 'NonNegativeFloat': (__spec__.parent, '.types'), 

316 'NonPositiveFloat': (__spec__.parent, '.types'), 

317 'FiniteFloat': (__spec__.parent, '.types'), 

318 'condecimal': (__spec__.parent, '.types'), 

319 'condate': (__spec__.parent, '.types'), 

320 'UUID1': (__spec__.parent, '.types'), 

321 'UUID3': (__spec__.parent, '.types'), 

322 'UUID4': (__spec__.parent, '.types'), 

323 'UUID5': (__spec__.parent, '.types'), 

324 'UUID6': (__spec__.parent, '.types'), 

325 'UUID7': (__spec__.parent, '.types'), 

326 'UUID8': (__spec__.parent, '.types'), 

327 'FilePath': (__spec__.parent, '.types'), 

328 'DirectoryPath': (__spec__.parent, '.types'), 

329 'NewPath': (__spec__.parent, '.types'), 

330 'Json': (__spec__.parent, '.types'), 

331 'Secret': (__spec__.parent, '.types'), 

332 'SecretStr': (__spec__.parent, '.types'), 

333 'SecretBytes': (__spec__.parent, '.types'), 

334 'StrictBool': (__spec__.parent, '.types'), 

335 'StrictBytes': (__spec__.parent, '.types'), 

336 'StrictInt': (__spec__.parent, '.types'), 

337 'StrictFloat': (__spec__.parent, '.types'), 

338 'ByteSize': (__spec__.parent, '.types'), 

339 'PastDate': (__spec__.parent, '.types'), 

340 'SocketPath': (__spec__.parent, '.types'), 

341 'FutureDate': (__spec__.parent, '.types'), 

342 'PastDatetime': (__spec__.parent, '.types'), 

343 'FutureDatetime': (__spec__.parent, '.types'), 

344 'AwareDatetime': (__spec__.parent, '.types'), 

345 'NaiveDatetime': (__spec__.parent, '.types'), 

346 'AllowInfNan': (__spec__.parent, '.types'), 

347 'EncoderProtocol': (__spec__.parent, '.types'), 

348 'EncodedBytes': (__spec__.parent, '.types'), 

349 'EncodedStr': (__spec__.parent, '.types'), 

350 'Base64Encoder': (__spec__.parent, '.types'), 

351 'Base64Bytes': (__spec__.parent, '.types'), 

352 'Base64Str': (__spec__.parent, '.types'), 

353 'Base64UrlBytes': (__spec__.parent, '.types'), 

354 'Base64UrlStr': (__spec__.parent, '.types'), 

355 'GetPydanticSchema': (__spec__.parent, '.types'), 

356 'Tag': (__spec__.parent, '.types'), 

357 'Discriminator': (__spec__.parent, '.types'), 

358 'JsonValue': (__spec__.parent, '.types'), 

359 'OnErrorOmit': (__spec__.parent, '.types'), 

360 'FailFast': (__spec__.parent, '.types'), 

361 # type_adapter 

362 'TypeAdapter': (__spec__.parent, '.type_adapter'), 

363 # warnings 

364 'PydanticDeprecatedSince20': (__spec__.parent, '.warnings'), 

365 'PydanticDeprecatedSince211': (__spec__.parent, '.warnings'), 

366 'PydanticDeprecationWarning': (__spec__.parent, '.warnings'), 

367 'PydanticExperimentalWarning': (__spec__.parent, '.warnings'), 

368 # annotated handlers 

369 'GetCoreSchemaHandler': (__spec__.parent, '.annotated_handlers'), 

370 'GetJsonSchemaHandler': (__spec__.parent, '.annotated_handlers'), 

371 # pydantic_core stuff 

372 'MISSING': ('pydantic_core', '.'), 

373 'ValidationError': ('pydantic_core', '.'), 

374 'ValidationInfo': ('pydantic_core', '.core_schema'), 

375 'SerializationInfo': ('pydantic_core', '.core_schema'), 

376 'ValidatorFunctionWrapHandler': ('pydantic_core', '.core_schema'), 

377 'FieldSerializationInfo': ('pydantic_core', '.core_schema'), 

378 'SerializerFunctionWrapHandler': ('pydantic_core', '.core_schema'), 

379} 

380 

381 

382def __getattr__(attr_name: str) -> object: 1abcdefghijklsmnopqr

383 dynamic_attr = _dynamic_imports.get(attr_name) 1abcdefghijklsmnopqr

384 if dynamic_attr is None: 1abcdefghijklsmnopqr

385 raise AttributeError(f'module {__name__!r} has no attribute {attr_name!r}') 1abcdefghijklsmnopqr

386 

387 package, module_name = dynamic_attr 1abcdefghijklsmnopqr

388 

389 if module_name == '__module__': 389 ↛ 390line 389 didn't jump to line 390 because the condition on line 389 was never true1abcdefghijklsmnopqr

390 result = import_module(f'.{attr_name}', package=package) 

391 globals()[attr_name] = result 

392 return result 

393 else: 

394 module = import_module(module_name, package=package) 1abcdefghijklsmnopqr

395 result = getattr(module, attr_name) 1abcdefghijklsmnopqr

396 g = globals() 1abcdefghijklsmnopqr

397 for k, (_, v_module_name) in _dynamic_imports.items(): 1abcdefghijklsmnopqr

398 if v_module_name == module_name: 1abcdefghijklsmnopqr

399 g[k] = getattr(module, k) 1abcdefghijklsmnopqr

400 return result 1abcdefghijklsmnopqr

401 

402 

403def __dir__() -> list[str]: 1abcdefghijklsmnopqr

404 return list(__all__) 1abcdefghijklmnopqr