Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/pydantic/_migration.py: 72%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

36 statements  

1import sys 

2from collections.abc import Callable 

3from typing import Any 

4 

5from pydantic.warnings import PydanticDeprecatedSince20 

6 

7from .version import version_short 

8 

9MOVED_IN_V2 = { 

10 'pydantic.utils:version_info': 'pydantic.version:version_info', 

11 'pydantic.error_wrappers:ValidationError': 'pydantic:ValidationError', 

12 'pydantic.utils:to_camel': 'pydantic.alias_generators:to_pascal', 

13 'pydantic.utils:to_lower_camel': 'pydantic.alias_generators:to_camel', 

14 'pydantic:PyObject': 'pydantic.types:ImportString', 

15 'pydantic.types:PyObject': 'pydantic.types:ImportString', 

16 'pydantic.generics:GenericModel': 'pydantic.BaseModel', 

17} 

18 

19DEPRECATED_MOVED_IN_V2 = { 

20 'pydantic.tools:schema_of': 'pydantic.deprecated.tools:schema_of', 

21 'pydantic.tools:parse_obj_as': 'pydantic.deprecated.tools:parse_obj_as', 

22 'pydantic.tools:schema_json_of': 'pydantic.deprecated.tools:schema_json_of', 

23 'pydantic.json:pydantic_encoder': 'pydantic.deprecated.json:pydantic_encoder', 

24 'pydantic:validate_arguments': 'pydantic.deprecated.decorator:validate_arguments', 

25 'pydantic.json:custom_pydantic_encoder': 'pydantic.deprecated.json:custom_pydantic_encoder', 

26 'pydantic.json:timedelta_isoformat': 'pydantic.deprecated.json:timedelta_isoformat', 

27 'pydantic.decorator:validate_arguments': 'pydantic.deprecated.decorator:validate_arguments', 

28 'pydantic.class_validators:validator': 'pydantic.deprecated.class_validators:validator', 

29 'pydantic.class_validators:root_validator': 'pydantic.deprecated.class_validators:root_validator', 

30 'pydantic.config:BaseConfig': 'pydantic.deprecated.config:BaseConfig', 

31 'pydantic.config:Extra': 'pydantic.deprecated.config:Extra', 

32} 

33 

34REDIRECT_TO_V1 = { 

35 f'pydantic.utils:{obj}': f'pydantic.v1.utils:{obj}' 

36 for obj in ( 

37 'deep_update', 

38 'GetterDict', 

39 'lenient_issubclass', 

40 'lenient_isinstance', 

41 'is_valid_field', 

42 'update_not_none', 

43 'import_string', 

44 'Representation', 

45 'ROOT_KEY', 

46 'smart_deepcopy', 

47 'sequence_like', 

48 ) 

49} 

50 

51 

