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

ocrd_utils.deprecate   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 21
dl 0
loc 30
rs 10
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A deprecated_alias() 0 11 1
A rename_kwargs() 0 9 4
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