Skip to content

FutureWarning from a nested set operand is not attributed to the caller #156187

Description

@fedonman

Bug description:

A FutureWarning raised while parsing a nested set inside a set-operation operand is attributed to a frame inside the re package rather than to the caller, and lands further away the deeper the nesting. _parse_operand() in Lib/re/_parser.py adds two frames but advances nested by one, so the stacklevel is one short.

import re, warnings

for pattern in [r'[[b]]', r'[a--[[b]]]']:
    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter('always')
        re.compile(pattern)
    print(pattern, '->', w[0].filename == __file__)
[[b]] -> True
[a--[[b]]] -> False

Expected: True for both. test_set_operations in Lib/test/test_re.py already asserts w.filename == __file__ for the top-level spelling. Because warning filters and the default duplicate suppression key on the reported location, -W 'error::FutureWarning:__main__' raises on the first pattern and misses the second.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

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/ directorytopic-regextype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions