Coverage for pydantic/__init__.py: 85.00%

30 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-05-01 08:13 +0000

1import typing 1abcdefghijklmnopqrstuvwxJKLMNOPyzABCDEFGHI

2from importlib import import_module 1abcdefghijklmnopqrstuvwxJKLMNOPyzABCDEFGHI

3from warnings import warn 1abcdefghijklmnopqrstuvwxJKLMNOPyzABCDEFGHI

4 

5from ._migration import getattr_migration 1abcdefghijklmnopqrstuvwxJKLMNOPyzABCDEFGHI

6from .version import VERSION 1abcdefghijklmnopqrstuvwxJKLMNOPyzABCDEFGHI

7 

8if typing.TYPE_CHECKING: 1abcdefghijklmnopqrstuvwxJKLMNOPyzABCDEFGHI

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

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

11 import pydantic_core 

12 from pydantic_core.core_schema import ( 

13 FieldSerializationInfo, 

14 SerializationInfo, 

15 SerializerFunctionWrapHandler, 

16 ValidationInfo, 

17 ValidatorFunctionWrapHandler, 

18 ) 

19 

20 from . import dataclasses 

21 from .aliases import AliasChoices, AliasGenerator, AliasPath 

22 from .annotated_handlers import GetCoreSchemaHandler, GetJsonSchemaHandler 

23 from .config import ConfigDict, with_config 

24 from .errors import * 

25 from .fields import Field, PrivateAttr, computed_field 

26 from .functional_serializers import ( 

27 PlainSerializer, 

28 SerializeAsAny, 

29 WrapSerializer, 

30 field_serializer, 

31 model_serializer, 

32 ) 

33 from .functional_validators import ( 

34 AfterValidator, 

35 BeforeValidator, 

36 InstanceOf, 

37 ModelWrapValidatorHandler, 

38 PlainValidator, 

39 SkipValidation, 

40 WrapValidator, 

41 field_validator, 

42 model_validator, 

43 ) 

44 from .json_schema import WithJsonSchema 

45 from .main import * 

46 from .networks import * 

47 from .type_adapter import TypeAdapter 

48 from .types import * 

49 from .validate_call_decorator import validate_call 

50 from .warnings import ( 

51 PydanticDeprecatedSince20, 

52 PydanticDeprecatedSince26, 

53 PydanticDeprecatedSince29, 

54 PydanticDeprecatedSince210, 

55 PydanticDeprecatedSince211, 

56 PydanticDeprecationWarning, 

57 PydanticExperimentalWarning, 

58 ) 

59 

60 # this encourages pycharm to import `ValidationError` from here, not pydantic_core 

61 ValidationError = pydantic_core.ValidationError 

62 from .deprecated.class_validators import root_validator, validator 

63 from .deprecated.config import BaseConfig, Extra 

64 from .deprecated.tools import * 

65 from .root_model import RootModel 

66 

67__version__ = VERSION 1abcdefghijklmnopqrstuvwxJKLMNOPyzABCDEFGHI

