Coverage for pydantic/__init__.py: 91.30%

19 statements  

« prev     ^ index     » next       coverage.py v7.5.4, created at 2024-07-03 19:29 +0000

1import typing 1abcdefghijklmnopqrstuvwxyzMNOPQRSTUVABCDEFGHIJKL

2 

3from ._migration import getattr_migration 1abcdefghijklmnopqrstuvwxyzMNOPQRSTUVABCDEFGHIJKL

4from .version import VERSION 1abcdefghijklmnopqrstuvwxyzMNOPQRSTUVABCDEFGHIJKL

5 

6if typing.TYPE_CHECKING: 1abcdefghijklmnopqrstuvwxyzMNOPQRSTUVABCDEFGHIJKL

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

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

9 import pydantic_core 

10 from pydantic_core.core_schema import ( 

11 FieldSerializationInfo, 

12 SerializationInfo, 

13 SerializerFunctionWrapHandler, 

14 ValidationInfo, 

15 ValidatorFunctionWrapHandler, 

16 ) 

17 

18 from . import dataclasses 

19 from ._internal._generate_schema import GenerateSchema as GenerateSchema 

20 from .aliases import AliasChoices, AliasGenerator, AliasPath 

21 from .annotated_handlers import GetCoreSchemaHandler, GetJsonSchemaHandler 

22 from .config import ConfigDict, with_config 

23 from .errors import * 

24 from .fields import Field, PrivateAttr, computed_field 

25 from .functional_serializers import ( 

26 PlainSerializer, 

27 SerializeAsAny, 

28 WrapSerializer, 

29 field_serializer, 

30 model_serializer, 

31 ) 

32 from .functional_validators import ( 

33 AfterValidator, 

34 BeforeValidator, 

35 InstanceOf, 

36 PlainValidator, 

37 SkipValidation, 

38 WrapValidator, 

39 field_validator, 

40 model_validator, 

41 ) 

42 from .json_schema import WithJsonSchema 

43 from .main import * 

44 from .networks import * 

45 from .type_adapter import TypeAdapter 

46 from .types import * 

47 from .validate_call_decorator import validate_call 

48 from .warnings import ( 

49 PydanticDeprecatedSince20, 

50 PydanticDeprecatedSince26, 

51 PydanticDeprecationWarning, 

52 PydanticExperimentalWarning, 

53 ) 

54 

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

56 ValidationError = pydantic_core.ValidationError 

57 from .deprecated.class_validators import root_validator, validator 

58 from .deprecated.config import BaseConfig, Extra 

59 from .deprecated.tools import * 

60 from .root_model import RootModel 

61 

62__version__ = VERSION 1abcdefghijklmnopqrstuvwxyzMNOPQRSTUVABCDEFGHIJKL

63__all__ = ( 1abcdefghijklmnopqrstuvwxyzMNOPQRSTUVABCDEFGHIJKL

64 # dataclasses 

65 'dataclasses', 

66 # functional validators 

67 'field_validator', 

68 'model_validator', 

69 'AfterValidator', 

70 'BeforeValidator', 

71 'PlainValidator', 

72 'WrapValidator', 

73 'SkipValidation', 

74 'InstanceOf', 

75 # JSON Schema 

76 'WithJsonSchema', 

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

78 'root_validator', 

79 'validator', 

80 # functional serializers 

81 'field_serializer', 

82 'model_serializer', 

83 'PlainSerializer', 

84 'SerializeAsAny', 

85 'WrapSerializer', 

86 # config 

87 'ConfigDict', 

88 'with_config', 

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

90 'BaseConfig', 

91 'Extra', 

92 # validate_call 

93 'validate_call', 

94 # errors 

95 'PydanticErrorCodes', 

96 'PydanticUserError', 

97 'PydanticSchemaGenerationError', 

98 'PydanticImportError', 

99 'PydanticUndefinedAnnotation', 

100 'PydanticInvalidForJsonSchema', 

101 # fields 

102 'Field', 

103 'computed_field', 

104 'PrivateAttr', 

105 # alias 

106 'AliasChoices', 

107 'AliasGenerator', 

108 'AliasPath', 

109 # main 

110 'BaseModel', 

111 'create_model', 

112 # network 

113 'AnyUrl', 

114 'AnyHttpUrl', 

115 'FileUrl', 

116 'HttpUrl', 

117 'FtpUrl', 

118 'WebsocketUrl', 

119 'AnyWebsocketUrl', 

120 'UrlConstraints', 

121 'EmailStr', 

122 'NameEmail', 

123 'IPvAnyAddress', 

124 'IPvAnyInterface', 

125 'IPvAnyNetwork', 

126 'PostgresDsn', 

127 'CockroachDsn', 

128 'AmqpDsn', 

129 'RedisDsn', 

130 'MongoDsn', 

131 'KafkaDsn', 

132 'NatsDsn', 

133 'MySQLDsn', 

134 'MariaDBDsn', 

135 'ClickHouseDsn', 

136 'validate_email', 

137 # root_model 

138 'RootModel', 

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

140 'parse_obj_as', 

141 'schema_of', 

142 'schema_json_of', 

143 # types 

144 'Strict', 

145 'StrictStr', 

146 'conbytes', 

147 'conlist', 

148 'conset', 

149 'confrozenset', 

150 'constr', 

151 'StringConstraints', 

152 'ImportString', 

153 'conint', 

154 'PositiveInt', 

155 'NegativeInt', 

156 'NonNegativeInt', 

157 'NonPositiveInt', 

158 'confloat', 

159 'PositiveFloat', 

160 'NegativeFloat', 

161 'NonNegativeFloat', 

162 'NonPositiveFloat', 

163 'FiniteFloat', 

164 'condecimal', 

165 'condate', 

166 'UUID1', 

167 'UUID3', 

168 'UUID4', 

169 'UUID5', 

170 'FilePath', 

171 'DirectoryPath', 

172 'NewPath', 

173 'Json', 

174 'Secret', 

175 'SecretStr', 

176 'SecretBytes', 

177 'StrictBool', 

178 'StrictBytes', 

179 'StrictInt', 

180 'StrictFloat', 

181 'PaymentCardNumber', 

182 'ByteSize', 

183 'PastDate', 

184 'FutureDate', 

185 'PastDatetime', 

186 'FutureDatetime', 

187 'AwareDatetime', 

188 'NaiveDatetime', 

189 'AllowInfNan', 

190 'EncoderProtocol', 

191 'EncodedBytes', 

192 'EncodedStr', 

193 'Base64Encoder', 

194 'Base64Bytes', 

195 'Base64Str', 

196 'Base64UrlBytes', 

197 'Base64UrlStr', 

198 'GetPydanticSchema', 

199 'Tag', 

200 'Discriminator', 

201 'JsonValue', 

202 'FailFast', 

203 # type_adapter 

204 'TypeAdapter', 

205 # version 

206 '__version__', 

207 'VERSION', 

208 # warnings 

209 'PydanticDeprecatedSince20', 

210 'PydanticDeprecatedSince26', 

211 'PydanticDeprecationWarning', 

212 'PydanticExperimentalWarning', 

213 # annotated handlers 

214 'GetCoreSchemaHandler', 

215 'GetJsonSchemaHandler', 

216 # generate schema from ._internal 

217 'GenerateSchema', 

218 # pydantic_core 

219 'ValidationError', 

220 'ValidationInfo', 

221 'SerializationInfo', 

222 'ValidatorFunctionWrapHandler', 

223 'FieldSerializationInfo', 

224 'SerializerFunctionWrapHandler', 

225 'OnErrorOmit', 

226) 

