Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/pydantic/__init__.py: 49%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

57 statements  

1from importlib import import_module 

2from typing import TYPE_CHECKING 

3from warnings import warn 

4 

5from ._migration import getattr_migration 

6from .version import VERSION, _ensure_pydantic_core_version 

7 

8_ensure_pydantic_core_version() 

9del _ensure_pydantic_core_version 

10 

11if TYPE_CHECKING: 

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

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

14 import pydantic_core 

15 from pydantic_core.core_schema import ( 

16 FieldSerializationInfo, 

17 SerializationInfo, 

18 SerializerFunctionWrapHandler, 

19 ValidationInfo, 

20 ValidatorFunctionWrapHandler, 

21 ) 

22 

23 from . import dataclasses 

24 from .aliases import AliasChoices, AliasGenerator, AliasPath 

25 from .annotated_handlers import GetCoreSchemaHandler, GetJsonSchemaHandler 

26 from .config import ConfigDict, with_config 

27 from .errors import * 

28 from .fields import Field, PrivateAttr, computed_field 

29 from .functional_serializers import ( 

30 PlainSerializer, 

31 SerializeAsAny, 

32 WrapSerializer, 

33 field_serializer, 

34 model_serializer, 

35 ) 

36 from .functional_validators import ( 

37 AfterValidator, 

38 BeforeValidator, 

39 InstanceOf, 

40 ModelWrapValidatorHandler, 

41 PlainValidator, 

42 SkipValidation, 

43 ValidateAs, 

44 WrapValidator, 

45 field_validator, 

46 model_validator, 

47 ) 

48 from .json_schema import WithJsonSchema 

49 from .main import * 

50 from .networks import * 

51 from .type_adapter import TypeAdapter 

52 from .types import * 

53 from .validate_call_decorator import validate_call 

54 from .warnings import ( 

55 PydanticDeprecatedSince20, 

56 PydanticDeprecatedSince26, 

57 PydanticDeprecatedSince29, 

58 PydanticDeprecatedSince210, 

59 PydanticDeprecatedSince211, 

60 PydanticDeprecatedSince212, 

61 PydanticDeprecatedSince214, 

62 PydanticDeprecationWarning, 

63 PydanticExperimentalWarning, 

64 ) 

65 

66 MISSING = pydantic_core.MISSING 

67 ValidationError = pydantic_core.ValidationError 

68 from .deprecated.class_validators import root_validator, validator 

69 from .deprecated.config import BaseConfig, Extra 

70 from .deprecated.tools import * 

71 from .root_model import RootModel 

72 

73__version__ = VERSION 

