Skip to content

Fix monkeypatch.setattr() failing to undo patches to __bases__ - #15030

Open
SemTiOne wants to merge 1 commit into
pytest-dev:mainfrom
SemTiOne:fix/1938-monkeypatch-class-bases
Open

SemTiOne wants to merge 1 commit into
pytest-dev:mainfrom
SemTiOne:fix/1938-monkeypatch-class-bases

Conversation

@SemTiOne

@SemTiOne SemTiOne commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

For class attributes not present in __dict__, preserve the pre-patch value so undo() can restore it with setattr(). Newly shadowed inherited attributes still use delattr(), preserving #156 behavior.

Added a regression test based on 5eaac19. Pre-fix:

>               delattr(obj, name)
E               TypeError: cannot delete '__bases__' attribute of immutable type 'ThingToTest'

src\_pytest\monkeypatch.py:437: TypeError
=========================== short test summary info ===========================
FAILED testing/test_monkeypatch.py::test_issue1938_patch_class_bases - TypeEr...
1 failed in 4.56s

Closes #1938.

  • Include documentation when adding new features.
  • Include new tests or update existing tests when applicable.
  • Allow maintainers to push and squash when merging my commits. Please uncheck this if you prefer to squash the commits yourself.
  • Add text like closes #XYZW to the PR description and/or commits (where XYZW is the issue number). See the github docs for more information.
  • If AI agents were used, they are credited in Co-authored-by commit trailers.
  • Create a new changelog file in the changelog directory, with a name like <ISSUE NUMBER>.<TYPE>.rst. See changelog/README.rst for details.
  • Add yourself to AUTHORS in alphabetical order.

@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Sep 15, 2026
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
@SemTiOne
SemTiOne force-pushed the fix/1938-monkeypatch-class-bases branch from 283e873 to cb63d4f Compare September 15, 2026 05:50
@SemTiOne
SemTiOne marked this pull request as ready for review September 15, 2026 05:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

monkeypatch class bases fails with TypeError

1 participant