Skip to content

FIX Fixes tags for passthrough in Pipeline #18820

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

Closed
wants to merge 1 commit into from
Closed
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: 5 additions & 2 deletions sklearn/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,8 +623,11 @@ def classes_(self):

def _more_tags(self):
# check if first estimator expects pairwise input
estimator_tags = self.steps[0][1]._get_tags()
return {'pairwise': estimator_tags.get('pairwise', False)}
try:
pairwise = self.steps[0][1]._get_tags().get('pairwise', False)
Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer just trying hasattr because otherwise it's not clear what may raise the AttributeError (could be e.g. self.steps).

Also about .get('pairwise', False): I think it's fair to assume that if _get_tags exists, then the key will exist.

Copy link
Member Author

Choose a reason for hiding this comment

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

Also about .get('pairwise', False): I think it's fair to assume that if _get_tags exists, then the key will exist.

This opens the discussion of "Which tags should we expect to exist". This becomes a bigger question when BaseEstimator is not expected to be inherited.

Copy link
Member

Choose a reason for hiding this comment

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

Which tags should we expect to exist

If _get_tags() exists, then all of them should exist I'd say

Also, I'm uncomfortable with having the tag's default defined twice: here, and in sklearn.base. The default might not change anytime soon, but this is still a pattern that we should avoid

Copy link
Member Author

Choose a reason for hiding this comment

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

If _get_tags() exists, then all of them should exist I'd say

This feels like something to be tested in check_estimator for "api-only".

except AttributeError:
pairwise = False
return {'pairwise': pairwise}

# TODO: Remove in 0.26
# mypy error: Decorated property not supported
Expand Down
8 changes: 8 additions & 0 deletions sklearn/tests/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -1264,3 +1264,11 @@ def test_feature_union_warns_unknown_transformer_weight():
union = FeatureUnion(transformer_list, transformer_weights=weights)
with pytest.raises(ValueError, match=expected_msg):
union.fit(X, y)


@pytest.mark.parametrize('passthrough', [None, 'passthrough'])
def test_pipeline_get_tags_none(passthrough):
"""Checks that tags are set correctly when passthrough is None or
'passthrough'"""
pipe = make_pipeline(passthrough, SVC())
assert not pipe._get_tags()['pairwise']