Skip to content
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

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

Closed
hyperkai opened this issue Oct 20, 2024 · 5 comments
Labels
docs Documentation in the Doc dir easy

Comments

@hyperkai
Copy link

hyperkai commented Oct 20, 2024

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

@hyperkai hyperkai added the type-bug An unexpected behavior, bug, or error label Oct 20, 2024
@picnixz
Copy link
Contributor

picnixz commented Oct 22, 2024

I can confirm this behaviour. But this seems weird that no one has ever noticed it so I'm wondering whether there is something I'm misunderstanding.

cc @ncoghlan

@picnixz picnixz added the stdlib Python modules in the Lib dir label Oct 22, 2024
@ncoghlan
Copy link
Contributor

ncoghlan commented Oct 23, 2024

There's a docs ambiguity here, since it doesn't explicitly state how the three filters that suppress repeated warnings determine if a warning is a repeat:

  • default: only full (message, category, module, lineno) matches are considered repeats
  • module: any warnings that match on (message, category, module) are considered repeats
  • once: any warnings that match on (message, category) are considered repeats

Unlike the initial check against the filter definition, these definitions are not affected by which fields are defined in the filter string. This is why a filter like default::DeprecationWarning works the way it does: the given action is applied to every reported deprecation warning, but they're not all considered to be the same deprecation warning.

error, always, ignore aren't affected by the ambiguity since they don't check for repeats in the first place

So in the example, all 6 warnings are considered distinct, since they all have different messages. If you drop the distinguishing numbers from each of them, you'll see once and module behaving as you expect.

That's a genuine flaw in the docs, but the behaviour is as expected, so I'll adjust the labels accordingly.

@ncoghlan ncoghlan added docs Documentation in the Doc dir and removed type-bug An unexpected behavior, bug, or error labels Oct 23, 2024
@picnixz picnixz added easy and removed stdlib Python modules in the Lib dir labels Oct 23, 2024
@byungchanKo99
Copy link
Contributor

I will take a look!
@corona10

@ncoghlan
Copy link
Contributor

ncoghlan commented Nov 2, 2024

Thanks for the docs update @byungchanKo99! The main PR has been merged, and the backport PRs will automatically merge once their CI runs complete.

It may be a while before the change goes lives on docs.python.org, though (almost always within 24 hours, but most often faster than that - building the full docs set with all its translations can take a while)

ncoghlan pushed a commit that referenced this issue Nov 2, 2024
…rnings module (gh-126330)

(cherry picked from commit 10eeec2)

Co-authored-by: 고병찬 <[email protected]>
ncoghlan pushed a commit that referenced this issue Nov 2, 2024
…rnings module (gh-126331)

(cherry picked from commit 10eeec2)

Co-authored-by: 고병찬 <[email protected]>
@picnixz
Copy link
Contributor

picnixz commented Nov 2, 2024

Closing since completed and backported. Thank you all!

@picnixz picnixz closed this as completed Nov 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir easy
Projects
Status: Done
Development

No branches or pull requests

4 participants