Skip to content

Commit d083972

Browse files
authored
FIX signature of deprecated classes (scikit-learn#30145)
1 parent 429d67a commit d083972

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

sklearn/utils/deprecation.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import functools
55
import warnings
6+
from inspect import signature
67

78
__all__ = ["deprecated"]
89

@@ -64,17 +65,21 @@ def _decorate_class(self, cls):
6465
msg += "; %s" % self.extra
6566

6667
new = cls.__new__
68+
sig = signature(cls)
6769

6870
def wrapped(cls, *args, **kwargs):
6971
warnings.warn(msg, category=FutureWarning)
7072
if new is object.__new__:
7173
return object.__new__(cls)
74+
7275
return new(cls, *args, **kwargs)
7376

7477
cls.__new__ = wrapped
7578

7679
wrapped.__name__ = "__new__"
7780
wrapped.deprecated_original = new
81+
# Restore the original signature, see PEP 362.
82+
cls.__signature__ = sig
7883

7984
return cls
8085

sklearn/utils/tests/test_deprecation.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44

55
import pickle
6+
from inspect import signature
67

78
import pytest
89

@@ -86,3 +87,12 @@ def test_is_deprecated():
8687

8788
def test_pickle():
8889
pickle.loads(pickle.dumps(mock_function))
90+
91+
92+
def test_deprecated_class_signature():
93+
@deprecated()
94+
class MockClass:
95+
def __init__(self, a, b=1, c=2):
96+
pass
97+
98+
assert list(signature(MockClass).parameters.keys()) == ["a", "b", "c"]

0 commit comments

Comments
 (0)