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

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

Closed
Zac-HD mannequin opened this issue Mar 21, 2022 · 1 comment · Fixed by #91435
Closed

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

Zac-HD mannequin opened this issue Mar 21, 2022 · 1 comment · Fixed by #91435
Labels
3.11 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@Zac-HD
Copy link
Mannequin

Zac-HD mannequin commented Mar 21, 2022

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']

@Zac-HD
Copy link
Mannequin Author

Zac-HD mannequin commented Mar 21, 2022

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

@Zac-HD Zac-HD mannequin added 3.11 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Mar 21, 2022
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.11 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

0 participants