52REMOVED_IN_V2 = { 

53 'pydantic:ConstrainedBytes', 

54 'pydantic:ConstrainedDate', 

55 'pydantic:ConstrainedDecimal', 

56 'pydantic:ConstrainedFloat', 

57 'pydantic:ConstrainedFrozenSet', 

58 'pydantic:ConstrainedInt', 

59 'pydantic:ConstrainedList', 

60 'pydantic:ConstrainedSet', 

61 'pydantic:ConstrainedStr', 

62 'pydantic:JsonWrapper', 

63 'pydantic:NoneBytes', 

64 'pydantic:NoneStr', 

65 'pydantic:NoneStrBytes', 

66 'pydantic:Protocol', 

67 'pydantic:Required', 

68 'pydantic:StrBytes', 

69 'pydantic:compiled', 

70 'pydantic.config:get_config', 

71 'pydantic.config:inherit_config', 

72 'pydantic.config:prepare_config', 

73 'pydantic:create_model_from_namedtuple', 

74 'pydantic:create_model_from_typeddict', 

75 'pydantic.dataclasses:create_pydantic_model_from_dataclass', 

76 'pydantic.dataclasses:make_dataclass_validator', 

77 'pydantic.dataclasses:set_validation', 

78 'pydantic.datetime_parse:parse_date', 

79 'pydantic.datetime_parse:parse_time', 

80 'pydantic.datetime_parse:parse_datetime', 

81 'pydantic.datetime_parse:parse_duration', 

82 'pydantic.error_wrappers:ErrorWrapper', 

83 'pydantic.errors:AnyStrMaxLengthError', 

84 'pydantic.errors:AnyStrMinLengthError', 

85 'pydantic.errors:ArbitraryTypeError', 

86 'pydantic.errors:BoolError', 

87 'pydantic.errors:BytesError', 

88 'pydantic.errors:CallableError', 

89 'pydantic.errors:ClassError', 

90 'pydantic.errors:ColorError', 

91 'pydantic.errors:ConfigError', 

92 'pydantic.errors:DataclassTypeError', 

93 'pydantic.errors:DateError', 

94 'pydantic.errors:DateNotInTheFutureError', 

95 'pydantic.errors:DateNotInThePastError', 

96 'pydantic.errors:DateTimeError', 

97 'pydantic.errors:DecimalError', 

98 'pydantic.errors:DecimalIsNotFiniteError', 

99 'pydantic.errors:DecimalMaxDigitsError', 

100 'pydantic.errors:DecimalMaxPlacesError', 

101 'pydantic.errors:DecimalWholeDigitsError', 

102 'pydantic.errors:DictError', 

103 'pydantic.errors:DurationError', 

104 'pydantic.errors:EmailError', 

105 'pydantic.errors:EnumError', 

106 'pydantic.errors:EnumMemberError', 

107 'pydantic.errors:ExtraError', 

108 'pydantic.errors:FloatError', 

109 'pydantic.errors:FrozenSetError', 

110 'pydantic.errors:FrozenSetMaxLengthError', 

111 'pydantic.errors:FrozenSetMinLengthError', 

112 'pydantic.errors:HashableError', 

113 'pydantic.errors:IPv4AddressError', 

114 'pydantic.errors:IPv4InterfaceError', 

115 'pydantic.errors:IPv4NetworkError', 

116 'pydantic.errors:IPv6AddressError', 

117 'pydantic.errors:IPv6InterfaceError', 

118 'pydantic.errors:IPv6NetworkError', 

119 'pydantic.errors:IPvAnyAddressError', 

120 'pydantic.errors:IPvAnyInterfaceError', 

121 'pydantic.errors:IPvAnyNetworkError', 

122 'pydantic.errors:IntEnumError', 

123 'pydantic.errors:IntegerError', 

124 'pydantic.errors:InvalidByteSize', 

125 'pydantic.errors:InvalidByteSizeUnit', 

126 'pydantic.errors:InvalidDiscriminator', 

127 'pydantic.errors:InvalidLengthForBrand', 

128 'pydantic.errors:JsonError', 

129 'pydantic.errors:JsonTypeError', 

130 'pydantic.errors:ListError', 

131 'pydantic.errors:ListMaxLengthError', 

132 'pydantic.errors:ListMinLengthError', 

133 'pydantic.errors:ListUniqueItemsError', 

134 'pydantic.errors:LuhnValidationError', 

135 'pydantic.errors:MissingDiscriminator', 

136 'pydantic.errors:MissingError', 

137 'pydantic.errors:NoneIsAllowedError', 

138 'pydantic.errors:NoneIsNotAllowedError', 

139 'pydantic.errors:NotDigitError', 

140 'pydantic.errors:NotNoneError', 

141 'pydantic.errors:NumberNotGeError', 

142 'pydantic.errors:NumberNotGtError', 

143 'pydantic.errors:NumberNotLeError', 

144 'pydantic.errors:NumberNotLtError', 

145 'pydantic.errors:NumberNotMultipleError', 

146 'pydantic.errors:PathError', 

147 'pydantic.errors:PathNotADirectoryError', 

148 'pydantic.errors:PathNotAFileError', 

149 'pydantic.errors:PathNotExistsError', 

150 'pydantic.errors:PatternError', 

151 'pydantic.errors:PyObjectError', 

152 'pydantic.errors:PydanticTypeError', 

153 'pydantic.errors:PydanticValueError', 

154 'pydantic.errors:SequenceError', 

155 'pydantic.errors:SetError', 

156 'pydantic.errors:SetMaxLengthError', 

157 'pydantic.errors:SetMinLengthError', 

158 'pydantic.errors:StrError', 

159 'pydantic.errors:StrRegexError', 

160 'pydantic.errors:StrictBoolError', 

161 'pydantic.errors:SubclassError', 

162 'pydantic.errors:TimeError', 

163 'pydantic.errors:TupleError', 

164 'pydantic.errors:TupleLengthError', 

165 'pydantic.errors:UUIDError', 

166 'pydantic.errors:UUIDVersionError', 

167 'pydantic.errors:UrlError', 

168 'pydantic.errors:UrlExtraError', 

169 'pydantic.errors:UrlHostError', 

170 'pydantic.errors:UrlHostTldError', 

171 'pydantic.errors:UrlPortError', 

172 'pydantic.errors:UrlSchemeError', 

173 'pydantic.errors:UrlSchemePermittedError', 

174 'pydantic.errors:UrlUserInfoError', 

175 'pydantic.errors:WrongConstantError', 

176 'pydantic.main:validate_model', 

177 'pydantic.networks:stricturl', 

178 'pydantic:parse_file_as', 

179 'pydantic:parse_raw_as', 

180 'pydantic:stricturl', 

181 'pydantic.tools:parse_file_as', 

182 'pydantic.tools:parse_raw_as', 

183 'pydantic.types:ConstrainedBytes', 

184 'pydantic.types:ConstrainedDate', 

185 'pydantic.types:ConstrainedDecimal', 

186 'pydantic.types:ConstrainedFloat', 

187 'pydantic.types:ConstrainedFrozenSet', 

188 'pydantic.types:ConstrainedInt', 

189 'pydantic.types:ConstrainedList', 

190 'pydantic.types:ConstrainedSet', 

191 'pydantic.types:ConstrainedStr', 

192 'pydantic.types:JsonWrapper', 

193 'pydantic.types:NoneBytes', 

194 'pydantic.types:NoneStr', 

195 'pydantic.types:NoneStrBytes', 

196 'pydantic.types:StrBytes', 

197 'pydantic.typing:evaluate_forwardref', 

198 'pydantic.typing:AbstractSetIntStr', 

199 'pydantic.typing:AnyCallable', 

200 'pydantic.typing:AnyClassMethod', 

201 'pydantic.typing:CallableGenerator', 

202 'pydantic.typing:DictAny', 

203 'pydantic.typing:DictIntStrAny', 

204 'pydantic.typing:DictStrAny', 

205 'pydantic.typing:IntStr', 

206 'pydantic.typing:ListStr', 

207 'pydantic.typing:MappingIntStrAny', 

208 'pydantic.typing:NoArgAnyCallable', 

209 'pydantic.typing:NoneType', 

210 'pydantic.typing:ReprArgs', 

211 'pydantic.typing:SetStr', 

212 'pydantic.typing:StrPath', 

213 'pydantic.typing:TupleGenerator', 

214 'pydantic.typing:WithArgsTypes', 

215 'pydantic.typing:all_literal_values', 

216 'pydantic.typing:display_as_type', 

217 'pydantic.typing:get_all_type_hints', 

218 'pydantic.typing:get_args', 

219 'pydantic.typing:get_origin', 

220 'pydantic.typing:get_sub_types', 

221 'pydantic.typing:is_callable_type', 

222 'pydantic.typing:is_classvar', 

223 'pydantic.typing:is_finalvar', 

224 'pydantic.typing:is_literal_type', 

225 'pydantic.typing:is_namedtuple', 

226 'pydantic.typing:is_new_type', 

227 'pydantic.typing:is_none_type', 

228 'pydantic.typing:is_typeddict', 

229 'pydantic.typing:is_typeddict_special', 

230 'pydantic.typing:is_union', 

231 'pydantic.typing:new_type_supertype', 

232 'pydantic.typing:resolve_annotations', 

233 'pydantic.typing:typing_base', 

234 'pydantic.typing:update_field_forward_refs', 

235 'pydantic.typing:update_model_forward_refs', 

236 'pydantic.utils:ClassAttribute', 

237 'pydantic.utils:DUNDER_ATTRIBUTES', 

238 'pydantic.utils:PyObjectStr', 

239 'pydantic.utils:ValueItems', 

240 'pydantic.utils:almost_equal_floats', 

241 'pydantic.utils:get_discriminator_alias_and_values', 

242 'pydantic.utils:get_model', 

243 'pydantic.utils:get_unique_discriminator_alias', 

244 'pydantic.utils:in_ipython', 

245 'pydantic.utils:is_valid_identifier', 

246 'pydantic.utils:path_type', 

247 'pydantic.utils:validate_field_name', 

248 'pydantic:validate_model', 

249} 