74__all__ = ( 

75 # dataclasses 

76 'dataclasses', 

77 # functional validators 

78 'field_validator', 

79 'model_validator', 

80 'AfterValidator', 

81 'BeforeValidator', 

82 'PlainValidator', 

83 'WrapValidator', 

84 'SkipValidation', 

85 'ValidateAs', 

86 'InstanceOf', 

87 'ModelWrapValidatorHandler', 

88 # JSON Schema 

89 'WithJsonSchema', 

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

91 'root_validator', 

92 'validator', 

93 # functional serializers 

94 'field_serializer', 

95 'model_serializer', 

96 'PlainSerializer', 

97 'SerializeAsAny', 

98 'WrapSerializer', 

99 # config 

100 'ConfigDict', 

101 'with_config', 

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

103 'BaseConfig', 

104 'Extra', 

105 # validate_call 

106 'validate_call', 

107 # errors 

108 'PydanticErrorCodes', 

109 'PydanticUserError', 

110 'PydanticSchemaGenerationError', 

111 'PydanticImportError', 

112 'PydanticUndefinedAnnotation', 

113 'PydanticInvalidForJsonSchema', 

114 'PydanticForbiddenQualifier', 

115 # fields 

116 'Field', 

117 'computed_field', 

118 'PrivateAttr', 

119 # alias 

120 'AliasChoices', 

121 'AliasGenerator', 

122 'AliasPath', 

123 # main 

124 'BaseModel', 

125 'create_model', 

126 # network 

127 'AnyUrl', 

128 'AnyHttpUrl', 

129 'FileUrl', 

130 'HttpUrl', 

131 'FtpUrl', 

132 'WebsocketUrl', 

133 'AnyWebsocketUrl', 

134 'UrlConstraints', 

135 'EmailStr', 

136 'NameEmail', 

137 'IPvAnyAddress', 

138 'IPvAnyInterface', 

139 'IPvAnyNetwork', 

140 'PostgresDsn', 

141 'CockroachDsn', 

142 'AmqpDsn', 

143 'RedisDsn', 

144 'MongoDsn', 

145 'KafkaDsn', 

146 'NatsDsn', 

147 'MySQLDsn', 

148 'MariaDBDsn', 

149 'ClickHouseDsn', 

150 'SnowflakeDsn', 

151 'validate_email', 

152 # root_model 

153 'RootModel', 

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

155 'parse_obj_as', 

156 'schema_of', 

157 'schema_json_of', 

158 # types 

159 'Strict', 

160 'StrictStr', 

161 'conbytes', 

162 'conlist', 

163 'conset', 

164 'confrozenset', 

165 'constr', 

166 'StringConstraints', 

167 'ImportString', 

168 'conint', 

169 'PositiveInt', 

170 'NegativeInt', 

171 'NonNegativeInt', 

172 'NonPositiveInt', 

173 'confloat', 

174 'PositiveFloat', 

175 'NegativeFloat', 

176 'NonNegativeFloat', 

177 'NonPositiveFloat', 

178 'FiniteFloat', 

179 'condecimal', 

180 'condate', 

181 'UUID1', 

182 'UUID3', 

183 'UUID4', 

184 'UUID5', 

185 'UUID6', 

186 'UUID7', 

187 'UUID8', 

188 'FilePath', 

189 'DirectoryPath', 

190 'NewPath', 

191 'Json', 

192 'Secret', 

193 'SecretStr', 

194 'SecretBytes', 

195 'SocketPath', 

196 'StrictBool', 

197 'StrictBytes', 

198 'StrictInt', 

199 'StrictFloat', 

200 'PaymentCardNumber', 

201 'ByteSize', 

202 'PastDate', 

203 'FutureDate', 

204 'PastDatetime', 

205 'FutureDatetime', 

206 'AwareDatetime', 

207 'NaiveDatetime', 

208 'AllowInfNan', 

209 'EncoderProtocol', 

210 'EncodedBytes', 

211 'EncodedStr', 

212 'Base64Encoder', 

213 'Base64Bytes', 

214 'Base64Str', 

215 'Base64UrlBytes', 

216 'Base64UrlStr', 

217 'GetPydanticSchema', 

218 'Tag', 

219 'Discriminator', 

220 'JsonValue', 

221 'FailFast', 

222 # type_adapter 

223 'TypeAdapter', 

224 # version 

225 '__version__', 

226 'VERSION', 

227 # warnings 

228 'PydanticDeprecatedSince20', 

229 'PydanticDeprecatedSince26', 

230 'PydanticDeprecatedSince29', 

231 'PydanticDeprecatedSince210', 

232 'PydanticDeprecatedSince211', 

233 'PydanticDeprecatedSince212', 

234 'PydanticDeprecatedSince214', 

235 'PydanticDeprecationWarning', 

236 'PydanticExperimentalWarning', 

237 # annotated handlers 

238 'GetCoreSchemaHandler', 

239 'GetJsonSchemaHandler', 

240 # pydantic_core 

241 'MISSING', 

242 'ValidationError', 

243 'ValidationInfo', 

244 'SerializationInfo', 

245 'ValidatorFunctionWrapHandler', 

246 'FieldSerializationInfo', 

247 'SerializerFunctionWrapHandler', 

248 'OnErrorOmit', 

249) 

250 

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

252_dynamic_imports: 'dict[str, tuple[str, str]]' = { 

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

254 # functional validators 

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

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

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

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

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

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

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

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

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

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

265 # JSON Schema 

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

267 # functional serializers 

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

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

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

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

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

273 # config 

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

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

276 # validate call 

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

278 # errors 

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

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

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

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

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

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

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

286 # fields 

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

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

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

290 # alias 

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

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

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

294 # main 

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

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

297 # network 

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

323 # root_model 

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

325 # types 

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

390 # type_adapter 

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

392 # warnings 

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

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

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

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

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

398 'PydanticDeprecatedSince212': (__spec__.parent, '.warnings'), 

399 'PydanticDeprecatedSince214': (__spec__.parent, '.warnings'), 

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

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

402 # annotated handlers 

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

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

405 # pydantic_core stuff 

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

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

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

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

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

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

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

413 # deprecated, mostly not included in __all__ 

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

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

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

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

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

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

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

421 # deprecated dynamic imports 

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

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

424} 

425_deprecated_dynamic_imports = {'FieldValidationInfo', 'GenerateSchema'} 

426 

427_getattr_migration = getattr_migration(__name__) 

428 

429 

430def __getattr__(attr_name: str) -> object: 

431 if attr_name in _deprecated_dynamic_imports: 

432 from pydantic.warnings import PydanticDeprecatedSince20 

433 

434 warn( 

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

436 PydanticDeprecatedSince20, 

437 stacklevel=2, 

438 ) 

439 

440 dynamic_attr = _dynamic_imports.get(attr_name) 

441 if dynamic_attr is None: 

442 return _getattr_migration(attr_name) 

443 

444 package, module_name = dynamic_attr 

445 

446 if module_name == '__module__': 

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

448 globals()[attr_name] = result 

449 return result 

450 else: 

451 module = import_module(module_name, package=package) 

452 result = getattr(module, attr_name) 

453 g = globals() 

454 for k, (_, v_module_name) in _dynamic_imports.items(): 

455 if v_module_name == module_name and k not in _deprecated_dynamic_imports: 

456 g[k] = getattr(module, k) 

457 return result 

458 

459 

460def __dir__() -> list[str]: 

461 return list(__all__)