Thanks to visit codestin.com
Credit goes to github.com

Skip to content

MAINT Make param validation more lenient towards downstream dependencies #25088

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions sklearn/utils/_param_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,6 @@ def validate_parameter_constraints(parameter_constraints, params, caller_name):
caller_name : str
The name of the estimator or function or method that called this function.
"""
if len(set(parameter_constraints) - set(params)) != 0:
raise ValueError(
f"The parameter constraints {list(parameter_constraints)}"
" contain unexpected parameters"
f" {set(parameter_constraints) - set(params)}"
)

for param_name, param_val in params.items():
# We allow parameters to not have a constraint so that third party estimators
# can inherit from sklearn estimators without having to necessarily use the
Expand Down
33 changes: 19 additions & 14 deletions sklearn/utils/tests/test_param_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,20 +453,6 @@ def test_validate_params():
_func(0, *[1, 2, 3], c="four", **{"e": 5})


def test_validate_params_match_error():
"""Check that an informative error is raised when there are constraints
that have no matching function paramaters
"""

@validate_params({"a": [int], "c": [int]})
def func(a, b):
pass

match = r"The parameter constraints .* contain unexpected parameters {'c'}"
with pytest.raises(ValueError, match=match):
func(1, 2)


def test_validate_params_missing_params():
"""Check that no error is raised when there are parameters without
constraints
Expand Down Expand Up @@ -633,3 +619,22 @@ def test_cv_objects():
assert constraint.is_satisfied_by([([1, 2], [3, 4]), ([3, 4], [1, 2])])
assert constraint.is_satisfied_by(None)
assert not constraint.is_satisfied_by("not a CV object")


def test_third_party_estimator():
"""Check that the validation from a scikit-learn estimator inherited by a third
party estimator does not impose a match between the dict of constraints and the
parameters of the estimator.
"""

class ThirdPartyEstimator(_Estimator):
def __init__(self, b):
self.b = b
super().__init__(a=0)

def fit(self, X=None, y=None):
super().fit(X, y)

# does not raise, even though "b" is not in the constraints dict and "a" is not
# a parameter of the estimator.
ThirdPartyEstimator(b=0).fit()