10000 MAINT move deprecated into deprecation.py · scikit-learn/scikit-learn@61ae8d5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 61ae8d5

Browse files
committed
MAINT move deprecated into deprecation.py
1 parent e19a981 commit 61ae8d5

File tree

4 files changed

+92
-89
lines changed

4 files changed

+92
-89
lines changed

sklearn/base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from scipy import sparse
1010
from .externals import six
1111
from .utils.fixes import signature
12+
from .utils.deprecation import deprecated
1213
from .exceptions import ChangedBehaviorWarning
1314

1415

sklearn/utils/__init__.py

Lines changed: 5 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
check_random_state, column_or_1d, check_array,
1414
check_consistent_length, check_X_y, indexable,
1515
check_symmetric)
16+
from .deprecation import deprecated
1617
from .class_weight import compute_class_weight, compute_sample_weight
1718
from ..externals.joblib import cpu_count
18-
from .exceptions import ConvergenceWarning
19-
from .exceptions import DataConversionWarning as DataConversionWarning_
20-
from .utils import deprecated
19+
from ..exceptions import ConvergenceWarning
20+
from ..exceptions import DataConversionWarning as DataConversionWarning_
2121

2222

2323
ConvergenceWarning = deprecated("ConvergenceWarning has been moved "
@@ -39,88 +39,6 @@
3939
"check_symmetric"]
4040

4141

42-
class deprecated(object):
43-
"""Decorator to mark a function or class as deprecated.
44-
45-
Issue a warning when the function is called/the class is instantiated and
46-
adds a warning to the docstring.
47-
48-
The optional extra argument will be appended to the deprecation message
49-
and the docstring. Note: to use this with the default value for extra, put
50-
in an empty of parentheses:
51-
52-
>>> from sklearn.utils import deprecated
53-
>>> deprecated() # doctest: +ELLIPSIS
54-
<sklearn.utils.deprecated object at ...>
55-
56-
>>> @deprecated()
57-
... def some_function(): pass
58-
"""
59-
60-
# Adapted from http://wiki.python.org/moin/PythonDecoratorLibrary,
61-
# but with many changes.
62-
63-
def __init__(self, extra=''):
64-
"""
65-
Parameters
66-
----------
67-
extra: string
68-
to be added to the deprecation messages
69-
70-
"""
71-
self.extra = extra
72-
73-
def __call__(self, obj):
74-
if isinstance(obj, type):
75-
return self._decorate_class(obj)
76-
else:
77-
return self._decorate_fun(obj)
78-
79-
def _decorate_class(self, cls):
80-
msg = "Class %s is deprecated" % cls.__name__
81-
if self.extra:
82-
msg += "; %s" % self.extra
83-
84-
# FIXME: we should probably reset __new__ for full generality
85-
init = cls.__init__
86-
87-
def wrapped(*args, **kwargs):
88-
warnings.warn(msg, category=DeprecationWarning)
89-
return init(*args, **kwargs)
90-
cls.__init__ = wrapped
91-
92-
wrapped.__name__ = '__init__'
93-
wrapped.__doc__ = self._update_doc(init.__doc__)
94-
wrapped.deprecated_original = init
95-
96-
return cls
97-
98-
def _decorate_fun(self, fun):
99-
"""Decorate function fun"""
100-
101-
msg = "Function %s is deprecated" % fun.__name__
102-
if self.extra:
103-
msg += "; %s" % self.extra
104-
105-
def wrapped(*args, **kwargs):
106-
warnings.warn(msg, category=DeprecationWarning)
107-
return fun(*args, **kwargs)
108-
109-
wrapped.__name__ = fun.__name__
110-
wrapped.__dict__ = fun.__dict__
111-
wrapped.__doc__ = self._update_doc(fun.__doc__)
112-
113-
return wrapped
114-
115-
def _update_doc(self, olddoc):
116-
F438 newdoc = "DEPRECATED"
117-
if self.extra:
118-
newdoc = "%s: %s" % (newdoc, self.extra)
119-
if olddoc:
120-
newdoc = "%s\n\n%s" % (newdoc, olddoc)
121-
return newdoc
122-
123-
12442
def safe_mask(X, mask):
12543
"""Return a mask which is safe to use on X.
12644
@@ -422,7 +340,8 @@ def gen_even_slices(n, n_packs, n_samples=None):
422340
"""
423341
start = 0
424342
if n_packs < 1:
425-
raise ValueError("gen_even_slices got n_packs=%s, must be >=1" % n_packs)
343+
raise ValueError("gen_even_slices got n_packs=%s, must be >=1"
344+
% n_packs)
426345
for pack_num in range(n_packs):
427346
this_n = n // n_packs
428347
if pack_num < n % n_packs:
@@ -482,5 +401,3 @@ def tosequence(x):
482401
return x
483402
else:
484403
return list(x)
485-
486-

