Completed
Pull Request — master (#2603)
by
unknown
03:15 queued 01:20
created

deprecate_settings()   C

Complexity

Conditions 10

Size

Total Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 10
c 2
b 0
f 0
dl 0
loc 56
rs 5.0704

2 Methods

Rating   Name   Duplication   Size   Complexity  
B wrapping_function() 0 12 6
D _deprecate_decorator() 0 23 9

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like deprecate_settings() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
"""
2
The bearlib is an optional library designed to ease the task of any Bear. Just
3
as the rest of coala the bearlib is designed to be as easy to use as possible
4
while offering the best possible flexibility.
5
"""
6
7
from coalib.settings.FunctionMetadata import FunctionMetadata
8
9
10
def deprecate_settings(**depr_args):
11
    """
12
     The purpose of this decorator is to allow passing old settings names to
13
     bears due to the heavy changes in their names.
14
15
     >>> @deprecate_settings(new='old')
16
     ... def run(new):
17
     ...     print(new)
18
19
     Now we can simply call the bear with the deprecated setting, we'll get a
20
     warning - but it still works!
21
22
     >>> run(old="Hello world!")
23
     The setting `old` is deprecated. Please use `new` instead.
24
     Hello world!
25
     >>> run(new="Hello world!")
26
     Hello world!
27
28
     The metadata for coala has been adjusted as well:
29
30
     >>> list(run.__metadata__.non_optional_params.keys())
31
     ['new']
32
     >>> list(run.__metadata__.optional_params.keys())
33
     ['old']
34
35
     You cannot deprecate an already deprecated setting. Don't try. It will
36
     introduce nondeterministic errors to your program.
37
38
     :param depr_args: A dictionary of settings as keys and their deprecated
39
                       names as values.
40
    """
41
    def _deprecate_decorator(func):
42
43
        def wrapping_function(*args, **kwargs):
44
            for arg, _deprecated_arg in depr_args.items():
45
                deprecated_arg, myfunc =  ( _deprecated_arg 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
Coding Style introduced by
Exactly one space required after assignment
deprecated_arg, myfunc = ( _deprecated_arg
^
Loading history...
Coding Style introduced by
No space allowed after bracket
deprecated_arg, myfunc = ( _deprecated_arg
^
Loading history...
46
                    if isinstance(_deprecated_arg, tuple)   
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
47
                    else (_deprecated_arg, None))
48
                if deprecated_arg in kwargs and arg not in kwargs:
49
                    print("The setting `{}` is deprecated. Please use `{}` "
50
                          "instead.".format(deprecated_arg, arg))
51
                    kwargs[arg] = (myfunc.__call__(kwargs[deprecated_arg])
52
                        if myfunc is not None else kwargs[deprecated_arg])
53
                    del kwargs[deprecated_arg]
54
            return func(*args, **kwargs)
55
56
        new_metadata = FunctionMetadata.from_function(func)
57
        for arg, _deprecated_arg in depr_args.items():
58
            deprecated_arg =  ( _deprecated_arg[0] 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
Coding Style introduced by
Exactly one space required after assignment
deprecated_arg = ( _deprecated_arg[0]
^
Loading history...
Coding Style introduced by
No space allowed after bracket
deprecated_arg = ( _deprecated_arg[0]
^
Loading history...
59
                if isinstance(_deprecated_arg, tuple) else _deprecated_arg)
60
            new_metadata.add_alias(arg, deprecated_arg)
61
        wrapping_function.__metadata__ = new_metadata
62
63
        return wrapping_function
64
65
    return _deprecate_decorator
66