25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

309 satır
12 KiB

  1. import sys
  2. from typing import Any, Callable
  3. from .version import version_short
  4. MOVED_IN_V2 = {
  5. 'pydantic.utils:version_info': 'pydantic.version:version_info',
  6. 'pydantic.error_wrappers:ValidationError': 'pydantic:ValidationError',
  7. 'pydantic.utils:to_camel': 'pydantic.alias_generators:to_pascal',
  8. 'pydantic.utils:to_lower_camel': 'pydantic.alias_generators:to_camel',
  9. 'pydantic:PyObject': 'pydantic.types:ImportString',
  10. 'pydantic.types:PyObject': 'pydantic.types:ImportString',
  11. 'pydantic.generics:GenericModel': 'pydantic.BaseModel',
  12. }
  13. DEPRECATED_MOVED_IN_V2 = {
  14. 'pydantic.tools:schema_of': 'pydantic.deprecated.tools:schema_of',
  15. 'pydantic.tools:parse_obj_as': 'pydantic.deprecated.tools:parse_obj_as',
  16. 'pydantic.tools:schema_json_of': 'pydantic.deprecated.tools:schema_json_of',
  17. 'pydantic.json:pydantic_encoder': 'pydantic.deprecated.json:pydantic_encoder',
  18. 'pydantic:validate_arguments': 'pydantic.deprecated.decorator:validate_arguments',
  19. 'pydantic.json:custom_pydantic_encoder': 'pydantic.deprecated.json:custom_pydantic_encoder',
  20. 'pydantic.json:timedelta_isoformat': 'pydantic.deprecated.json:timedelta_isoformat',
  21. 'pydantic.decorator:validate_arguments': 'pydantic.deprecated.decorator:validate_arguments',
  22. 'pydantic.class_validators:validator': 'pydantic.deprecated.class_validators:validator',
  23. 'pydantic.class_validators:root_validator': 'pydantic.deprecated.class_validators:root_validator',
  24. 'pydantic.config:BaseConfig': 'pydantic.deprecated.config:BaseConfig',
  25. 'pydantic.config:Extra': 'pydantic.deprecated.config:Extra',
  26. }
  27. REDIRECT_TO_V1 = {
  28. f'pydantic.utils:{obj}': f'pydantic.v1.utils:{obj}'
  29. for obj in (
  30. 'deep_update',
  31. 'GetterDict',
  32. 'lenient_issubclass',
  33. 'lenient_isinstance',
  34. 'is_valid_field',
  35. 'update_not_none',
  36. 'import_string',
  37. 'Representation',
  38. 'ROOT_KEY',
  39. 'smart_deepcopy',
  40. 'sequence_like',
  41. )
  42. }
  43. REMOVED_IN_V2 = {
  44. 'pydantic:ConstrainedBytes',
  45. 'pydantic:ConstrainedDate',
  46. 'pydantic:ConstrainedDecimal',
  47. 'pydantic:ConstrainedFloat',
  48. 'pydantic:ConstrainedFrozenSet',
  49. 'pydantic:ConstrainedInt',
  50. 'pydantic:ConstrainedList',
  51. 'pydantic:ConstrainedSet',
  52. 'pydantic:ConstrainedStr',
  53. 'pydantic:JsonWrapper',
  54. 'pydantic:NoneBytes',
  55. 'pydantic:NoneStr',
  56. 'pydantic:NoneStrBytes',
  57. 'pydantic:Protocol',
  58. 'pydantic:Required',
  59. 'pydantic:StrBytes',
  60. 'pydantic:compiled',
  61. 'pydantic.config:get_config',
  62. 'pydantic.config:inherit_config',
  63. 'pydantic.config:prepare_config',
  64. 'pydantic:create_model_from_namedtuple',
  65. 'pydantic:create_model_from_typeddict',
  66. 'pydantic.dataclasses:create_pydantic_model_from_dataclass',
  67. 'pydantic.dataclasses:make_dataclass_validator',
  68. 'pydantic.dataclasses:set_validation',
  69. 'pydantic.datetime_parse:parse_date',
  70. 'pydantic.datetime_parse:parse_time',
  71. 'pydantic.datetime_parse:parse_datetime',
  72. 'pydantic.datetime_parse:parse_duration',
  73. 'pydantic.error_wrappers:ErrorWrapper',
  74. 'pydantic.errors:AnyStrMaxLengthError',
  75. 'pydantic.errors:AnyStrMinLengthError',
  76. 'pydantic.errors:ArbitraryTypeError',
  77. 'pydantic.errors:BoolError',
  78. 'pydantic.errors:BytesError',
  79. 'pydantic.errors:CallableError',
  80. 'pydantic.errors:ClassError',
  81. 'pydantic.errors:ColorError',
  82. 'pydantic.errors:ConfigError',
  83. 'pydantic.errors:DataclassTypeError',
  84. 'pydantic.errors:DateError',
  85. 'pydantic.errors:DateNotInTheFutureError',
  86. 'pydantic.errors:DateNotInThePastError',
  87. 'pydantic.errors:DateTimeError',
  88. 'pydantic.errors:DecimalError',
  89. 'pydantic.errors:DecimalIsNotFiniteError',
  90. 'pydantic.errors:DecimalMaxDigitsError',
  91. 'pydantic.errors:DecimalMaxPlacesError',
  92. 'pydantic.errors:DecimalWholeDigitsError',
  93. 'pydantic.errors:DictError',
  94. 'pydantic.errors:DurationError',
  95. 'pydantic.errors:EmailError',
  96. 'pydantic.errors:EnumError',
  97. 'pydantic.errors:EnumMemberError',
  98. 'pydantic.errors:ExtraError',
  99. 'pydantic.errors:FloatError',
  100. 'pydantic.errors:FrozenSetError',
  101. 'pydantic.errors:FrozenSetMaxLengthError',
  102. 'pydantic.errors:FrozenSetMinLengthError',
  103. 'pydantic.errors:HashableError',
  104. 'pydantic.errors:IPv4AddressError',
  105. 'pydantic.errors:IPv4InterfaceError',
  106. 'pydantic.errors:IPv4NetworkError',
  107. 'pydantic.errors:IPv6AddressError',
  108. 'pydantic.errors:IPv6InterfaceError',
  109. 'pydantic.errors:IPv6NetworkError',
  110. 'pydantic.errors:IPvAnyAddressError',
  111. 'pydantic.errors:IPvAnyInterfaceError',
  112. 'pydantic.errors:IPvAnyNetworkError',
  113. 'pydantic.errors:IntEnumError',
  114. 'pydantic.errors:IntegerError',
  115. 'pydantic.errors:InvalidByteSize',
  116. 'pydantic.errors:InvalidByteSizeUnit',
  117. 'pydantic.errors:InvalidDiscriminator',
  118. 'pydantic.errors:InvalidLengthForBrand',
  119. 'pydantic.errors:JsonError',
  120. 'pydantic.errors:JsonTypeError',
  121. 'pydantic.errors:ListError',
  122. 'pydantic.errors:ListMaxLengthError',
  123. 'pydantic.errors:ListMinLengthError',
  124. 'pydantic.errors:ListUniqueItemsError',
  125. 'pydantic.errors:LuhnValidationError',
  126. 'pydantic.errors:MissingDiscriminator',
  127. 'pydantic.errors:MissingError',
  128. 'pydantic.errors:NoneIsAllowedError',
  129. 'pydantic.errors:NoneIsNotAllowedError',
  130. 'pydantic.errors:NotDigitError',
  131. 'pydantic.errors:NotNoneError',
  132. 'pydantic.errors:NumberNotGeError',
  133. 'pydantic.errors:NumberNotGtError',
  134. 'pydantic.errors:NumberNotLeError',
  135. 'pydantic.errors:NumberNotLtError',
  136. 'pydantic.errors:NumberNotMultipleError',
  137. 'pydantic.errors:PathError',
  138. 'pydantic.errors:PathNotADirectoryError',
  139. 'pydantic.errors:PathNotAFileError',
  140. 'pydantic.errors:PathNotExistsError',
  141. 'pydantic.errors:PatternError',
  142. 'pydantic.errors:PyObjectError',
  143. 'pydantic.errors:PydanticTypeError',
  144. 'pydantic.errors:PydanticValueError',
  145. 'pydantic.errors:SequenceError',
  146. 'pydantic.errors:SetError',
  147. 'pydantic.errors:SetMaxLengthError',
  148. 'pydantic.errors:SetMinLengthError',
  149. 'pydantic.errors:StrError',
  150. 'pydantic.errors:StrRegexError',
  151. 'pydantic.errors:StrictBoolError',
  152. 'pydantic.errors:SubclassError',
  153. 'pydantic.errors:TimeError',
  154. 'pydantic.errors:TupleError',
  155. 'pydantic.errors:TupleLengthError',
  156. 'pydantic.errors:UUIDError',
  157. 'pydantic.errors:UUIDVersionError',
  158. 'pydantic.errors:UrlError',
  159. 'pydantic.errors:UrlExtraError',
  160. 'pydantic.errors:UrlHostError',
  161. 'pydantic.errors:UrlHostTldError',
  162. 'pydantic.errors:UrlPortError',
  163. 'pydantic.errors:UrlSchemeError',
  164. 'pydantic.errors:UrlSchemePermittedError',
  165. 'pydantic.errors:UrlUserInfoError',
  166. 'pydantic.errors:WrongConstantError',
  167. 'pydantic.main:validate_model',
  168. 'pydantic.networks:stricturl',
  169. 'pydantic:parse_file_as',
  170. 'pydantic:parse_raw_as',
  171. 'pydantic:stricturl',
  172. 'pydantic.tools:parse_file_as',
  173. 'pydantic.tools:parse_raw_as',
  174. 'pydantic.types:ConstrainedBytes',
  175. 'pydantic.types:ConstrainedDate',
  176. 'pydantic.types:ConstrainedDecimal',
  177. 'pydantic.types:ConstrainedFloat',
  178. 'pydantic.types:ConstrainedFrozenSet',
  179. 'pydantic.types:ConstrainedInt',
  180. 'pydantic.types:ConstrainedList',
  181. 'pydantic.types:ConstrainedSet',
  182. 'pydantic.types:ConstrainedStr',
  183. 'pydantic.types:JsonWrapper',
  184. 'pydantic.types:NoneBytes',
  185. 'pydantic.types:NoneStr',
  186. 'pydantic.types:NoneStrBytes',
  187. 'pydantic.types:StrBytes',
  188. 'pydantic.typing:evaluate_forwardref',
  189. 'pydantic.typing:AbstractSetIntStr',
  190. 'pydantic.typing:AnyCallable',
  191. 'pydantic.typing:AnyClassMethod',
  192. 'pydantic.typing:CallableGenerator',
  193. 'pydantic.typing:DictAny',
  194. 'pydantic.typing:DictIntStrAny',
  195. 'pydantic.typing:DictStrAny',
  196. 'pydantic.typing:IntStr',
  197. 'pydantic.typing:ListStr',
  198. 'pydantic.typing:MappingIntStrAny',
  199. 'pydantic.typing:NoArgAnyCallable',
  200. 'pydantic.typing:NoneType',
  201. 'pydantic.typing:ReprArgs',
  202. 'pydantic.typing:SetStr',
  203. 'pydantic.typing:StrPath',
  204. 'pydantic.typing:TupleGenerator',
  205. 'pydantic.typing:WithArgsTypes',
  206. 'pydantic.typing:all_literal_values',
  207. 'pydantic.typing:display_as_type',
  208. 'pydantic.typing:get_all_type_hints',
  209. 'pydantic.typing:get_args',
  210. 'pydantic.typing:get_origin',
  211. 'pydantic.typing:get_sub_types',
  212. 'pydantic.typing:is_callable_type',
  213. 'pydantic.typing:is_classvar',
  214. 'pydantic.typing:is_finalvar',
  215. 'pydantic.typing:is_literal_type',
  216. 'pydantic.typing:is_namedtuple',
  217. 'pydantic.typing:is_new_type',
  218. 'pydantic.typing:is_none_type',
  219. 'pydantic.typing:is_typeddict',
  220. 'pydantic.typing:is_typeddict_special',
  221. 'pydantic.typing:is_union',
  222. 'pydantic.typing:new_type_supertype',
  223. 'pydantic.typing:resolve_annotations',
  224. 'pydantic.typing:typing_base',
  225. 'pydantic.typing:update_field_forward_refs',
  226. 'pydantic.typing:update_model_forward_refs',
  227. 'pydantic.utils:ClassAttribute',
  228. 'pydantic.utils:DUNDER_ATTRIBUTES',
  229. 'pydantic.utils:PyObjectStr',
  230. 'pydantic.utils:ValueItems',
  231. 'pydantic.utils:almost_equal_floats',
  232. 'pydantic.utils:get_discriminator_alias_and_values',
  233. 'pydantic.utils:get_model',
  234. 'pydantic.utils:get_unique_discriminator_alias',
  235. 'pydantic.utils:in_ipython',
  236. 'pydantic.utils:is_valid_identifier',
  237. 'pydantic.utils:path_type',
  238. 'pydantic.utils:validate_field_name',
  239. 'pydantic:validate_model',
  240. }
  241. def getattr_migration(module: str) -> Callable[[str], Any]:
  242. """Implement PEP 562 for objects that were either moved or removed on the migration
  243. to V2.
  244. Args:
  245. module: The module name.
  246. Returns:
  247. A callable that will raise an error if the object is not found.
  248. """
  249. # This avoids circular import with errors.py.
  250. from .errors import PydanticImportError
  251. def wrapper(name: str) -> object:
  252. """Raise an error if the object is not found, or warn if it was moved.
  253. In case it was moved, it still returns the object.
  254. Args:
  255. name: The object name.
  256. Returns:
  257. The object.
  258. """
  259. if name == '__path__':
  260. raise AttributeError(f'module {module!r} has no attribute {name!r}')
  261. import warnings
  262. from ._internal._validators import import_string
  263. import_path = f'{module}:{name}'
  264. if import_path in MOVED_IN_V2.keys():
  265. new_location = MOVED_IN_V2[import_path]
  266. warnings.warn(f'`{import_path}` has been moved to `{new_location}`.')
  267. return import_string(MOVED_IN_V2[import_path])
  268. if import_path in DEPRECATED_MOVED_IN_V2:
  269. # skip the warning here because a deprecation warning will be raised elsewhere
  270. return import_string(DEPRECATED_MOVED_IN_V2[import_path])
  271. if import_path in REDIRECT_TO_V1:
  272. new_location = REDIRECT_TO_V1[import_path]
  273. warnings.warn(
  274. f'`{import_path}` has been removed. We are importing from `{new_location}` instead.'
  275. 'See the migration guide for more details: https://docs.pydantic.dev/latest/migration/'
  276. )
  277. return import_string(REDIRECT_TO_V1[import_path])
  278. if import_path == 'pydantic:BaseSettings':
  279. raise PydanticImportError(
  280. '`BaseSettings` has been moved to the `pydantic-settings` package. '
  281. f'See https://docs.pydantic.dev/{version_short()}/migration/#basesettings-has-moved-to-pydantic-settings '
  282. 'for more details.'
  283. )
  284. if import_path in REMOVED_IN_V2:
  285. raise PydanticImportError(f'`{import_path}` has been removed in V2.')
  286. globals: dict[str, Any] = sys.modules[module].__dict__
  287. if name in globals:
  288. return globals[name]
  289. raise AttributeError(f'module {module!r} has no attribute {name!r}')
  290. return wrapper