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

Setting None to skip_file_prefixes of warnings.warn() gets error #125818

Open
hyperkai opened this issue Oct 21, 2024 · 2 comments
Open

Setting None to skip_file_prefixes of warnings.warn() gets error #125818

hyperkai opened this issue Oct 21, 2024 · 2 comments
Labels
docs Documentation in the Doc dir

Comments

@hyperkai
Copy link

hyperkai commented Oct 21, 2024

Bug report

Bug description:

The doc of warnings.warn() says skip_file_prefixes is None by default as shown below:

warnings.warn(message, category=None, stacklevel=1, source=None, *, skip_file_prefixes=None)

But, setting None to skip_file_prefixes gets the error as shown below:

import warnings

warnings.warn(message='Warning',
              category=None,
              stacklevel=1,
              source=None,
              skip_file_prefixes=None) # Error
            # ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑

TypeError: warn() argument 'skip_file_prefixes' must be tuple, not None

CPython versions tested on:

3.13

Operating systems tested on:

Windows

Linked PRs

@hyperkai hyperkai added the type-bug An unexpected behavior, bug, or error label Oct 21, 2024
@tomasr8
Copy link
Member

tomasr8 commented Oct 21, 2024

This is probably just a typo in the docs. The default value for that parameter as actually an empty tuple:

cpython/Lib/warnings.py

Lines 302 to 316 in 4848b0b

def warn(message, category=None, stacklevel=1, source=None,
*, skip_file_prefixes=()):
"""Issue a warning, or maybe ignore it or raise an exception."""
# Check if message is already a Warning object
if isinstance(message, Warning):
category = message.__class__
# Check category argument
if category is None:
category = UserWarning
if not (isinstance(category, type) and issubclass(category, Warning)):
raise TypeError("category must be a Warning subclass, "
"not '{:s}'".format(type(category).__name__))
if not isinstance(skip_file_prefixes, tuple):
# The C version demands a tuple for implementation performance.
raise TypeError('skip_file_prefixes must be a tuple of strs.')

(It's also the case for the C module) The docs mention it too:

[...] If supplied, it must be a tuple of strings.

Would you like to open a PR to fix the method signature in the docs?

@tomasr8 tomasr8 added docs Documentation in the Doc dir and removed type-bug An unexpected behavior, bug, or error labels Oct 21, 2024
@rruuaanng
Copy link
Contributor

Maybe the task of repairing this document can be entrusted to me.
If there is no comment, I will launch a PR.

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
Projects
None yet
Development

No branches or pull requests

3 participants