Skip to content

Fix monkeypatch resolution of cached submodules - #15157

Open
SharifWaqas wants to merge 2 commits into
pytest-dev:mainfrom
SharifWaqas:fix-monkeypatch-cached-submodules
Open

SharifWaqas wants to merge 2 commits into
pytest-dev:mainfrom
SharifWaqas:fix-monkeypatch-cached-submodules

Conversation

@SharifWaqas

Copy link
Copy Markdown

Fix string-based monkeypatch.setattr and delattr for cached submodules whose parent-package attribute is missing. Use the module returned by importlib. Regression tests cover both operations and undo.

Validation: 4,688 tests passed with no failures. Both regressions fail before the fix. Pre-commit checks passed.

Developed with OpenAI Codex assistance. The temporary changelog/0.bugfix.rst will be renamed to this PR number.

Use the module returned by importlib when a parent package no longer
exposes a cached submodule. Cover setattr, delattr, and undo behavior.

Developed with OpenAI Codex assistance.
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Oct 10, 2026
@0xamlab

0xamlab commented Oct 10, 2026

Copy link
Copy Markdown

Verified the fix locally. The new regression tests in testing/test_monkeypatch.py pass on this branch (46 passed, 2 skipped in testing/test_monkeypatch.py), and I reproduced the failure on main: mp.setattr("cached_target_package.child.value", 2) raises AttributeError: 'module' object at cached_target_package.child has no attribute 'child' when the submodule is cached but no longer exposed on its parent. The mechanism is the capture of the module returned by importlib.import_module at src/_pytest/monkeypatch.py:82, instead of falling through to annotated_getattr on the parent package. Clean change, good test coverage for setattr, delattr, and undo.

This branch has not been deployed

No deployments
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.

2 participants