Raise an error if the object is not found, or warn if it was moved. In case it was moved, it still returns the object. Args: name: The object name. Returns: The object.
(name: str)
| 262 | from .errors import PydanticImportError |
| 263 | |
| 264 | def wrapper(name: str) -> object: |
| 265 | """Raise an error if the object is not found, or warn if it was moved. |
| 266 | |
| 267 | In case it was moved, it still returns the object. |
| 268 | |
| 269 | Args: |
| 270 | name: The object name. |
| 271 | |
| 272 | Returns: |
| 273 | The object. |
| 274 | """ |
| 275 | if name == '__path__': |
| 276 | raise AttributeError(f'module {module!r} has no attribute {name!r}') |
| 277 | |
| 278 | import warnings |
| 279 | |
| 280 | from ._internal._validators import import_string |
| 281 | |
| 282 | import_path = f'{module}:{name}' |
| 283 | if import_path in MOVED_IN_V2.keys(): |
| 284 | new_location = MOVED_IN_V2[import_path] |
| 285 | warnings.warn( |
| 286 | f'`{import_path}` has been moved to `{new_location}`.', |
| 287 | category=PydanticDeprecatedSince20, |
| 288 | stacklevel=2, |
| 289 | ) |
| 290 | return import_string(MOVED_IN_V2[import_path]) |
| 291 | if import_path in DEPRECATED_MOVED_IN_V2: |
| 292 | # skip the warning here because a deprecation warning will be raised elsewhere |
| 293 | return import_string(DEPRECATED_MOVED_IN_V2[import_path]) |
| 294 | if import_path in REDIRECT_TO_V1: |
| 295 | new_location = REDIRECT_TO_V1[import_path] |
| 296 | warnings.warn( |
| 297 | f'`{import_path}` has been removed. We are importing from `{new_location}` instead.' |
| 298 | 'See the migration guide for more details: https://docs.pydantic.dev/latest/migration/', |
| 299 | category=PydanticDeprecatedSince20, |
| 300 | stacklevel=2, |
| 301 | ) |
| 302 | return import_string(REDIRECT_TO_V1[import_path]) |
| 303 | if import_path == 'pydantic:BaseSettings': |
| 304 | raise PydanticImportError( |
| 305 | '`BaseSettings` has been moved to the `pydantic-settings` package. ' |
| 306 | f'See https://docs.pydantic.dev/{version_short()}/migration/#basesettings-has-moved-to-pydantic-settings ' |
| 307 | 'for more details.' |
| 308 | ) |
| 309 | if import_path in REMOVED_IN_V2: |
| 310 | raise PydanticImportError(f'`{import_path}` has been removed in V2.') |
| 311 | globals: dict[str, Any] = sys.modules[module].__dict__ |
| 312 | if name in globals: |
| 313 | return globals[name] |
| 314 | raise AttributeError(f'module {module!r} has no attribute {name!r}') |
| 315 | |
| 316 | return wrapper |
nothing calls this directly
no test coverage detected