Skip to content

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

Open
@zanieb

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions