pytest icon indicating copy to clipboard operation
pytest copied to clipboard

monkeypatch.setattr on a class's __annotations__ deletes them on undo (Python 3.14)

Open HipHoff opened this issue 1 month ago • 2 comments

  • [x] a detailed description of the bug or problem you are having
  • [x] output of pip list from the virtual environment you are using
  • [x] pytest and operating system versions
  • [x] minimal example if possible

On Python 3.14, undoing monkeypatch.setattr(SomeClass, "__annotations__", ...) deletes the class's annotations instead of restoring them. Every later test then sees SomeClass.__annotations__ == {}. On 3.13 the same tests pass.

# test_annotations.py
class Config:
    name: str
    size: int


def test_patch_annotations(monkeypatch):
    monkeypatch.setattr(Config, "__annotations__", {**Config.__annotations__, "extra": bool})
    assert set(Config.__annotations__) == {"name", "size", "extra"}


def test_annotations_restored():
    # Runs after the monkeypatch above has been undone.
    assert Config.__annotations__ == {"name": str, "size": int}
$ python3.14 -m pytest -q test_annotations.py
.F
    def test_annotations_restored():
>       assert Config.__annotations__ == {"name": str, "size": int}
E       AssertionError: assert {} == {'name': <cla...<class 'int'>}
1 failed, 1 passed

Cause. For a class target, MonkeyPatch.setattr records target.__dict__.get(name, NOTSET) as the value to restore. Under PEP 649/749, 3.14 no longer keeps __annotations__ in the class __dict__: it holds __annotate_func__ and __annotations_cache__, and type.__annotations__ is a data descriptor on the metaclass. So the recorded old value is NOTSET, and undo() calls delattr(Config, "__annotations__"), which on 3.14 clears the annotations for good.

>>> class C:
...     a: int
>>> C.__annotations__
{'a': <class 'int'>}
>>> '__annotations__' in C.__dict__, sorted(k for k in C.__dict__ if 'annot' in k)
(False, ['__annotate_func__', '__annotations_cache__'])

The same shape affects any attribute served by a data descriptor on the metaclass rather than the class __dict__, which #1938 already tracks for __bases__. I'm filing this separately because on 3.14 it is now reachable through __annotations__, a common thing to patch in tests (TypedDict and dataclass field lists), and the failure is silent: the patched test passes and an unrelated later test fails.

Status on main. pytest 9.2.0 and main (a7b2446ee) both fail. #15136 fixes it: with that branch installed, the repro above passes, because a data descriptor on type(target) now makes undo() write the looked-up value back. So I am not opening a competing PR. If it is useful, I can contribute the 3.14 __annotations__ case above as a regression test to #15136 (skipped below 3.14), and a changelog line, whichever the maintainers prefer.

The workaround we use meanwhile is monkeypatch.setitem(Config.__annotations__, "extra", bool).

Environment. macOS 26.6 (arm64), CPython 3.14.4. pip list:

iniconfig==2.3.1
packaging==26.3
pluggy==1.7.0
Pygments==2.21.0
pytest==9.2.0

Disclosure: this issue was drafted by Claude (Anthropic's AI assistant, via Claude Code) on behalf of @HipHoff, who approved filing it and takes responsibility for it. The repro was run against pytest 9.2.0, pytest main and the #15136 branch on CPython 3.14.4, and against pytest 9.2.0 on 3.13 (passes).

HipHoff avatar Oct 28 '26 19:10 HipHoff

I'd like to work on this. I've reviewed a fix prepared with AI assistance (Instinct) that restores the class's lazy annotation state and preserves previously edited annotation values on undo. It still depends on a CPython-specific cache entry, which I'd like maintainer feedback on. Automated Linux tests cover Python 3.14, 3.13 and 3.12; Windows hasn't been tested.

aryank25 avatar Oct 30 '26 13:10 aryank25

I'd like to work on this.

rinopatrick avatar Nov 02 '26 02:11 rinopatrick