• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

LSSTDESC / CCL / 5191066774

06 Jun 2023 04:42PM UTC coverage: 97.543% (+0.4%) from 97.136%
5191066774

Pull #1087

github

web-flow
Merge 987bfdbae into 17a0e5a2a
Pull Request #1087: v3 preparation

83 of 83 new or added lines in 36 files covered. (100.0%)

5122 of 5251 relevant lines covered (97.54%)

0.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

21.43
/pyccl/_core/deprecations.py
1
__all__ = ("deprecated", "deprecate_attr",)
1✔
2

3
import functools
1✔
4
import warnings
1✔
5

6
from .. import CCLDeprecationWarning
1✔
7

8

9
def deprecated(new_function=None):
1✔
10
    """This is a decorator which can be used to mark functions
11
    as deprecated. It will result in a warning being emitted
12
    when the function is used. If there is a replacement function,
13
    pass it as `new_function`.
14
    """
15
    def decorator(func):
×
16
        @functools.wraps(func)
×
17
        def wrapper(*args, **kwargs):
×
18
            s = f"The function {func.__qualname__} is deprecated."
×
19
            if new_function:
×
20
                s += f" Use {new_function.__qualname__} instead."
×
21
            warnings.warn(s, CCLDeprecationWarning)
×
22
            return func(*args, **kwargs)
×
23
        return wrapper
×
24
    return decorator
×
25

26

27
def deprecate_attr(getter=None, *, pairs=[]):
1✔
28
    """This decorator can be used to deprecate attributes,
29
    warning users about it and pointing them to the new attribute.
30

31
    Parameters
32
    ----------
33
    getter : slot wrapper ``__getattribute__``
34
        This is the getter method to be decorated.
35
    pairs : list of pairs
36
        List of renaming pairs ``('old', 'new')``.
37

38
    Example
39
    -------
40
    We have the legacy attribute ``old_name`` which we want to rename
41
    to ``new_name``. To achieve this we decorate the ``__getattribute__``
42
    method of the parent class in the main class body to retrieve the
43
    ``__getattr__`` method for the main class, like so:
44

45
    >>>  __getattr__ = deprecate_attr([('old_name', 'new_name')])(
46
             super.__getattribute__)
47

48
    Now, every time the attribute is called via its old name, the user will
49
    be warned about the renaming, and the attribute value will be returned.
50

51
    .. note:: Make sure that you bind ``__getattr__`` to the decorator,
52
              rather than ``__getattribute__``, because ``__getattr__``
53
              provides the fallback mechanism we want to use. Otherwise,
54
              an infinite recursion will initiate.
55

56
    """
57
    if getter is None:
×
58
        return functools.partial(deprecate_attr, pairs=pairs)
×
59

60
    rename = dict(pairs)
×
61

62
    @functools.wraps(getter)
×
63
    def wrapper(cls, name):
×
64
        if name in rename:
×
65
            new_name = rename[name]
×
66
            class_name = cls.__class__.__name__
×
67
            warnings.warn(
×
68
                f"Attribute {name} is deprecated in {class_name}. "
69
                f"Pass the new name {new_name}.", CCLDeprecationWarning)
70
            name = new_name
×
71

72
        return cls.__getattribute__(name)
×
73
    return wrapper
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc