-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[MRG] Migrate raising errors from nose to pytest #321
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
[MRG] Migrate raising errors from nose to pytest #321
Conversation
Codecov Report
@@ Coverage Diff @@
## master #321 +/- ##
==========================================
+ Coverage 98% 98.19% +0.18%
==========================================
Files 66 66
Lines 3860 3980 +120
==========================================
+ Hits 3783 3908 +125
+ Misses 77 72 -5
Continue to review full report at Codecov.
|
deec149
to
c995c8b
Compare
Hello @massich! Thanks for updating the PR.
Comment last updated on August 24, 2017 at 15:40 Hours UTC |
c995c8b
to
8d2a1dd
Compare
Regarding cc: @glemaitre, @chkoar |
move to regex. for the second thing, only the semantic is necessary. that's why I like regex. |
imblearn/utils/testing.py
Outdated
Assert that a code block/function call warns ``expected_warning`` | ||
and raise a failure exception otherwise. | ||
|
||
If using Python 2.5 or above, you may use this function as a |
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.
This should change to 2.7. cc: @glemaitre
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.
and the docstring lines should be truncated to 80 characters. But I don't know if this would kill the doctest. Does anyone know it?
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 use +NORMALIZE_WHITESPACE
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 need also to use a more numpydoc way using Parameters Returns and Examples.
I would add a narrative documentation in the user guide instead of the docstring.
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.
I think that the structure of this narrative documentation should be done in a different PR (merge it before this one) and add the relevant information regarding warns
in this PR.
edit: I've created #325 for such purpose
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 make a PR adding warns + documentation if you which
imblearn/utils/testing.py
Outdated
Assert that a code block/function call warns ``expected_warning`` | ||
and raise a failure exception otherwise. | ||
|
||
If using Python 2.5 or above, you may use this function as a |
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 use +NORMALIZE_WHITESPACE
imblearn/utils/testing.py
Outdated
Assert that a code block/function call warns ``expected_warning`` | ||
and raise a failure exception otherwise. | ||
|
||
If using Python 2.5 or above, you may use this function as a |
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 need also to use a more numpydoc way using Parameters Returns and Examples.
I would add a narrative documentation in the user guide instead of the docstring.
6014b7e
to
8da92de
Compare
a46ade0
to
09a744a
Compare
I think I'll finish this PR here and ask for reviews (cc:@glemaitre, @chkoar, @mrastgoo). Refer to this for assert_no_warnings |
09a744a
to
e161fa1
Compare
e161fa1
to
66f7ea4
Compare
doc/developers_utils.rst
Outdated
... | ||
Failed: DID NOT WARN. No warnings of type (<class 'RuntimeWarning'>,) was emitted. The list of emitted warnings is: [UserWarning(<class 'UserWarning'>,)]. | ||
|
||
|
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.
remove two blanks lines
doc/developers_utils.rst
Outdated
@@ -96,3 +96,45 @@ same information as the deprecation warning as explained above. Use the | |||
On the top of all the functionality provided by scikit-learn. Imbalance-learn | |||
provides :func:`deprecate_parameter`: which is used to deprecate a sampler's | |||
parameter (attribute) by another one. | |||
|
|||
Warning management when testing |
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.
Change to "Testing utilities"
doc/developers_utils.rst
Outdated
|
||
Warning management when testing | ||
=============================== | ||
|
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.
Currently, imbalanced-learn provide a warning management utility.
This feature is going to be merge in pytest and will be removed when the pytest release will have it.
66f7ea4
to
d206aa4
Compare
You should check my previous comment. Since that you pushed they get folded and outdated but they need to be addressed. |
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.
Apart of the import ordered and the doc of warns it LGTM
doc/developers_utils.rst
Outdated
... pass | ||
Traceback (most recent call last): | ||
... | ||
Failed: DID NOT WARN. No warnings of type (<.*RuntimeWarning.*>,) was emitted. The list of emitted warnings is: []. |
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.
I did not think that doctest is an exact matching. You probably have to check in the doctest doc how to make regular expression or use the ellipsis as work around
from sklearn.utils.testing import assert_warns_message | ||
from imblearn.utils.testing import warns | ||
|
||
from pytest import raises |
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.
this import need to be before the imblearn import
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.
I would prefer under numpy in fact
from sklearn.ensemble import RandomForestClassifier | ||
|
||
from imblearn.ensemble import BalanceCascade | ||
|
||
from pytest import raises |
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.
before imblearn import (the best would be under numpy)
@@ -7,7 +7,7 @@ | |||
|
|||
import numpy as np | |||
from sklearn.utils.testing import assert_allclose, assert_array_equal | |||
from sklearn.utils.testing import assert_raises_regex | |||
from pytest import raises |
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.
move it under numpy
@@ -7,7 +7,7 @@ | |||
|
|||
import numpy as np | |||
from sklearn.utils.testing import assert_allclose, assert_array_equal | |||
from sklearn.utils.testing import assert_raises_regex | |||
from pytest import raises |
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.
move under numpy
imblearn/utils/estimator_checks.py
Outdated
@@ -18,7 +18,9 @@ | |||
as sklearn_yield_all_checks, check_estimator \ | |||
as sklearn_check_estimator, check_parameters_default_constructible | |||
from sklearn.exceptions import NotFittedError | |||
from sklearn.utils.testing import assert_warns, assert_raises_regex | |||
from pytest import raises |
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.
above the scikit-learn import
imblearn/utils/testing.py
Outdated
@@ -14,6 +14,11 @@ | |||
|
|||
from sklearn.base import BaseEstimator | |||
|
|||
from pytest import warns as _warns | |||
from contextlib import contextmanager | |||
from re import compile |
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.
the order will be:
- standard library
- 3rd party library
- own library
then
move context and re on the top
then pytest
then scikit-learn
then imblearn
imblearn/utils/testing.py
Outdated
|
||
@contextmanager | ||
def warns(expected_warning, match=None): | ||
""" |
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 need to improve the docstring
def warns(expected_warning, match=None):
"""Assert a warning is raised with an optional pattern.
Assert that a code block/function call warns ``expected_warning``
and raise a failure exception otherwise. It can be used within a
context manager ``with``.
Parameters
----------
expected_warning : Warning
Warning type.
match : regex str or None, optional
The pattern to be checked. By default, no check is done.
Returns
-------
None
"""
Check the english because it is on the top of the head
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 add a small example as well.
from sklearn.utils.validation import check_X_y, check_array | ||
|
||
from imblearn.utils.estimator_checks import check_estimator | ||
|
||
from pytest import raises |
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.
above sklearn
from sklearn.utils.testing import assert_raises_regex | ||
from sklearn.utils.testing import assert_warns_message | ||
from imblearn.utils.testing import warns | ||
from pytest import raises |
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.
above sklearn
d206aa4
to
a127230
Compare
a127230
to
2077fca
Compare
47ea290
to
dd2b0db
Compare
1acfd30
to
4be2547
Compare
ping @glemaitre |
good to be merge when green |
Reference Issue
Partial migration from nose to pytest (see #323)
What does this implement/fix? Explain your changes.
Things related to raising. The idea is to migrate or change the following:
from sklearn.utils.testing import assert_raises
from sklearn.utils.testing import assert_raises_regex
from sklearn.utils.testing import assert_raise_message
from sklearn.utils.testing import assert_warns
from sklearn.utils.testing import assert_warns_message
* [ ]from sklearn.utils.testing import assert_no_warnings
Without touching any testing logic.
Any other comments?
Do not squash this PR (I've reworked the git history for easier inspection). And I would recommend to review each commit of the PR independently.