Skip to content

Convenient simplefilter() in warnings.catch_warnings() #91230

Closed

Description

@Zac-HD
mannequin
BPO 47074
Nosy @Zac-HD

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2022-03-21.01:46:48.500>
labels = ['type-feature', 'library', '3.11']
title = 'Convenient `simplefilter()` in `warnings.catch_warnings()`'
updated_at = <Date 2022-03-21.01:46:48.500>
user = 'https://github.com/Zac-HD'

bugs.python.org fields:

activity = <Date 2022-03-21.01:46:48.500>
actor = 'Zac Hatfield-Dodds'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2022-03-21.01:46:48.500>
creator = 'Zac Hatfield-Dodds'
dependencies = []
files = []
hgrepos = []
issue_num = 47074
keywords = []
message_count = 1.0
messages = ['415647']
nosy_count = 1.0
nosy_names = ['Zac Hatfield-Dodds']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue47074'
versions = ['Python 3.11']

Activity

  1. Zac-HD commented on Mar 21, 2022

    Zac-HDmannequin
    MannequinAuthor

    Many pytest users would like a concise way to either suppress warnings, or convert them to errors [1]. The current best approach is:

        with warnings.catch_warnings():
            warnings.simplefilter("ignore")  # or "error"

    but even this becomes tediously verbose when repeated in hundreds of tests (which I suspect is responsible for many misuses of pytest.raises()). Since these two functions take disjoint arguments [2], I propose allowing catch_warnings() to accept arguments for simplefilter() and forward them on:

        with warnings.catch_warnings("ignore"):  # or "error"

    I have a quick implementation at [3] which I can finish if the idea is acceptable. Although I don't have a concrete use-case for more than the 'action' and 'category' arguments, forwarding all rather than some makes the semantics very simple.

    I'd emphasize again that this is very much a convenience-focussed change: I estimate that 95% of all uses will pass only action="error" or action="ignore". Supporting a concise and simple interface for this seems worth the small complication to the design of the warnings module.

    [1] see e.g. pytest-dev/pytest#9404 for a long thread
    [2] unlike filterwarnings(), which has a module= argument which would collide with catch_warnings()
    [3] main...Zac-HD:zac-hd/one-line-catch-warnings

  2. added
    3.11only security fixes
    stdlibStandard Library Python modules in the Lib/ directory
    type-featureA feature request or enhancement
    on Mar 21, 2022
  3. transferred this issue fromon Apr 10, 2022
  4. added a commit that references this issue on Apr 24, 2022
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

    3.11only security fixesstdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions