Skip to content

Commit

Permalink
bearlib: Cache deprecation message
Browse files Browse the repository at this point in the history
Annoying when the same message is logged hundreds of times.

Fixes coala#3165
  • Loading branch information
adtac authored and sils committed Dec 10, 2016
1 parent 4da8bd6 commit a22ec25
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions coalib/bearlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,17 @@ def deprecate_settings(**depr_args):
WARNING:root:The setting `old` is deprecated. Please use `new` instead.
WARNING:root:The value of `old` and `new` are conflicting. `new` will...
coala is always written with lowercase `c`.
>>> @deprecate_settings(new='old')
... def run(new):
... print(new)
>>> run(old='Hello!', new='Hello!')
WARNING:root:The setting `old` is deprecated. Please use `new` instead.
Hello!
Note that messages are cached. So the same message won't be printed twice:
>>> run(old='Hello!', new='Hello!')
Hello!
The metadata for coala has been adjusted as well:
>>> list(run.__metadata__.non_optional_params.keys())
Expand All @@ -72,16 +79,20 @@ def deprecate_settings(**depr_args):
"""
def _deprecate_decorator(func):

logged_deprecated_args = set()

def wrapping_function(*args, **kwargs):
for arg, depr_arg_and_modifier in depr_args.items():
deprecated_arg, _func = (
depr_arg_and_modifier
if isinstance(depr_arg_and_modifier, tuple)
else (depr_arg_and_modifier, lambda x: x))
if deprecated_arg in kwargs:
logging.warning(
'The setting `{}` is deprecated. Please use `{}` '
'instead.'.format(deprecated_arg, arg))
if deprecated_arg not in logged_deprecated_args:
logging.warning(
'The setting `{}` is deprecated. Please use `{}` '
'instead.'.format(deprecated_arg, arg))
logged_deprecated_args.add(deprecated_arg)
depr_arg_value = _func.__call__(kwargs[deprecated_arg])
if arg in kwargs and depr_arg_value != kwargs[arg]:
logging.warning(
Expand Down

0 comments on commit a22ec25

Please sign in to comment.