68__all__ = ( 1abcdefghijklmnopqrstuvwxJKLMNOPyzABCDEFGHI

69 # dataclasses 

70 'dataclasses', 

71 # functional validators 

72 'field_validator', 

73 'model_validator', 

74 'AfterValidator', 

75 'BeforeValidator', 

76 'PlainValidator', 

77 'WrapValidator', 

78 'SkipValidation', 

79 'InstanceOf', 

80 'ModelWrapValidatorHandler', 

81 # JSON Schema 

82 'WithJsonSchema', 

83 # deprecated V1 functional validators, these are imported via `__getattr__` below 

84 'root_validator', 

85 'validator', 

86 # functional serializers 

87 'field_serializer', 

88 'model_serializer', 

89 'PlainSerializer', 

90 'SerializeAsAny', 

91 'WrapSerializer', 

92 # config 

93 'ConfigDict', 

94 'with_config', 

95 # deprecated V1 config, these are imported via `__getattr__` below 

96 'BaseConfig', 

97 'Extra', 

98 # validate_call 

99 'validate_call', 

100 # errors 

101 'PydanticErrorCodes', 

102 'PydanticUserError', 

103 'PydanticSchemaGenerationError', 

104 'PydanticImportError', 

105 'PydanticUndefinedAnnotation', 

106 'PydanticInvalidForJsonSchema', 

107 'PydanticForbiddenQualifier', 

108 # fields 

109 'Field', 

110 'computed_field', 

111 'PrivateAttr', 

112 # alias 

113 'AliasChoices', 

114 'AliasGenerator', 

115 'AliasPath', 

116 # main 

117 'BaseModel', 

118 'create_model', 

119 # network 

120 'AnyUrl', 

121 'AnyHttpUrl', 

122 'FileUrl', 

123 'HttpUrl', 

124 'FtpUrl', 

125 'WebsocketUrl', 

126 'AnyWebsocketUrl', 

127 'UrlConstraints', 

128 'EmailStr', 

129 'NameEmail', 

130 'IPvAnyAddress', 

131 'IPvAnyInterface', 

132 'IPvAnyNetwork', 

133 'PostgresDsn', 

134 'CockroachDsn', 

135 'AmqpDsn', 

136 'RedisDsn', 

137 'MongoDsn', 

138 'KafkaDsn', 

139 'NatsDsn', 

140 'MySQLDsn', 

141 'MariaDBDsn', 

142 'ClickHouseDsn', 

143 'SnowflakeDsn', 

144 'validate_email', 

145 # root_model 

146 'RootModel', 

147 # deprecated tools, these are imported via `__getattr__` below 

148 'parse_obj_as', 

149 'schema_of', 

150 'schema_json_of', 

151 # types 

152 'Strict', 

153 'StrictStr', 

154 'conbytes', 

155 'conlist', 

156 'conset', 

157 'confrozenset', 

158 'constr', 

159 'StringConstraints', 

160 'ImportString', 

161 'conint', 

162 'PositiveInt', 

163 'NegativeInt', 

164 'NonNegativeInt', 

165 'NonPositiveInt', 

166 'confloat', 

167 'PositiveFloat', 

168 'NegativeFloat', 

169 'NonNegativeFloat', 

170 'NonPositiveFloat', 

171 'FiniteFloat', 

172 'condecimal', 

173 'condate', 

174 'UUID1', 

175 'UUID3', 

176 'UUID4', 

177 'UUID5', 

178 'UUID6', 

179 'UUID7', 

180 'UUID8', 

181 'FilePath', 

182 'DirectoryPath', 

183 'NewPath', 

184 'Json', 

185 'Secret', 

186 'SecretStr', 

187 'SecretBytes', 

188 'SocketPath', 

189 'StrictBool', 

190 'StrictBytes', 

191 'StrictInt', 

192 'StrictFloat', 

193 'PaymentCardNumber', 

194 'ByteSize', 

195 'PastDate', 

196 'FutureDate', 

197 'PastDatetime', 

198 'FutureDatetime', 

199 'AwareDatetime', 

200 'NaiveDatetime', 

201 'AllowInfNan', 

202 'EncoderProtocol', 

203 'EncodedBytes', 

204 'EncodedStr', 

205 'Base64Encoder', 

206 'Base64Bytes', 

207 'Base64Str', 

208 'Base64UrlBytes', 

209 'Base64UrlStr', 

210 'GetPydanticSchema', 

211 'Tag', 

212 'Discriminator', 

213 'JsonValue', 

214 'FailFast', 

215 # type_adapter 

216 'TypeAdapter', 

217 # version 

218 '__version__', 

219 'VERSION', 

220 # warnings 

221 'PydanticDeprecatedSince20', 

222 'PydanticDeprecatedSince26', 

223 'PydanticDeprecatedSince29', 

224 'PydanticDeprecatedSince210', 

225 'PydanticDeprecatedSince211', 

226 'PydanticDeprecationWarning', 

227 'PydanticExperimentalWarning', 

228 # annotated handlers 

229 'GetCoreSchemaHandler', 

230 'GetJsonSchemaHandler', 

231 # pydantic_core 

232 'ValidationError', 

233 'ValidationInfo', 

234 'SerializationInfo', 

235 'ValidatorFunctionWrapHandler', 

236 'FieldSerializationInfo', 

237 'SerializerFunctionWrapHandler', 

238 'OnErrorOmit', 

239) 

240 

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

