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