250 

251 

252def getattr_migration(module: str) -> Callable[[str], Any]: 

253 """Implement PEP 562 for objects that were either moved or removed on the migration 

254 to V2. 

255 

256 Args: 

257 module: The module name. 

258 

259 Returns: 

260 A callable that will raise an error if the object is not found. 

261 """ 

262 # This avoids circular import with errors.py. 

263 from .errors import PydanticImportError 

264 

265 def wrapper(name: str) -> object: 

266 """Raise an error if the object is not found, or warn if it was moved. 

267 

268 In case it was moved, it still returns the object. 

269 

270 Args: 

271 name: The object name. 

272 

273 Returns: 

274 The object. 

275 """ 

276 if name == '__path__': 

277 raise AttributeError(f'module {module!r} has no attribute {name!r}') 

278 

279 import warnings 

280 

281 from ._internal._validators import import_string 

282 

283 import_path = f'{module}:{name}' 

284 if import_path in MOVED_IN_V2: 

285 new_location = MOVED_IN_V2[import_path] 

286 warnings.warn( 

287 f'`{import_path}` has been moved to `{new_location}`.', 

288 category=PydanticDeprecatedSince20, 

289 stacklevel=2, 

290 ) 

291 return import_string(new_location) 

292 if import_path in DEPRECATED_MOVED_IN_V2: 