242_dynamic_imports: 'dict[str, tuple[str, str]]' = { 1abcdefghijklmnopqrstuvwxJKLMNOPyzABCDEFGHI

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

244 # functional validators 

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

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

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

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

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

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

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

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

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

254 # JSON Schema 

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

256 # functional serializers 

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

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

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

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

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

262 # config 

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

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

265 # validate call 

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

267 # errors 

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

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

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

271 'PydanticImportError': (__spec__.parent, '.errors'), 

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

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

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

275 # fields 

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

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

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

279 # alias 

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

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

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

283 # main 

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

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

286 # network 

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

312 # root_model 

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

314 # types 

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

379 # type_adapter 

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

381 # warnings 

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

383 'PydanticDeprecatedSince26': (__spec__.parent, '.warnings'), 

384 'PydanticDeprecatedSince29': (__spec__.parent, '.warnings'), 

385 'PydanticDeprecatedSince210': (__spec__.parent, '.warnings'), 

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

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

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

389 # annotated handlers 

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

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

392 # pydantic_core stuff 

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

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

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

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

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

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

399 # deprecated, mostly not included in __all__ 

400 'root_validator': (__spec__.parent, '.deprecated.class_validators'), 

401 'validator': (__spec__.parent, '.deprecated.class_validators'), 

402 'BaseConfig': (__spec__.parent, '.deprecated.config'), 

403 'Extra': (__spec__.parent, '.deprecated.config'), 

404 'parse_obj_as': (__spec__.parent, '.deprecated.tools'), 

405 'schema_of': (__spec__.parent, '.deprecated.tools'), 

406 'schema_json_of': (__spec__.parent, '.deprecated.tools'), 

407 # deprecated dynamic imports 

408 'FieldValidationInfo': ('pydantic_core', '.core_schema'), 

409 'GenerateSchema': (__spec__.parent, '._internal._generate_schema'), 

410} 

411_deprecated_dynamic_imports = {'FieldValidationInfo', 'GenerateSchema'} 1abcdefghijklmnopqrstuvwxJKLMNOPyzABCDEFGHI

412 

413_getattr_migration = getattr_migration(__name__) 1abcdefghijklmnopqrstuvwxJKLMNOPyzABCDEFGHI

414 

415 

416def __getattr__(attr_name: str) -> object: 1abcdefghijklmnopqrstuvwxJKLMNOPyzABCDEFGHI

417 if attr_name in _deprecated_dynamic_imports: 417 ↛ 418line 417 didn't jump to line 418 because the condition on line 417 was never true1abcdefghijklmnopqrstuvwxJKLMNOPyzABCDEFGHI

418 warn( 

419 f'Importing {attr_name} from `pydantic` is deprecated. This feature is either no longer supported, or is not public.', 

420 DeprecationWarning, 

421 stacklevel=2, 

422 ) 

423 

424 dynamic_attr = _dynamic_imports.get(attr_name) 1abcdefghijklmnopqrstuvwxJKLMNOPyzABCDEFGHI

425 if dynamic_attr is None: 1abcdefghijklmnopqrstuvwxJKLMNOPyzABCDEFGHI

426 return _getattr_migration(attr_name) 1abcdefghijklmnopqrstuvwxJKLMNOPyzABCDEFGHI

427 

428 package, module_name = dynamic_attr 1abcdefghijklmnopqrstuvwxJKLMNOPyzABCDEFGHI

429 

430 if module_name == '__module__': 430 ↛ 431line 430 didn't jump to line 431 because the condition on line 430 was never true1abcdefghijklmnopqrstuvwxJKLMNOPyzABCDEFGHI

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

432 globals()[attr_name] = result 

433 return result 

434 else: 

435 module = import_module(module_name, package=package) 1abcdefghijklmnopqrstuvwxJKLMNOPyzABCDEFGHI

436 result = getattr(module, attr_name) 1abcdefghijklmnopqrstuvwxJKLMNOPyzABCDEFGHI

437 g = globals() 1abcdefghijklmnopqrstuvwxJKLMNOPyzABCDEFGHI

438 for k, (_, v_module_name) in _dynamic_imports.items(): 1abcdefghijklmnopqrstuvwxJKLMNOPyzABCDEFGHI

439 if v_module_name == module_name and k not in _deprecated_dynamic_imports: 1abcdefghijklmnopqrstuvwxJKLMNOPyzABCDEFGHI

440 g[k] = getattr(module, k) 1abcdefghijklmnopqrstuvwxJKLMNOPyzABCDEFGHI

441 return result 1abcdefghijklmnopqrstuvwxJKLMNOPyzABCDEFGHI

442 

443 

444def __dir__() -> 'list[str]': 1abcdefghijklmnopqrstuvwxJKLMNOPyzABCDEFGHI

445 return list(__all__) 1abcdefghijklmnopqrstuvwxyzABCDEFGHI