Skip to content

MAINT:Fix assert raises in sklearn/tree/tests/#14737

Merged
rth merged 6 commits intoscikit-learn:masterfrom
sameshl:tests_tree
Aug 28, 2019
Merged

MAINT:Fix assert raises in sklearn/tree/tests/#14737
rth merged 6 commits intoscikit-learn:masterfrom
sameshl:tests_tree

Conversation

@sameshl
Copy link
Copy Markdown
Contributor

@sameshl sameshl commented Aug 23, 2019

replaced assert_raises and assert_raises_regex with pytest.raises context manager.

related to #14216

@glemaitre
Copy link
Copy Markdown
Member

As a general comment, could you remove assert_raises_message as well. If match contains the full message, the it will be equivalent.

Copy link
Copy Markdown
Member

@glemaitre glemaitre left a comment

Choose a reason for hiding this comment

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

Apart of the assert_raises_message, LGTM.

# use values of max_features that are invalid
est = TreeEstimator(max_features=10)
assert_raises(ValueError, est.fit, X, y)
with pytest.raises(ValueError):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We should open an issue to refactor theses tests as well to use parametrization.
It might be easier to read. But let's keep it for another PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, that is a good point. We should do that

@sameshl
Copy link
Copy Markdown
Contributor Author

sameshl commented Aug 26, 2019

@glemaitre Can you help me figure out why the tests are failing in build? It doesn't look like it is due to a change I made. Thanks!

"Got {!r} instead.".format(allowed_presort, invalid_presort))
est = cls(presort=invalid_presort)
assert_raise_message(ValueError, msg, est.fit, X, y)
with pytest.raises(ValueError, match=msg):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this one fails because:

The regexp parameter of the match method is matched with the re.search function, so in the above example match='123' would have worked as well.

whereas before it was a simple string comparison, I think.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@adrinjalali So can you tell me the correct way of writing this case using pytest.raises?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In this case:

    with pytest.raises(ValueError, match=msg.replace('(', '\(').replace(')', '\)')):

would work, but I'm not sure if it's the most elegant solution.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Indeed a bit cumbersome, but nothing better comes immediatiely to mind.

@sameshl
Copy link
Copy Markdown
Contributor Author

sameshl commented Aug 28, 2019

@glemaitre I have made the required changes. I also resolved the matching conflict as suggested by @adrinjalali here.

Let me know if any other changes are required.

Copy link
Copy Markdown
Member

@rth rth left a comment

Choose a reason for hiding this comment

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

Thanks @sameshl !

@rth rth merged commit e74e032 into scikit-learn:master Aug 28, 2019
@sameshl
Copy link
Copy Markdown
Contributor Author

sameshl commented Aug 28, 2019

Thanks @rth!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants