Skip to content

Commit f82525e

Browse files
authored
CLN Deprecations position args in PartialDependenceDisplay.plot (scikit-learn#18293)
1 parent eaa45c8 commit f82525e

File tree

4 files changed

+55
-32
lines changed

4 files changed

+55
-32
lines changed

doc/whats_new/v0.24.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ Changelog
9393
When set to `True`, additional messages will be displayed which can aid with
9494
debugging. :pr:`18052` by :user:`Sean O. Stalley <sstalley>`.
9595

96-
- |Enhancement| Added :func:`cluster.kmeans_plusplus` as public function.
96+
- |Enhancement| Added :func:`cluster.kmeans_plusplus` as public function.
9797
Initialization by KMeans++ can now be called separately to generate
9898
initial cluster centroids. :pr:`17937` by :user:`g-walsh`
9999

@@ -373,6 +373,10 @@ Changelog
373373
:func:`inspection.permutation_importance`. :pr:`16906` by
374374
:user:`Roei Kahny <RoeiKa>`.
375375

376+
- |API| Positional arguments are deprecated in
377+
:meth:`inspection.PartialDependenceDisplay.plot` and will error in 0.26.
378+
:pr:`18293` by `Thomas Fan`_.
379+
376380
:mod:`sklearn.isotonic`
377381
.......................
378382

@@ -741,7 +745,7 @@ Changelog
741745
when `handle_unknown='error'` and `drop=None` for samples
742746
encoded as all zeros. :pr:`14982` by
743747
:user:`Kevin Winata <kwinata>`.
744-
748+
745749
:mod:`sklearn.semi_supervised`
746750
..............................
747751

sklearn/inspection/_plot/partial_dependence.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,8 @@ def _plot_two_way_partial_dependence(
817817
ax.set_xlabel(self.feature_names[feature_idx[0]])
818818
ax.set_ylabel(self.feature_names[feature_idx[1]])
819819

820-
def plot(self, ax=None, n_cols=3, line_kw=None, contour_kw=None):
820+
@_deprecate_positional_args(version="0.26")
821+
def plot(self, *, ax=None, n_cols=3, line_kw=None, contour_kw=None):
821822
"""Plot partial dependence plots.
822823
823824
Parameters

sklearn/utils/tests/test_validation.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,6 +1168,16 @@ def f3(a, *, b, c=1, d=1):
11681168
f3(1, 2)
11691169

11701170

1171+
def test_deprecate_positional_args_warns_for_function_version():
1172+
@_deprecate_positional_args(version="0.26")
1173+
def f1(a, *, b):
1174+
pass
1175+
1176+
with pytest.warns(FutureWarning,
1177+
match=r"From version 0.26 passing these as positional"):
1178+
f1(1, 2)
1179+
1180+
11711181
def test_deprecate_positional_args_warns_for_class():
11721182

11731183
class A1:

sklearn/utils/validation.py

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,44 +32,52 @@
3232
FLOAT_DTYPES = (np.float64, np.float32, np.float16)
3333

3434

35-
def _deprecate_positional_args(f):
35+
def _deprecate_positional_args(func=None, *, version="0.25"):
3636
"""Decorator for methods that issues warnings for positional arguments.
3737
3838
Using the keyword-only argument syntax in pep 3102, arguments after the
3939
* will issue a warning when passed as a positional argument.
4040
4141
Parameters
4242
----------
43-
f : callable
43+
func : callable, default=None
4444
Function to check arguments on.
45+
version : callable, default="0.25"
46+
The version when positional arguments will result in error.
4547
"""
46-
sig = signature(f)
47-
kwonly_args = []
48-
all_args = []
49-
50-
for name, param in sig.parameters.items():
51-
if param.kind == Parameter.POSITIONAL_OR_KEYWORD:
52-
all_args.append(name)
53-
elif param.kind == Parameter.KEYWORD_ONLY:
54-
kwonly_args.append(name)
55-
56-
@wraps(f)
57-
def inner_f(*args, **kwargs):
58-
extra_args = len(args) - len(all_args)
59-
if extra_args <= 0:
60-
return f(*args, **kwargs)
61-
62-
# extra_args > 0
63-
args_msg = ['{}={}'.format(name, arg)
64-
for name, arg in zip(kwonly_args[:extra_args],
65-
args[-extra_args:])]
66-
warnings.warn("Pass {} as keyword args. From version 0.25 "
67-
"passing these as positional arguments will "
68-
"result in an error".format(", ".join(args_msg)),
69-
FutureWarning)
70-
kwargs.update(zip(sig.parameters, args))
71-
return f(**kwargs)
72-
return inner_f
48+
def _inner_deprecate_positional_args(f):
49+
sig = signature(f)
50+
kwonly_args = []
51+
all_args = []
52+
53+
for name, param in sig.parameters.items():
54+
if param.kind == Parameter.POSITIONAL_OR_KEYWORD:
55+
all_args.append(name)
56+
elif param.kind == Parameter.KEYWORD_ONLY:
57+
kwonly_args.append(name)
58+
59+
@wraps(f)
60+
def inner_f(*args, **kwargs):
61+
extra_args = len(args) - len(all_args)
62+
if extra_args <= 0:
63+
return f(*args, **kwargs)
64+
65+
# extra_args > 0
66+
args_msg = ['{}={}'.format(name, arg)
67+
for name, arg in zip(kwonly_args[:extra_args],
68+
args[-extra_args:])]
69+
args_msg = ", ".join(args_msg)
70+
warnings.warn(f"Pass {args_msg} as keyword args. From version "
71+
f"{version} passing these as positional arguments "
72+
"will result in an error", FutureWarning)
73+
kwargs.update(zip(sig.parameters, args))
74+
return f(**kwargs)
75+
return inner_f
76+
77+
if func is not None:
78+
return _inner_deprecate_positional_args(func)
79+
80+
return _inner_deprecate_positional_args
7381

7482

7583
def _assert_all_finite(X, allow_nan=False, msg_dtype=None):

0 commit comments

Comments
 (0)