BUG: Use manager to set title
1 |
""" Module to help with deprecating objects and classes
|
|
2 |
"""
|
|
3 |
|
|
4 | 33 |
import warnings |
5 |
|
|
6 | 33 |
from .deprecator import Deprecator |
7 | 33 |
from .pkg_info import cmp_pkg_version |
8 |
|
|
9 |
|
|
10 | 33 |
class ModuleProxy(object): |
11 |
""" Proxy for module that may not yet have been imported
|
|
12 |
|
|
13 |
Parameters
|
|
14 |
----------
|
|
15 |
module_name : str
|
|
16 |
Full module name e.g. ``nibabel.minc``
|
|
17 |
|
|
18 |
Examples
|
|
19 |
--------
|
|
20 |
|
|
21 |
::
|
|
22 |
arr = np.arange(24).reshape((2, 3, 4))
|
|
23 |
nifti1 = ModuleProxy('nibabel.nifti1')
|
|
24 |
nifti1_image = nifti1.Nifti1Image(arr, np.eye(4))
|
|
25 |
|
|
26 |
So, the ``nifti1`` object is a proxy that will import the required module
|
|
27 |
when you do attribute access and return the attributes of the imported
|
|
28 |
module.
|
|
29 |
"""
|
|
30 |
|
|
31 | 33 |
def __init__(self, module_name): |
32 | 33 |
self._module_name = module_name |
33 |
|
|
34 | 33 |
def __getattr__(self, key): |
35 | 33 |
mod = __import__(self._module_name, fromlist=['']) |
36 | 33 |
return getattr(mod, key) |
37 |
|
|
38 | 33 |
def __repr__(self): |
39 | 33 |
return f"<module proxy for {self._module_name}>" |
40 |
|
|
41 |
|
|
42 | 33 |
class FutureWarningMixin(object): |
43 |
""" Insert FutureWarning for object creation
|
|
44 |
|
|
45 |
Examples
|
|
46 |
--------
|
|
47 |
>>> class C(object): pass
|
|
48 |
>>> class D(FutureWarningMixin, C):
|
|
49 |
... warn_message = "Please, don't use this class"
|
|
50 |
|
|
51 |
Record the warning
|
|
52 |
|
|
53 |
>>> with warnings.catch_warnings(record=True) as warns:
|
|
54 |
... d = D()
|
|
55 |
... warns[0].message.args[0]
|
|
56 |
"Please, don't use this class"
|
|
57 |
"""
|
|
58 | 33 |
warn_message = 'This class will be removed in future versions' |
59 |
|
|
60 | 33 |
def __init__(self, *args, **kwargs): |
61 | 33 |
warnings.warn(self.warn_message, |
62 |
FutureWarning, |
|
63 |
stacklevel=2) |
|
64 | 33 |
super(FutureWarningMixin, self).__init__(*args, **kwargs) |
65 |
|
|
66 |
|
|
67 | 33 |
class VisibleDeprecationWarning(UserWarning): |
68 |
""" Deprecation warning that will be shown by default
|
|
69 |
|
|
70 |
Python >= 2.7 does not show standard DeprecationWarnings by default:
|
|
71 |
|
|
72 |
http://docs.python.org/dev/whatsnew/2.7.html#the-future-for-python-2-x
|
|
73 |
|
|
74 |
Use this class for cases where we do want to show deprecations by default.
|
|
75 |
"""
|
|
76 | 33 |
pass
|
77 |
|
|
78 |
|
|
79 | 33 |
deprecate_with_version = Deprecator(cmp_pkg_version) |
Read our documentation on viewing source code .