Skip to content

Traceback for exit of multi-line with statement points to with (, rather than the context manager #137932

@Zac-HD

Description

@Zac-HD

Bug description:

class CM:
    def __init__(self, fail=False):
        self.fail = fail

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        assert not self.fail


with (
    CM(fail=True) as a,
    CM() as b,
):
    pass

and the traceback should tell us where this failed, right?

Traceback (most recent call last):
  File "repro.py", line 12, in <module>
    with (
  File "repro.py", line 9, in __exit__
    assert not self.fail
AssertionError

In more realistic use-cases1, it would be very helpful if the traceback pointed to line 13 CM(fail=True) as a, rather than line 12 with (.

I've tested this on Python 3.11, and searching the changelogs for 3.12/13/14 didn't show any related changes.

Footnotes

  1. in the case that motivated this issue, I have eleven context managers in one async with statement, which I admit may be excessive - it's certainly motivating.

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.11only security fixestype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions