_migration.py 12 KB

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