Skip to content

Python 3: warnings displayed multiple times despite default 'once' filter #11207

@embray

Description

@embray

(I suspect this has come up before but I couldn't find an existing issue for it.)

The expected default behavior in Python for handling warnings is such that once a specific warning has been displayed once, it should not be displayed again (unless the warning filters are updated). E.g.:

Python 3.6.1 (default, Feb 21 2018, 12:59:49)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import warnings
>>> def foo():
...     warnings.warn('foo')
...
>>> foo()
__main__:2: UserWarning: foo
>>> foo()
>>>

However, in IPython CLI this behavior is not observed (probably same for the notebook but I haven't tested):

Python 3.6.1 (default, Feb 21 2018, 12:59:49)
Type "copyright", "credits" or "license" for more information.

IPython 5.5.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import warnings

In [2]: def foo():
   ...:     warnings.warn('foo')
   ...:

In [3]: foo()
.../local/bin/ipython3:2: UserWarning: foo
  # EASY-INSTALL-ENTRY-SCRIPT: 'ipython==5.5.0','console_scripts','ipython3'

In [4]: foo()
.../local/bin/ipython3:2: UserWarning: foo
  # EASY-INSTALL-ENTRY-SCRIPT: 'ipython==5.5.0','console_scripts','ipython3'

I have documented the cause of this behavior here, and I believe it is more of a CPython bug than an IPython bug. But it's possible there's a workaround that could be applied, or at the very least I thought the issue should be noted.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions