- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 19.2k
 
TST: assert_produces_warning works with filterwarnings #25721
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
TST: assert_produces_warning works with filterwarnings #25721
Conversation
Previously, assert_produces_warning did not play well with pytest's
filterwarnings.
```
===================================================================================== FAILURES ======================================================================================
____________________________________________________________________ test_assert_produces_warning_honors_filter _____________________________________________________________________
    @pytest.mark.filterwarnings('ignore:f1:FutureWarning')
    def test_assert_produces_warning_honors_filter():
        with tm.assert_produces_warning(RuntimeWarning):
>           f()
pandas/tests/util/test_assert_produces_warning.py:17:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <contextlib._GeneratorContextManager object at 0x11dd61128>, type = None, value = None, traceback = None
    def __exit__(self, type, value, traceback):
        if type is None:
            try:
>               next(self.gen)
E               AssertionError: Caused unexpected warning(s): [('FutureWarning', FutureWarning('f1'), '/Users/taugspurger/sandbox/pandas/pandas/tests/util/test_assert_produces_warni
ng.py', 10)].
/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/contextlib.py:119: AssertionError
============================================================================= 1 failed in 0.20 seconds ==============================================================================```
Internally, `assert_produces_warning` sets a new `filter`, which
overrides any filters set by pytest. We allow for `filter_level=None` to
not set a filter.
    
          Codecov Report
 @@            Coverage Diff             @@
##           master   #25721      +/-   ##
==========================================
+ Coverage   91.24%   91.24%   +<.01%     
==========================================
  Files         172      172              
  Lines       52967    52968       +1     
==========================================
+ Hits        48331    48332       +1     
  Misses       4636     4636
 
 Continue to review full report at Codecov. 
  | 
    
          Codecov Report
 @@            Coverage Diff             @@
##           master   #25721      +/-   ##
==========================================
+ Coverage   91.24%   91.25%   +<.01%     
==========================================
  Files         172      172              
  Lines       52967    52973       +6     
==========================================
+ Hits        48331    48339       +8     
+ Misses       4636     4634       -2
 
 Continue to review full report at Codecov. 
  | 
    
| 
               | 
          ||
| @pytest.mark.filterwarnings('ignore:f1:FutureWarning') | ||
| def test_assert_produces_warning_message(): | ||
| with tm.assert_produces_warning(FutureWarning, message='f2'): | 
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.
can you add a test for tm.assert_produces_warning(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.
Asserting that no warning emitted?
        
          
                pandas/util/testing.py
              
                Outdated
          
        
      | If True, displays the line that called the function containing | ||
| the warning to show were the function is called. Otherwise, the | ||
| line that implements the function is displayed. | ||
| message : str, default '' | 
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.
how would one use message?
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.
It ends up being hard to use effectively, because unhandled warnings are cause us to raise immediately. Just removing for now, and we can re-add later if needed.
| @pytest.mark.filterwarnings('ignore:f1:FutureWarning') | ||
| @pytest.mark.filterwarnings('ignore:f2:RuntimeWarning') | ||
| def test_assert_produces_warning_honors_filter(): | ||
| with tm.assert_produces_warning(RuntimeWarning): | 
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.
Shouldn't you be testing the new option here?
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.
Yes... I've messed something up, sorry.
| 
           Hmm this may require a slightly different approach. As a test-written, I only care about the RuntimeWarning. But calling  def f():
    warnings.warn('f1', FutureWarning)
    warnings.warn('f2', RuntimeWarning)So I think we need an option for whether "extra" warnings cause the test to fail (I really don't want to wrap every call to   | 
    
          
 I think this is related to an issue that came up in  Not a big deal, but any reason why this merits a new test file?  | 
    
| 
           For the sparse array deprecation, I'd like to decorate the test class with
`@pytest.mark.filterwarnings(...)`
ignore warnings about the deprecation. But we'd like to assert that
expected warnings for other things still happen. 
…On Fri, Mar 15, 2019 at 10:25 AM jbrockmendel ***@***.***> wrote:
 But calling f may emit other warnings
 I think this is related to an issue that came up in
 DatetimeArray._addsub_int in cases where multiple warnings were expected.
 The conclusion at the time was that tm.assert_produces_warning doesn't
 handle multiple expected warnings, so the workaround was to just suppress
 one and only test for the other.
 Not a big deal, but any reason why this merits a new test file?
 —
 You are receiving this because you authored the thread.
 Reply to this email directly, view it on GitHub
 <#25721 (comment)>,
 or mute the thread
 <https://github.com/notifications/unsubscribe-auth/ABQHIhbVstqEzakbRSbPldBi3OAc1NM3ks5vW7tMgaJpZM4by-5g>
 .
 
 | 
    
          
 Right - it would be worthwhile though at some point to investigate adding such support.  | 
    
| 
           thanks @TomAugspurger  | 
    
* origin/master: DOC: clean bug fix section in whatsnew (pandas-dev#25792) DOC: Fixed PeriodArray api ref (pandas-dev#25526) Move locale code out of tm, into _config (pandas-dev#25757) Unpin pycodestyle (pandas-dev#25789) Add test for rdivmod on EA array (GH23287) (pandas-dev#24047) ENH: Support datetime.timezone objects (pandas-dev#25065) Cython language level 3 (pandas-dev#24538) API: concat on sparse values (pandas-dev#25719) TST: assert_produces_warning works with filterwarnings (pandas-dev#25721) make core.config self-contained (pandas-dev#25613) CLN: replace %s syntax with .format in pandas.io.parsers (pandas-dev#24721) TST: Check pytables<3.5.1 when skipping (pandas-dev#25773) DOC: Fix typo in docstring of DataFrame.memory_usage (pandas-dev#25770) Replace dicts with OrderedDicts in groupby aggregation functions (pandas-dev#25693) TST: Fixturize tests/frame/test_missing.py (pandas-dev#25640) DOC: Improve the docsting of Series.iteritems (pandas-dev#24879) DOC: Fix function name. (pandas-dev#25751) Implementing iso_week_year support for to_datetime (pandas-dev#25541) DOC: clarify corr behaviour when using a callable (pandas-dev#25732) remove unnecessary check_output (pandas-dev#25755) # Conflicts: # doc/source/whatsnew/v0.25.0.rst
Previously, assert_produces_warning did not play well with pytest's
filterwarnings.
Internally,
assert_produces_warningsets a newfilter, whichoverrides any filters set by pytest. We allow for
filter_level=Nonetonot set a filter.
This will be useful for the SparseDataFrame deprecation, where I'd like to just set a
pytest.mark.filterwarningon the class, and not worry about catching individual warnings.