227 

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

229_dynamic_imports: 'dict[str, tuple[str, str]]' = { 1abcdefghijklmnopqrstuvwxyzMNOPQRSTUVABCDEFGHIJKL

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

231 # functional validators 

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

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

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

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

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

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

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

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

240 # JSON Schema 

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

242 # functional serializers 

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

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

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

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

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

248 # config 

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

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

251 # validate call 

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

253 # errors 

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

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

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

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

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

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

260 # fields 

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

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

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

264 # alias 

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

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

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

268 # main 

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

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

271 # network 

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

296 # root_model 

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

298 # types 

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

359 # type_adapter 

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

361 # warnings 

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

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

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

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

366 # annotated handlers 

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

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

369 # generate schema from ._internal 

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

371 # pydantic_core stuff 

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

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

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

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

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

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

378 # deprecated, mostly not included in __all__ 

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

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

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

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

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

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

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

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

387} 

388 

389_getattr_migration = getattr_migration(__name__) 1abcdefghijklmnopqrstuvwxyzMNOPQRSTUVABCDEFGHIJKL

390 

391 

392def __getattr__(attr_name: str) -> object: 1abcdefghijklmnopqrstuvwxyzMNOPQRSTUVABCDEFGHIJKL

393 dynamic_attr = _dynamic_imports.get(attr_name) 1abcdefghijklmnopqrstuvwxyzMNOPQRSTUVABCDEFGHIJKL

394 if dynamic_attr is None: 1abcdefghijklmnopqrstuvwxyzMNOPQRSTUVABCDEFGHIJKL

395 return _getattr_migration(attr_name) 1abcdefghijklmnopqrstuvwxyzMNOPQRSTUVABCDEFGHIJKL

396 

397 package, module_name = dynamic_attr 1abcdefghijklmnopqrstuvwxyzMNOPQRSTUVABCDEFGHIJKL

398 

399 from importlib import import_module 1abcdefghijklmnopqrstuvwxyzMNOPQRSTUVABCDEFGHIJKL

400 

401 if module_name == '__module__': 401 ↛ 402line 401 didn't jump to line 402 because the condition on line 401 was never true1abcdefghijklmnopqrstuvwxyzMNOPQRSTUVABCDEFGHIJKL

402 return import_module(f'.{attr_name}', package=package) 

403 else: 

404 module = import_module(module_name, package=package) 1abcdefghijklmnopqrstuvwxyzMNOPQRSTUVABCDEFGHIJKL

405 return getattr(module, attr_name) 1abcdefghijklmnopqrstuvwxyzMNOPQRSTUVABCDEFGHIJKL

406 

407 

408def __dir__() -> 'list[str]': 1abcdefghijklmnopqrstuvwxyzMNOPQRSTUVABCDEFGHIJKL

409 return list(__all__) 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKL