File: errors.py

package info (click to toggle)
python-pyvista 0.46.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 176,968 kB
  • sloc: python: 94,346; sh: 216; makefile: 70
file content (72 lines) | stat: -rw-r--r-- 2,065 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""Deprecated utilities subpackage."""

from __future__ import annotations

import importlib
import inspect
import warnings

# Places to look for the utility
_MODULES = [
    'pyvista.core.errors',
    'pyvista.plotting.errors',
]


def _try_import(module, name):
    """Attempt to import a module."""
    _module = importlib.import_module(module)
    try:
        feature = inspect.getattr_static(_module, name)
        import_path = f'from {module} import {name}'
    except AttributeError:
        return None, None
    return feature, import_path


def __getattr__(name):
    """Fetch an attribute ``name`` from ``globals()`` and warn if it's from a deprecated module.

    Note that ``__getattr__()`` only gets called when ``name`` is missing
    from the module's globals. The trick is that we want to import this
    function into other deprecated modules, and we want to carry this
    subpackage's globals along to prevent some spurious warnings.

    Raises
    ------
    AttributeError
        If the attribute is not found in ``globals()`` and also could not be
        imported from the modules in ``_MODULES``.

    Warns
    -----
    PyVistaDeprecationWarning
        If the attribute has been found via importing from the modules in
        ``_MODULES``, as this implies that the feature has been moved from
        ``pyvista.utilities``.

    """
    from pyvista.core.errors import PyVistaDeprecationWarning  # noqa: PLC0415

    for module in _MODULES:
        feature, import_path = _try_import(module, name)
        if feature is not None:
            break
    else:  # pragma: no cover
        msg = (
            f'Module `pyvista.errors` has been deprecated and we could not automatically '
            f'find `{name}`. This feature has moved.'
        )
        raise AttributeError(msg) from None

    message = (
        f'The `pyvista.errors` module has been deprecated. '
        f'`{name}` is now imported as: `{import_path}`.'
    )

    warnings.warn(
        message,
        PyVistaDeprecationWarning,
    )

    return feature