Skip to content

Should warnings.warn default to stacklevel=2? #88462

Description

@asottile
mannequin
BPO 44296
Nosy @asottile

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 2021-06-02.23:55:20.255>
labels = ['type-bug', 'library', '3.11']
title = 'Should warnings.warn default to stacklevel=2?'
updated_at = <Date 2021-06-02.23:55:20.255>
user = 'https://github.com/asottile'

bugs.python.org fields:

activity = <Date 2021-06-02.23:55:20.255>
actor = 'Anthony Sottile'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2021-06-02.23:55:20.255>
creator = 'Anthony Sottile'
dependencies = []
files = []
hgrepos = []
issue_num = 44296
keywords = []
message_count = 1.0
messages = ['394960']
nosy_count = 1.0
nosy_names = ['Anthony Sottile']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue44296'
versions = ['Python 3.11']

Activity

  1. asottile commented on Jun 2, 2021

    asottilemannequin
    MannequinAuthor

    I have yet to come across a usecase where stacklevel=1 makes sense -- usually it is more helpful to point at the calling code than the function which is itself warning

    my proposal is to update the default for stacklevel= from 1 to 2

    an example bpo where this is relevant is bpo-44295

  2. added
    3.11only security fixes
    stdlibStandard Library Python modules in the Lib/ directory
    type-bugAn unexpected behavior, bug, or error
    on Jun 2, 2021
  3. transferred this issue fromon Apr 10, 2022
  4. gpshead commented on Jan 1, 2023

    @gpshead
    Member

    stacklevel is also often not a great API as it's an integer that depends on a pile of calling code far from the warnings.warn call itself. #39615 is likely to provide a more useful API. check the module/file of the stack frame and bubble up outside of the current file/module/package or a set of such.

  5. added
    type-featureA feature request or enhancement
    and removed
    3.11only security fixes
    on Jan 1, 2023
  6. removed
    type-bugAn unexpected behavior, bug, or error
    on Mar 13, 2025
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-featureA feature request or enhancement

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions