Debian Codegraph @

pyvista/utilities/__init__.py

raw
1 """Deprecated utilities subpackage."""
2
3 from __future__ import annotations
4
5 import importlib
6 import inspect
7
8 from pyvista._warn_external import warn_external
9
10 # Places to look for the utility
11 _MODULES = [
12 'pyvista.core.utilities',
13 'pyvista.core.errors',
14 'pyvista.core',
15 'pyvista.plotting.utilities',
16 'pyvista.plotting.errors',
17 'pyvista.plotting.texture',
18 'pyvista.plotting',
19 'pyvista.report',
20 'pyvista.plotting.themes',
21 ]
22
23
24 def _try_import(module, name):
25 """Attempt to import a module."""
26 _module = importlib.import_module(module)
27 try:
28 feature = inspect.getattr_static(_module, name)
29 import_path = f'from {module} import {name}'
30 except AttributeError:
31 return None, None
32 return feature, import_path
33
34
35 def __getattr__(name):
36 """Fetch an attribute ``name`` from ``globals()`` and warn if it's from a deprecated module.
37
38 Note that ``__getattr__()`` only gets called when ``name`` is missing
39 from the module's globals. The trick is that we want to import this
40 function into other deprecated modules, and we want to carry this
41 subpackage's globals along to prevent some spurious warnings.
42
43 Raises
44 ------
45 AttributeError
46 If the attribute is not found in ``globals()`` and also could not be
47 imported from the modules in ``_MODULES``.
48
49 PyVistaDeprecationWarning
50 If the attribute has been found via importing from the modules in
51 ``_MODULES``, as this implies that the feature has been moved from
52 pyvista.utilities.
53
54 """
55 try:
56 return globals()[name]
57 except KeyError:
58 pass
59
60 for module in _MODULES:
61 feature, import_path = _try_import(module, name)
62 if feature is not None:
63 break
64 else:
65 msg = (
66 f'Module `pyvista.utilities` has been deprecated and we could not automatically '
67 f'find `{name}`. This feature has moved.'
68 )
69 raise AttributeError(msg) from None
70
71 message = (
72 f'The `pyvista.utilities` module has been deprecated. `{name}` is now imported '
73 f'as: `{import_path}`.'
74 )
75
76 from pyvista.core.errors import PyVistaDeprecationWarning # noqa: PLC0415
77
78 warn_external(message, PyVistaDeprecationWarning)
79
80 return feature