coalib.bearlib package

Subpackages

Module contents

The bearlib is an optional library designed to ease the task of any Bear. Just as the rest of coala the bearlib is designed to be as easy to use as possible while offering the best possible flexibility.

coalib.bearlib.deprecate_bear(bear)[source]

Use this to deprecate a bear. Say we have a bear:

>>> class SomeBear:
...     def run(*args):
...         print("I'm running!")

To change the name from SomeOldBear to SomeBear you can keep the SomeOldBear.py around with those contents:

>>> @deprecate_bear
... class SomeOldBear(SomeBear): pass

Now let’s run the bear:

>>> import sys
>>> logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
>>> SomeOldBear().run()
WARNING:root:The bear SomeOldBear is deprecated. Use SomeBear instead!
I'm running!
Parameters:bear – An old bear class that inherits from the new one (so it gets its methods and can just contain a pass.)
Returns:A bear class that warns about deprecation on use.
coalib.bearlib.deprecate_settings(**depr_args)[source]

The purpose of this decorator is to allow passing old settings names to bears due to the heavy changes in their names.

>>> @deprecate_settings(new='old')
... def run(new):
...     print(new)

Now we can simply call the bear with the deprecated setting, we’ll get a warning - but it still works!

>>> import sys
>>> logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
>>> run(old="Hello world!")
WARNING:root:The setting `old` is deprecated. Please use `new` instead.
Hello world!
>>> run(new="Hello world!")
Hello world!

This example represents the case where the old setting name needs to be modified to match the new one.

>>> @deprecate_settings(new=('old', lambda a: a + 'coala!'))
... def func(new):
...     print(new)
>>> func(old="Welcome to ")
WARNING:root:The setting `old` is deprecated. Please use `new` instead.
Welcome to coala!
>>> func(new='coala!')
coala!

This example represents the case where the old and new settings are provided to the function.

>>> @deprecate_settings(new='old')
... def run(new):
...     print(new)
>>> 
... run(old="Hello!", new='coala is always written with lowercase `c`.')
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!

Multiple deprecations can be provided for the same setting. The modifier function for each deprecated setting can be given as a value in a dict where the deprecated setting is the key. A default modifier may be specified at the end of the deprecated settings tuple.

>>> @deprecate_settings(new=({'old': lambda x: x + ' coala!'},
...                          'older',
...                           lambda x: x + '!' ))
... def run(new):
...     print(new)
>>> run(old='Hi')
WARNING:root:The setting `old` is deprecated. Please use `new` instead.
Hi coala!
>>> run(older='Hi')
WARNING:root:The setting `older` is deprecated. Please use `new` instead.
Hi!

The metadata for coala has been adjusted as well:

>>> list(run.__metadata__.non_optional_params.keys())
['new']
>>> list(run.__metadata__.optional_params.keys())
['old', 'older']
Parameters:depr_args – A dictionary of settings as keys and their deprecated names as values.