Coverage for pydantic/__init__.py: 85.00%

30 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-02-13 19:35 +0000

1import typing 1abcdefghijklmnopqrstuvGHIJKLMwxyzABCDEF

2from importlib import import_module 1abcdefghijklmnopqrstuvGHIJKLMwxyzABCDEF

3from warnings import warn 1abcdefghijklmnopqrstuvGHIJKLMwxyzABCDEF

4 

5from ._migration import getattr_migration 1abcdefghijklmnopqrstuvGHIJKLMwxyzABCDEF

6from .version import VERSION 1abcdefghijklmnopqrstuvGHIJKLMwxyzABCDEF

7 

8if typing.TYPE_CHECKING: 1abcdefghijklmnopqrstuvGHIJKLMwxyzABCDEF

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 1abcdefghijklmnopqrstuvGHIJKLMwxyzABCDEF

68__all__ = ( 1abcdefghijklmnopqrstuvGHIJKLMwxyzABCDEF

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 # fields 

108 'Field', 

109 'computed_field', 

110 'PrivateAttr', 

111 # alias 

112 'AliasChoices', 

113 'AliasGenerator', 

114 'AliasPath', 

115 # main 

116 'BaseModel', 

117 'create_model', 

118 # network 

119 'AnyUrl', 

120 'AnyHttpUrl', 

121 'FileUrl', 

122 'HttpUrl', 

123 'FtpUrl', 

124 'WebsocketUrl', 

125 'AnyWebsocketUrl', 

126 'UrlConstraints', 

127 'EmailStr', 

128 'NameEmail', 

129 'IPvAnyAddress', 

130 'IPvAnyInterface', 

131 'IPvAnyNetwork', 

132 'PostgresDsn', 

133 'CockroachDsn', 

134 'AmqpDsn', 

135 'RedisDsn', 

136 'MongoDsn', 

137 'KafkaDsn', 

138 'NatsDsn', 

139 'MySQLDsn', 

140 'MariaDBDsn', 

141 'ClickHouseDsn', 

142 'SnowflakeDsn', 

143 'validate_email', 

144 # root_model 

145 'RootModel', 

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

147 'parse_obj_as', 

148 'schema_of', 

149 'schema_json_of', 

150 # types 

151 'Strict', 

152 'StrictStr', 

153 'conbytes', 

154 'conlist', 

155 'conset', 

156 'confrozenset', 

157 'constr', 

158 'StringConstraints', 

159 'ImportString', 

160 'conint', 

161 'PositiveInt', 

162 'NegativeInt', 

163 'NonNegativeInt', 

164 'NonPositiveInt', 

165 'confloat', 

166 'PositiveFloat', 

167 'NegativeFloat', 

168 'NonNegativeFloat', 

169 'NonPositiveFloat', 

170 'FiniteFloat', 

171 'condecimal', 

172 'condate', 

173 'UUID1', 

174 'UUID3', 

175 'UUID4', 

176 'UUID5', 

177 'FilePath', 

178 'DirectoryPath', 

179 'NewPath', 

180 'Json', 

181 'Secret', 

182 'SecretStr', 

183 'SecretBytes', 

184 'SocketPath', 

185 'StrictBool', 

186 'StrictBytes', 

187 'StrictInt', 

188 'StrictFloat', 

189 'PaymentCardNumber', 

190 'ByteSize', 

191 'PastDate', 

192 'FutureDate', 

193 'PastDatetime', 

194 'FutureDatetime', 

195 'AwareDatetime', 

196 'NaiveDatetime', 

197 'AllowInfNan', 

198 'EncoderProtocol', 

199 'EncodedBytes', 

200 'EncodedStr', 

201 'Base64Encoder', 

202 'Base64Bytes', 

203 'Base64Str', 

204 'Base64UrlBytes', 

205 'Base64UrlStr', 

206 'GetPydanticSchema', 

207 'Tag', 

208 'Discriminator', 

209 'JsonValue', 

210 'FailFast', 

211 # type_adapter 

212 'TypeAdapter', 

213 # version 

214 '__version__', 

215 'VERSION', 

216 # warnings 

217 'PydanticDeprecatedSince20', 

218 'PydanticDeprecatedSince26', 

219 'PydanticDeprecatedSince29', 

220 'PydanticDeprecatedSince210', 

221 'PydanticDeprecatedSince211', 

222 'PydanticDeprecationWarning', 

223 'PydanticExperimentalWarning', 

224 # annotated handlers 

225 'GetCoreSchemaHandler', 

226 'GetJsonSchemaHandler', 

227 # pydantic_core 

228 'ValidationError', 

229 'ValidationInfo', 

230 'SerializationInfo', 

231 'ValidatorFunctionWrapHandler', 

232 'FieldSerializationInfo', 

233 'SerializerFunctionWrapHandler', 

234 'OnErrorOmit', 

235) 

