Skip to content

warnings.simplefilter("once") and warnings.warn() print all occurrences of matching warnings, regardless of location #125761

Description

@hyperkai

Bug report

Bug description:

With warnings.simplefilter("module") and warnings.warn(), I ran main.py which runs file1.py(module) and file2.py(module) as shown below:

*Memos:

  • The doc says "module" print the first occurrence of matching warnings for each module where the warning is issued (regardless of line number).
  • I also used warnings.filterwarnings("module").
  • I ran it on Windows and Linux.
my_project
 |-main.py
 |-file1.py(module)
 └-file2.py(module)

main.py:

import warnings
warnings.simplefilter("module")

import file1, file2

file1.py:

import warnings

warnings.warn("Warning 1")
warnings.warn("Warning 2")
warnings.warn("Warning 3")

file2.py:

import warnings

warnings.warn("Warning 1")
warnings.warn("Warning 2")
warnings.warn("Warning 3")

Then, "module" print the first occurrence of matching warnings for each module where the warning is issued (regardless of line number)` as shown below:

...\my_project\file1.py:3: UserWarning: Warning 1
  warnings.warn("Warning 1")
...\my_project\file1.py:4: UserWarning: Warning 2
  warnings.warn("Warning 2")
...\my_project\file1.py:6: UserWarning: Warning 3
  warnings.warn("Warning 3")
...\my_project\file2.py:3: UserWarning: Warning 1
  warnings.warn("Warning 1")
...\my_project\file2.py:4: UserWarning: Warning 2
  warnings.warn("Warning 2")
...\my_project\file2.py:6: UserWarning: Warning 3
  warnings.warn("Warning 3")

Now with warnings.simplefilter("once") and warnings.warn(), I ran main.py which runs file1.py(module) and file2.py(module) as shown below:

*Memos:

  • The doc says "once" print only the first occurrence of matching warnings, regardless of location.
  • I also used warnings.filterwarnings("once").
  • I ran it on Windows and Linux.
import warnings
warnings.simplefilter("once")

import file1, file2

But "once" print all occurrences of matching warnings, regardless of location as shown below:

...\my_project\file1.py:3: UserWarning: Warning 1
  warnings.warn("Warning 1")
...\my_project\file1.py:4: UserWarning: Warning 2
  warnings.warn("Warning 2")
...\my_project\file1.py:6: UserWarning: Warning 3
  warnings.warn("Warning 3")
...\my_project\file2.py:3: UserWarning: Warning 1
  warnings.warn("Warning 1")
...\my_project\file2.py:4: UserWarning: Warning 2
  warnings.warn("Warning 2")
...\my_project\file2.py:6: UserWarning: Warning 3
  warnings.warn("Warning 3")

CPython versions tested on:

3.11

Operating systems tested on:

Linux, Windows

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc direasy
    No fields configured for issues without a type.

    Projects

    Status
    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions