Skip to content

Conversation

@sameshl
Copy link
Contributor

@sameshl sameshl commented Aug 21, 2019

replaced assert_raises and assert_raises_regex with pytest.raises context manager.

related to #14216

@glemaitre
Copy link
Member

Same as other PR: assert_raise_message needs to be repalaced.

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,
Copy link
Member

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

Copy link
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.

Otherwise LGTM

@sameshl
Copy link
Contributor Author

sameshl commented Aug 28, 2019

@glemaitre I removed all instances of assert_raise_message except at

C_sparse = assert_raise_message(ValueError,
. Could you tell me how I could proceed there?

@rth
Copy link
Member

rth commented Aug 29, 2019

C_sparse = assert_raise_message(ValueError,

@sameshl That looks like a typo; the resulting C_sparse variable is never used, you can remove it.

@sameshl
Copy link
Contributor Author

sameshl commented Aug 29, 2019

@sameshl That looks like a typo; the resulting C_sparse variable is never used, you can remove it.

I thought the same, but felt it might me better if I ask someone before proceding.

rth
rth previously approved these changes Aug 29, 2019
Copy link
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.

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)):
Copy link
Member

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).

Copy link
Contributor Author

@sameshl sameshl Aug 29, 2019

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).

@rth
But I thought this way is better for readability. What do you think?

Copy link
Member

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)):
Copy link
Member

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))):
Copy link
Member

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))):
Copy link
Member

Choose a reason for hiding this comment

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

Same

@rth rth dismissed their stale review August 29, 2019 11:46

I meant to say that my comments above still need addressing before this PR is merged.

@sameshl
Copy link
Contributor Author

sameshl commented Aug 30, 2019

@rth I have made the changes.

@sameshl
Copy link
Contributor Author

sameshl commented Sep 7, 2019

@thomasjpfan Review this as well. Thanks!

Copy link
Member

@thomasjpfan thomasjpfan left a comment

Choose a reason for hiding this comment

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

LGTM

@rth rth dismissed glemaitre’s stale review September 8, 2019 12:11

Comments were addressed.

Copy link
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 changed the title MAINT:Fix assert raises in sklearn/metrics/cluster/tests/ TST: Fix assert raises in sklearn/metrics/cluster/tests/ Sep 8, 2019
@rth rth merged commit 7e8405c into scikit-learn:master Sep 8, 2019
@sameshl sameshl deleted the tests_metrics_cluster branch September 8, 2019 14:05
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