293 # skip the warning here because a deprecation warning will be raised elsewhere 

294 return import_string(DEPRECATED_MOVED_IN_V2[import_path]) 

295 if import_path in REDIRECT_TO_V1: 

296 new_location = REDIRECT_TO_V1[import_path] 

297 warnings.warn( 

298 f'`{import_path}` has been removed. We are importing from `{new_location}` instead.' 

299 f' See the migration guide for more details: https://pydantic.dev/docs/validation/{version_short()}/migration/', 

300 category=PydanticDeprecatedSince20, 

301 stacklevel=2, 

302 ) 

303 return import_string(new_location) 

304 if import_path == 'pydantic:BaseSettings': 

305 raise PydanticImportError( 

306 '`BaseSettings` has been moved to the `pydantic-settings` package. ' 

307 f'See https://pydantic.dev/docs/validation/{version_short()}/migration/#basesettings-has-moved-to-pydantic-settings ' 

308 'for more details.' 

309 ) 

310 if import_path in REMOVED_IN_V2: 

311 raise PydanticImportError(f'`{import_path}` has been removed in V2.') 

312 globals: dict[str, Any] = sys.modules[module].__dict__ 

313 if name in globals: 

314 return globals[name] 

315 raise AttributeError(f'module {module!r} has no attribute {name!r}') 

316 

317 return wrapper