Skip to content

BUG Ignores tags when estimator is a class in parametrize_with_checks #16709

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 10 commits into from
Mar 18, 2020
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
19 changes: 19 additions & 0 deletions sklearn/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from sklearn.utils import IS_PYPY
from sklearn.utils._testing import SkipTest
from sklearn.utils.estimator_checks import (
_mark_xfail_checks,
_construct_instance,
_set_checking_parameters,
_set_check_estimator_ids,
Expand All @@ -47,6 +48,24 @@ def test_all_estimator_no_base_class():
assert not name.lower().startswith('base'), msg


def test_estimator_cls_parameterize_with_checks():
# Non-regression test for #16707 to ensure that parametrize_with_checks
# works with estimator classes
param_checks = parametrize_with_checks([LogisticRegression])
# Using the generator does not raise
list(param_checks.args[1])


def test_mark_xfail_checks_with_unconsructable_estimator():
class MyEstimator:
def __init__(self):
raise ValueError("This is bad")

estimator, check = _mark_xfail_checks(MyEstimator, 42, None)
assert estimator == MyEstimator
assert check == 42


@pytest.mark.parametrize(
'name, Estimator',
all_estimators()
Expand Down
11 changes: 10 additions & 1 deletion sklearn/utils/estimator_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,17 @@ def _generate_class_checks(Estimator):

def _mark_xfail_checks(estimator, check, pytest):
"""Mark estimator check pairs with xfail"""
if isinstance(estimator, type):
# try to construct estimator to get tags, if it is unable to then
# return the estimator class
try:
xfail_checks = _safe_tags(_construct_instance(estimator),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_construct_instance is potentially costly, but I also don't see a way around it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not concerned given our conventions

'_xfail_test')
except Exception:
return estimator, check
else:
xfail_checks = _safe_tags(estimator, '_xfail_test')

xfail_checks = _safe_tags(estimator, '_xfail_test')
if not xfail_checks:
return estimator, check

Expand Down