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

martineau
  • 119,623
  • 25
  • 170
  • 301
dshanahan
  • 676
  • 5
  • 12
  • How would I go about making the warnings maintainers aware of this? I do not see the option to open an Issue on the [repo](https://github.com/python/cpython/blob/master/Lib/warnings.py) – dshanahan Jan 20 '21 at 21:18

0 Answers0