`filterwarnings` causes `warnings.warn` to raise `TypeError` on non-string messages · Issue #103577 · python/cpython · GitHub
Skip to content

filterwarnings causes warnings.warn to raise TypeError on non-string messages #103577

Description

@zanieb

Bug report

When using filterwarnings, a TypeError is raised when a non-string value is passed to warnings.warn which differs from the default behavior of warnings.warn.

import warnings

# Does not raise a type error
warnings.warn(1)

# Raises a type error
with warnings.catch_warnings():
    warnings.filterwarnings("ignore", "test")
    warnings.warn(1)

The traceback does not demonstrate that this is a detail of warning filtering:

/Users/mz/eng/src/madkinsz/cpython-103577/example.py:4: UserWarning: 1
  warnings.warn(1)
Traceback (most recent call last):
  File "/Users/mz/eng/src/madkinsz/cpython-103577/example.py", line 9, in <module>
    warnings.warn(1)
TypeError: expected string or bytes-like object

Filtering warnings should not change the accepted types of warnings.warn.

Reproduction example at https://github.com/madkinsz/cpython-103577
Originally raised at pytest-dev/pytest#10865

Your environment

Reproduced Ubuntu with Python 3.7-3.12: https://github.com/madkinsz/cpython-103577/actions/runs/4714383423/jobs/8360711980

Also reproduced manually on macOS with Python 3.10/3.11

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions