warnings filter precedence

Viewed 56
import warnings

def warn():
    with warnings.catch_warnings():
        warnings.simplefilter('always')
        warnings.warn('warning')
        
with warnings.catch_warnings():
    warnings.simplefilter('ignore')
    warn()
warn.py:6: UserWarning: warning
  warnings.warn('warning')

I was thinking that the 'ignore' filter would apply but it seems the 'always' filter within the function is taking priority. Is this the expected behavior? Is there any way to ignore a warning in this case? I am importing something with simplefilter('always'), so I do not have control, but I would like to ignore it.

0 Answers
Related