python / python/cpython

`contextlib.contextmanager` raises `dataclasses.FrozenInstanceError` when handling a frozen dataclass exception

Open
#99,856 14 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib topic-dataclasses type-bug
Dominant language
Python
Stars
77.2k
Forks
36k
PR merge metrics
PR metrics pending

Description

Bug report

In Python 3.11, raising a frozen dataclass as an exception inside a contextlib.contextmanager context will raise a dataclasses.FrozenInstanceError exception. This issue does not occur in Python 3.10.

This is a result of the changes made in https://github.com/python/cpython/pull/92202

Reproducer

import contextlib
import dataclasses


@dataclasses.dataclass(frozen=True)
class Notice(Exception):
    pass


@contextlib.contextmanager
def demo():
    yield


try:
    with demo():
        raise Notice()
except Notice:
    pass

Expected Result

$ python3.10 ./repro.py

Actual Result

$ python3.11 ./repro.py 
Traceback (most recent call last):
  File "/usr/lib/python3.11/contextlib.py", line 155, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/home/mclay/code/mattclay/contextlib-repro/./repro.py", line 12, in demo
    yield
  File "/home/mclay/code/mattclay/contextlib-repro/./repro.py", line 17, in <module>
    raise Notice()
Notice

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/mclay/code/mattclay/contextlib-repro/./repro.py", line 16, in <module>
    with demo():
  File "/usr/lib/python3.11/contextlib.py", line 188, in __exit__
    exc.__traceback__ = traceback
    ^^^^^^^^^^^^^^^^^
  File "<string>", line 4, in __setattr__
dataclasses.FrozenInstanceError: cannot assign to field '__traceback__'

Your environment

$ python3.10 -V
Python 3.10.6
$ python3.11 -V
Python 3.11.0
$ source /etc/os-release; echo $PRETTY_NAME
Ubuntu 22.04.1 LTS
$ uname -a
Linux redstone 5.11.0-27-generic #29~20.04.1-Ubuntu SMP Wed Aug 11 15:58:17 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with contextlib.py, especially the contextmanager exit path shown in the traceback, and reproduce the failure with the supplied frozen dataclass exception example on Python 3.11. Done means the exception can be handled through the context manager without raising FrozenInstanceError while preserving the expected behavior shown by Python 3.10.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.