__init__.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. import typing
  2. from ._migration import getattr_migration
  3. from .version import VERSION
  4. if typing.TYPE_CHECKING:
  5. # import of virtually everything is supported via `__getattr__` below,
  6. # but we need them here for type checking and IDE support
  7. import pydantic_core
  8. from pydantic_core.core_schema import (
  9. FieldSerializationInfo,
  10. SerializationInfo,
  11. SerializerFunctionWrapHandler,
  12. ValidationInfo,
  13. ValidatorFunctionWrapHandler,
  14. )
  15. from . import dataclasses
  16. from ._internal._generate_schema import GenerateSchema as GenerateSchema
  17. from .annotated_handlers import GetCoreSchemaHandler, GetJsonSchemaHandler
  18. from .config import ConfigDict
  19. from .errors import *
  20. from .fields import AliasChoices, AliasPath, Field, PrivateAttr, computed_field
  21. from .functional_serializers import (
  22. PlainSerializer,
  23. SerializeAsAny,
  24. WrapSerializer,
  25. field_serializer,
  26. model_serializer,
  27. )
  28. from .functional_validators import (
  29. AfterValidator,
  30. BeforeValidator,
  31. InstanceOf,
  32. PlainValidator,
  33. SkipValidation,
  34. WrapValidator,
  35. field_validator,
  36. model_validator,
  37. )
  38. from .json_schema import WithJsonSchema
  39. from .main import *
  40. from .networks import *
  41. from .type_adapter import TypeAdapter
  42. from .types import *
  43. from .validate_call_decorator import validate_call
  44. from .warnings import PydanticDeprecatedSince20, PydanticDeprecationWarning
  45. # this encourages pycharm to import `ValidationError` from here, not pydantic_core
  46. ValidationError = pydantic_core.ValidationError
  47. from .deprecated.class_validators import root_validator, validator
  48. from .deprecated.config import BaseConfig, Extra
  49. from .deprecated.tools import *
  50. from .root_model import RootModel
  51. __version__ = VERSION
  52. __all__ = (
  53. # dataclasses
  54. 'dataclasses',
  55. # functional validators
  56. 'field_validator',
  57. 'model_validator',
  58. 'AfterValidator',
  59. 'BeforeValidator',
  60. 'PlainValidator',
  61. 'WrapValidator',
  62. 'SkipValidation',
  63. 'InstanceOf',
  64. # JSON Schema
  65. 'WithJsonSchema',
  66. # deprecated V1 functional validators, these are imported via `__getattr__` below
  67. 'root_validator',
  68. 'validator',
  69. # functional serializers
  70. 'field_serializer',
  71. 'model_serializer',
  72. 'PlainSerializer',
  73. 'SerializeAsAny',
  74. 'WrapSerializer',
  75. # config
  76. 'ConfigDict',
  77. # deprecated V1 config, these are imported via `__getattr__` below
  78. 'BaseConfig',
  79. 'Extra',
  80. # validate_call
  81. 'validate_call',
  82. # errors
  83. 'PydanticErrorCodes',
  84. 'PydanticUserError',
  85. 'PydanticSchemaGenerationError',
  86. 'PydanticImportError',
  87. 'PydanticUndefinedAnnotation',
  88. 'PydanticInvalidForJsonSchema',
  89. # fields
  90. 'AliasPath',
  91. 'AliasChoices',
  92. 'Field',
  93. 'computed_field',
  94. 'PrivateAttr',
  95. # main
  96. 'BaseModel',
  97. 'create_model',
  98. # network
  99. 'AnyUrl',
  100. 'AnyHttpUrl',
  101. 'FileUrl',
  102. 'HttpUrl',
  103. 'UrlConstraints',
  104. 'EmailStr',
  105. 'NameEmail',
  106. 'IPvAnyAddress',
  107. 'IPvAnyInterface',
  108. 'IPvAnyNetwork',
  109. 'PostgresDsn',
  110. 'CockroachDsn',
  111. 'AmqpDsn',
  112. 'RedisDsn',
  113. 'MongoDsn',
  114. 'KafkaDsn',
  115. 'MySQLDsn',
  116. 'MariaDBDsn',
  117. 'validate_email',
  118. # root_model
  119. 'RootModel',
  120. # deprecated tools, these are imported via `__getattr__` below
  121. 'parse_obj_as',
  122. 'schema_of',
  123. 'schema_json_of',
  124. # types
  125. 'Strict',
  126. 'StrictStr',
  127. 'conbytes',
  128. 'conlist',
  129. 'conset',
  130. 'confrozenset',
  131. 'constr',
  132. 'StringConstraints',
  133. 'ImportString',
  134. 'conint',
  135. 'PositiveInt',
  136. 'NegativeInt',
  137. 'NonNegativeInt',
  138. 'NonPositiveInt',
  139. 'confloat',
  140. 'PositiveFloat',
  141. 'NegativeFloat',
  142. 'NonNegativeFloat',
  143. 'NonPositiveFloat',
  144. 'FiniteFloat',
  145. 'condecimal',
  146. 'condate',
  147. 'UUID1',
  148. 'UUID3',
  149. 'UUID4',
  150. 'UUID5',
  151. 'FilePath',
  152. 'DirectoryPath',
  153. 'NewPath',
  154. 'Json',
  155. 'SecretStr',
  156. 'SecretBytes',
  157. 'StrictBool',
  158. 'StrictBytes',
  159. 'StrictInt',
  160. 'StrictFloat',
  161. 'PaymentCardNumber',
  162. 'ByteSize',
  163. 'PastDate',
  164. 'FutureDate',
  165. 'PastDatetime',
  166. 'FutureDatetime',
  167. 'AwareDatetime',
  168. 'NaiveDatetime',
  169. 'AllowInfNan',
  170. 'EncoderProtocol',
  171. 'EncodedBytes',
  172. 'EncodedStr',
  173. 'Base64Encoder',
  174. 'Base64Bytes',
  175. 'Base64Str',
  176. 'Base64UrlBytes',
  177. 'Base64UrlStr',
  178. 'GetPydanticSchema',
  179. 'Tag',
  180. 'Discriminator',
  181. 'JsonValue',
  182. # type_adapter
  183. 'TypeAdapter',
  184. # version
  185. '__version__',
  186. 'VERSION',
  187. # warnings
  188. 'PydanticDeprecatedSince20',
  189. 'PydanticDeprecationWarning',
  190. # annotated handlers
  191. 'GetCoreSchemaHandler',
  192. 'GetJsonSchemaHandler',
  193. # generate schema from ._internal
  194. 'GenerateSchema',
  195. # pydantic_core
  196. 'ValidationError',
  197. 'ValidationInfo',
  198. 'SerializationInfo',
  199. 'ValidatorFunctionWrapHandler',
  200. 'FieldSerializationInfo',
  201. 'SerializerFunctionWrapHandler',
  202. )
  203. # A mapping of {<member name>: (package, <module name>)} defining dynamic imports
  204. _dynamic_imports: 'dict[str, tuple[str, str]]' = {
  205. 'dataclasses': (__package__, '__module__'),
  206. # functional validators
  207. 'field_validator': (__package__, '.functional_validators'),
  208. 'model_validator': (__package__, '.functional_validators'),
  209. 'AfterValidator': (__package__, '.functional_validators'),
  210. 'BeforeValidator': (__package__, '.functional_validators'),
  211. 'PlainValidator': (__package__, '.functional_validators'),
  212. 'WrapValidator': (__package__, '.functional_validators'),
  213. 'SkipValidation': (__package__, '.functional_validators'),
  214. 'InstanceOf': (__package__, '.functional_validators'),
  215. # JSON Schema
  216. 'WithJsonSchema': (__package__, '.json_schema'),
  217. # functional serializers
  218. 'field_serializer': (__package__, '.functional_serializers'),
  219. 'model_serializer': (__package__, '.functional_serializers'),
  220. 'PlainSerializer': (__package__, '.functional_serializers'),
  221. 'SerializeAsAny': (__package__, '.functional_serializers'),
  222. 'WrapSerializer': (__package__, '.functional_serializers'),
  223. # config
  224. 'ConfigDict': (__package__, '.config'),
  225. # validate call
  226. 'validate_call': (__package__, '.validate_call_decorator'),
  227. # errors
  228. 'PydanticErrorCodes': (__package__, '.errors'),
  229. 'PydanticUserError': (__package__, '.errors'),
  230. 'PydanticSchemaGenerationError': (__package__, '.errors'),
  231. 'PydanticImportError': (__package__, '.errors'),
  232. 'PydanticUndefinedAnnotation': (__package__, '.errors'),
  233. 'PydanticInvalidForJsonSchema': (__package__, '.errors'),
  234. # fields
  235. 'AliasPath': (__package__, '.fields'),
  236. 'AliasChoices': (__package__, '.fields'),
  237. 'Field': (__package__, '.fields'),
  238. 'computed_field': (__package__, '.fields'),
  239. 'PrivateAttr': (__package__, '.fields'),
  240. # main
  241. 'BaseModel': (__package__, '.main'),
  242. 'create_model': (__package__, '.main'),
  243. # network
  244. 'AnyUrl': (__package__, '.networks'),
  245. 'AnyHttpUrl': (__package__, '.networks'),
  246. 'FileUrl': (__package__, '.networks'),
  247. 'HttpUrl': (__package__, '.networks'),
  248. 'UrlConstraints': (__package__, '.networks'),
  249. 'EmailStr': (__package__, '.networks'),
  250. 'NameEmail': (__package__, '.networks'),
  251. 'IPvAnyAddress': (__package__, '.networks'),
  252. 'IPvAnyInterface': (__package__, '.networks'),
  253. 'IPvAnyNetwork': (__package__, '.networks'),
  254. 'PostgresDsn': (__package__, '.networks'),
  255. 'CockroachDsn': (__package__, '.networks'),
  256. 'AmqpDsn': (__package__, '.networks'),
  257. 'RedisDsn': (__package__, '.networks'),
  258. 'MongoDsn': (__package__, '.networks'),
  259. 'KafkaDsn': (__package__, '.networks'),
  260. 'MySQLDsn': (__package__, '.networks'),
  261. 'MariaDBDsn': (__package__, '.networks'),
  262. 'validate_email': (__package__, '.networks'),
  263. # root_model
  264. 'RootModel': (__package__, '.root_model'),
  265. # types
  266. 'Strict': (__package__, '.types'),
  267. 'StrictStr': (__package__, '.types'),
  268. 'conbytes': (__package__, '.types'),
  269. 'conlist': (__package__, '.types'),
  270. 'conset': (__package__, '.types'),
  271. 'confrozenset': (__package__, '.types'),
  272. 'constr': (__package__, '.types'),
  273. 'StringConstraints': (__package__, '.types'),
  274. 'ImportString': (__package__, '.types'),
  275. 'conint': (__package__, '.types'),
  276. 'PositiveInt': (__package__, '.types'),
  277. 'NegativeInt': (__package__, '.types'),
  278. 'NonNegativeInt': (__package__, '.types'),
  279. 'NonPositiveInt': (__package__, '.types'),
  280. 'confloat': (__package__, '.types'),
  281. 'PositiveFloat': (__package__, '.types'),
  282. 'NegativeFloat': (__package__, '.types'),
  283. 'NonNegativeFloat': (__package__, '.types'),
  284. 'NonPositiveFloat': (__package__, '.types'),
  285. 'FiniteFloat': (__package__, '.types'),
  286. 'condecimal': (__package__, '.types'),
  287. 'condate': (__package__, '.types'),
  288. 'UUID1': (__package__, '.types'),
  289. 'UUID3': (__package__, '.types'),
  290. 'UUID4': (__package__, '.types'),
  291. 'UUID5': (__package__, '.types'),
  292. 'FilePath': (__package__, '.types'),
  293. 'DirectoryPath': (__package__, '.types'),
  294. 'NewPath': (__package__, '.types'),
  295. 'Json': (__package__, '.types'),
  296. 'SecretStr': (__package__, '.types'),
  297. 'SecretBytes': (__package__, '.types'),
  298. 'StrictBool': (__package__, '.types'),
  299. 'StrictBytes': (__package__, '.types'),
  300. 'StrictInt': (__package__, '.types'),
  301. 'StrictFloat': (__package__, '.types'),
  302. 'PaymentCardNumber': (__package__, '.types'),
  303. 'ByteSize': (__package__, '.types'),
  304. 'PastDate': (__package__, '.types'),
  305. 'FutureDate': (__package__, '.types'),
  306. 'PastDatetime': (__package__, '.types'),
  307. 'FutureDatetime': (__package__, '.types'),
  308. 'AwareDatetime': (__package__, '.types'),
  309. 'NaiveDatetime': (__package__, '.types'),
  310. 'AllowInfNan': (__package__, '.types'),
  311. 'EncoderProtocol': (__package__, '.types'),
  312. 'EncodedBytes': (__package__, '.types'),
  313. 'EncodedStr': (__package__, '.types'),
  314. 'Base64Encoder': (__package__, '.types'),
  315. 'Base64Bytes': (__package__, '.types'),
  316. 'Base64Str': (__package__, '.types'),
  317. 'Base64UrlBytes': (__package__, '.types'),
  318. 'Base64UrlStr': (__package__, '.types'),
  319. 'GetPydanticSchema': (__package__, '.types'),
  320. 'Tag': (__package__, '.types'),
  321. 'Discriminator': (__package__, '.types'),
  322. 'JsonValue': (__package__, '.types'),
  323. # type_adapter
  324. 'TypeAdapter': (__package__, '.type_adapter'),
  325. # warnings
  326. 'PydanticDeprecatedSince20': (__package__, '.warnings'),
  327. 'PydanticDeprecationWarning': (__package__, '.warnings'),
  328. # annotated handlers
  329. 'GetCoreSchemaHandler': (__package__, '.annotated_handlers'),
  330. 'GetJsonSchemaHandler': (__package__, '.annotated_handlers'),
  331. # generate schema from ._internal
  332. 'GenerateSchema': (__package__, '._internal._generate_schema'),
  333. # pydantic_core stuff
  334. 'ValidationError': ('pydantic_core', '.'),
  335. 'ValidationInfo': ('pydantic_core', '.core_schema'),
  336. 'SerializationInfo': ('pydantic_core', '.core_schema'),
  337. 'ValidatorFunctionWrapHandler': ('pydantic_core', '.core_schema'),
  338. 'FieldSerializationInfo': ('pydantic_core', '.core_schema'),
  339. 'SerializerFunctionWrapHandler': ('pydantic_core', '.core_schema'),
  340. # deprecated, mostly not included in __all__
  341. 'root_validator': (__package__, '.deprecated.class_validators'),
  342. 'validator': (__package__, '.deprecated.class_validators'),
  343. 'BaseConfig': (__package__, '.deprecated.config'),
  344. 'Extra': (__package__, '.deprecated.config'),
  345. 'parse_obj_as': (__package__, '.deprecated.tools'),
  346. 'schema_of': (__package__, '.deprecated.tools'),
  347. 'schema_json_of': (__package__, '.deprecated.tools'),
  348. 'FieldValidationInfo': ('pydantic_core', '.core_schema'),
  349. }
  350. _getattr_migration = getattr_migration(__name__)
  351. def __getattr__(attr_name: str) -> object:
  352. dynamic_attr = _dynamic_imports.get(attr_name)
  353. if dynamic_attr is None:
  354. return _getattr_migration(attr_name)
  355. package, module_name = dynamic_attr
  356. from importlib import import_module
  357. if module_name == '__module__':
  358. return import_module(f'.{attr_name}', package=package)
  359. else:
  360. module = import_module(module_name, package=package)
  361. return getattr(module, attr_name)
  362. def __dir__() -> 'list[str]':
  363. return list(__all__)