sklearn/utils/deprecation.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import warnings
2+
3+
__all__ = ["deprecated", ]
4+
5+
6+
class deprecated(object):
7+
"""Decorator to mark a function or class as deprecated.
8+
9+
Issue a warning when the function is called/the class is instantiated and
10+
adds a warning to the docstring.
11+
12+
The optional extra argument will be appended to the deprecation message
13+
and the docstring. Note: to use this with the default value for extra, put
14+
in an empty of parentheses:
15+
16+
>>> from sklearn.utils import deprecated
17+
>>> deprecated() # doctest: +ELLIPSIS
18+
<sklearn.utils.deprecation.deprecated object at ...>
19+
20+
>>> @deprecated()
21+
... def some_function(): pass
22+
"""
23+
24+
# Adapted from http://wiki.python.org/moin/PythonDecoratorLibrary,
25+
# but with many changes.
26+
27+
def __init__(self, extra=''):
28+
"""
29+
Parameters
30+
----------
31+
extra: string
32+
to be added to the deprecation messages
33+
34+
"""
35+
self.extra = extra
36+
37+
def __call__(self, obj):
38+
if isinstance(obj, type):
39+
return self._decorate_class(obj)
40+
else:
41+
return self._decorate_fun(obj)
42+
43+
def _decorate_class(self, cls):
44+
msg = "Class %s is deprecated" % cls.__name__
45+
if self.extra:
46+
msg += "; %s" % self.extra
47+
48+
# FIXME: we should probably reset __new__ for full generality
49+
init = cls.__init__
50+
51+
def wrapped(*args, **kwargs):
52+
warnings.warn(msg, category=DeprecationWarning)
53+
return init(*args, **kwargs)
54+
cls.__init__ = wrapped
55+
56+
wrapped.__name__ = '__init__'
57+
wrapped.__doc__ = self._update_doc(init.__doc__)
58+
wrapped.deprecated_original = init
59+
60+
return cls
61+
62+
def _decorate_fun(self, fun):
63+
"""Decorate function fun"""
64+
65+
msg = "Function %s is deprecated" % fun.__name__
66+
if self.extra:
67+
msg += "; %s" % self.extra
68+
69+
def wrapped(*args, **kwargs):
70+
warnings.warn(msg, category=DeprecationWarning)
71+
return fun(*args, **kwargs)
72+
73+
wrapped.__name__ = fun.__name__
74+
wrapped.__dict__ = fun.__dict__
75+
wrapped.__doc__ = self._update_doc(fun.__doc__)
76+
77+
return wrapped
78+
79+
def _update_doc(self, olddoc):
80+
newdoc = "DEPRECATED"
81+
if self.extra:
82+
newdoc = "%s: %s" % (newdoc, self.extra)
83+
if olddoc:
84+
newdoc = "%s\n\n%s" % (newdoc, olddoc)
85+
return newdoc

sklearn/utils/validation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from ..externals import six
1616
from ..utils.fixes import signature
17-
from . import deprecated
17+
from .deprecation import deprecated
1818
from ..exceptions import DataConversionWarning as DataConversionWarning_
1919
from ..exceptions import NonBLASDotWarning
2020
from ..exceptions import NotFittedError as NotFittedError_

0 commit comments

Comments
 (0)
0