-
-
Notifications
You must be signed in to change notification settings - Fork 26.5k
TST: Fix assert raises in sklearn/metrics/cluster/tests/
#14707
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
Conversation
|
Same as other PR: |
| r'Number of labels is %d\. Valid values are 2 ' | ||
| r'to n_samples - 1 \(inclusive\)' % len(np.unique(y)), | ||
| silhouette_score, X, y) | ||
| with pytest.raises(ValueError, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can put the message out of raises
glemaitre
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise LGTM
|
@glemaitre I removed all instances of
|
@sameshl That looks like a typo; the resulting |
I thought the same, but felt it might me better if I ask someone before proceding. |
rth
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few comments, otherwise LGTM. Thanks!
| expected = "labels_true must be 1D: shape is (2" | ||
| assert_raise_message(ValueError, expected, score_func, | ||
| [[0, 1], [1, 0]], [1, 1, 1]) | ||
| with pytest.raises(ValueError, match=re.escape(expected)): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better to manually escape with raw strings the message above. (and then remove the re import above).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better to manually escape with raw strings the message above. (and then remove the
reimport above).
@rth
But I thought this way is better for readability. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well readers won't necessarily know exactly what re.escape does without checking the docs. I think explicitly escaping the string simpler to understand.
| expected = "labels_pred must be 1D: shape is (2" | ||
| assert_raise_message(ValueError, expected, score_func, | ||
| [0, 1, 0], [[1, 1], [0, 0]]) | ||
| with pytest.raises(ValueError, match=re.escape(expected)): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above
| silhouette_score, X, y) | ||
| with pytest.raises(ValueError, | ||
| match=r'Number of labels is %d\. Valid values are 2 ' | ||
| r'to n_samples - 1 \(inclusive\)' % len(np.unique(y))): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please put the message above,
msg = (r'..')
with pytest.raises(ValueError, match=msg):
| silhouette_score, X, y) | ||
| with pytest.raises(ValueError, | ||
| match=r'Number of labels is %d\. Valid values are 2 ' | ||
| r'to n_samples - 1 \(inclusive\)' % len(np.unique(y))): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same
I meant to say that my comments above still need addressing before this PR is merged.
|
@rth I have made the changes. |
|
@thomasjpfan Review this as well. Thanks! |
thomasjpfan
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
rth
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @sameshl !
sklearn/metrics/cluster/tests/sklearn/metrics/cluster/tests/
replaced
assert_raisesandassert_raises_regexwithpytest.raisescontext manager.related to #14216