| Conditions | 7 |
| Total Lines | 55 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
| 1 | """ |
||
| 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: |
||
| 36 | |||
| 37 | >>> @deprecate_settings(old='very_old', new='old') |
||
| 38 | ... def run(new): pass |
||
| 39 | Traceback (most recent call last): |
||
| 40 | ... |
||
| 41 | KeyError: 'old' |
||
| 42 | |||
| 43 | :param depr_args: A dictionary of settings as keys and their deprecated |
||
| 44 | names as values. |
||
| 45 | """ |
||
| 46 | def _deprecate_decorator(func): |
||
| 47 | |||
| 48 | def wrapping_function(*args, **kwargs): |
||
| 49 | for arg, deprecated_arg in depr_args.items(): |
||
| 50 | if deprecated_arg in kwargs and arg not in kwargs: |
||
| 51 | print("The setting `{}` is deprecated. Please use `{}` " |
||
| 52 | "instead.".format(deprecated_arg, arg)) |
||
| 53 | kwargs[arg] = kwargs[deprecated_arg] |
||
| 54 | del kwargs[deprecated_arg] |
||
| 55 | return func(*args, **kwargs) |
||
| 56 | |||
| 57 | new_metadata = FunctionMetadata.from_function(func) |
||
| 58 | for arg, deprecated_arg in depr_args.items(): |
||
| 59 | new_metadata.add_alias(arg, deprecated_arg) |
||
| 60 | wrapping_function.__metadata__ = new_metadata |
||
| 61 | |||
| 62 | return wrapping_function |
||
| 63 | |||
| 64 | return _deprecate_decorator |
||
| 65 |