Passed
Push — master ( 7aa640...5058b2 )
by Konstantin
01:54
created

ocrd_utils.deprecate.rename_kwargs()   A

Complexity

Conditions 4

Size

Total Lines 9
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 9
rs 9.95
c 0
b 0
f 0
cc 4
nop 3
1
import functools
2
import warnings
3
4
"""
5
https://stackoverflow.com/questions/49802412/how-to-implement-deprecation-in-python-with-argument-alias
6
by user2357112 supports Monica
7
"""
8
9
def deprecated_alias(**aliases):
10
    """
11
    Deprecate a kwarg in favor of another kwarg
12
    """
13
    def deco(f):
14
        @functools.wraps(f)
15
        def wrapper(*args, **kwargs):
16
            rename_kwargs(f.__name__, kwargs, aliases)
17
            return f(*args, **kwargs)
18
        return wrapper
19
    return deco
20
21
def rename_kwargs(func_name, kwargs, aliases):
22
    for alias, new in aliases.items():
23
        if alias in kwargs:
24
            if new in kwargs:
25
                raise TypeError('{} received both {} and {}'.format(
26
                    func_name, alias, new))
27
            warnings.warn('{} is deprecated; use {}'.format(alias, new),
28
                          DeprecationWarning)
29
            kwargs[new] = kwargs.pop(alias)
30