236 

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

238_dynamic_imports: 'dict[str, tuple[str, str]]' = { 1abcdefghijklmnopqrstuvGHIJKLMwxyzABCDEF

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

240 # functional validators 

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

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

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

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

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

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

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

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

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

250 # JSON Schema 

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

252 # functional serializers 

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

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

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

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

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

258 # config 

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

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

261 # validate call 

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

263 # errors 

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

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

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

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

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

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

270 # fields 

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

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

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

274 # alias 

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

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

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

278 # main 

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

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

281 # network 

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

307 # root_model 

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

309 # types 

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

371 # type_adapter 

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

373 # warnings 

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

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

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

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

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

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

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

381 # annotated handlers 

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

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

384 # pydantic_core stuff 

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

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

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

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

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

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

391 # deprecated, mostly not included in __all__ 

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

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

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

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

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

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

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

399 # deprecated dynamic imports 

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

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

402} 

403_deprecated_dynamic_imports = {'FieldValidationInfo', 'GenerateSchema'} 1abcdefghijklmnopqrstuvGHIJKLMwxyzABCDEF

404 

405_getattr_migration = getattr_migration(__name__) 1abcdefghijklmnopqrstuvGHIJKLMwxyzABCDEF

406 

407 

408def __getattr__(attr_name: str) -> object: 1abcdefghijklmnopqrstuvGHIJKLMwxyzABCDEF

409 if attr_name in _deprecated_dynamic_imports: 409 ↛ 410line 409 didn't jump to line 410 because the condition on line 409 was never true1abcdefghijklmnopqrstuvGHIJKLMwxyzABCDEF

410 warn( 

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

412 DeprecationWarning, 

413 stacklevel=2, 

414 ) 

415 

416 dynamic_attr = _dynamic_imports.get(attr_name) 1abcdefghijklmnopqrstuvGHIJKLMwxyzABCDEF

417 if dynamic_attr is None: 1abcdefghijklmnopqrstuvGHIJKLMwxyzABCDEF

418 return _getattr_migration(attr_name) 1abcdefghijklmnopqrstuvGHIJKLMwxyzABCDEF

419 

420 package, module_name = dynamic_attr 1abcdefghijklmnopqrstuvGHIJKLMwxyzABCDEF

421 

422 if module_name == '__module__': 422 ↛ 423line 422 didn't jump to line 423 because the condition on line 422 was never true1abcdefghijklmnopqrstuvGHIJKLMwxyzABCDEF

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

424 globals()[attr_name] = result 

425 return result 

426 else: 

427 module = import_module(module_name, package=package) 1abcdefghijklmnopqrstuvGHIJKLMwxyzABCDEF

428 result = getattr(module, attr_name) 1abcdefghijklmnopqrstuvGHIJKLMwxyzABCDEF

429 g = globals() 1abcdefghijklmnopqrstuvGHIJKLMwxyzABCDEF

430 for k, (_, v_module_name) in _dynamic_imports.items(): 1abcdefghijklmnopqrstuvGHIJKLMwxyzABCDEF

431 if v_module_name == module_name and k not in _deprecated_dynamic_imports: 1abcdefghijklmnopqrstuvGHIJKLMwxyzABCDEF

432 g[k] = getattr(module, k) 1abcdefghijklmnopqrstuvGHIJKLMwxyzABCDEF

433 return result 1abcdefghijklmnopqrstuvGHIJKLMwxyzABCDEF

434 

435 

436def __dir__() -> 'list[str]': 1abcdefghijklmnopqrstuvGHIJKLMwxyzABCDEF

437 return list(__all__) 1abcdefghijklmnopqrstuvwxyzABCDEF