monkeypatch.setattr fails to undo on objects with a custom __setattr__ (regression from #14969)
维护者通常 1 天内回复
@hardikkaurani 已经在做这个了。
开始于 2026年9月24日。
评估
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 新手友好度
- 78/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- python
- 领域
- testing-qa
调研方向
从 MonkeyPatch.setattr 中的新分支开始,并将其与 issue 中描述的先前行为进行比较。向 test_proxy.py 添加一个使用自定义 setattr/getattr 对象的回归用例,然后运行该测试,以验证 undo 会恢复原始值,并且修补后的值不会泄漏到后续测试中。
由索引模型根据 Issue 内容生成。
描述
Since #14969 (0c601d510, not released yet), monkeypatch.setattr doesn't restore attributes on objects that store them somewhere other than __dict__ through a custom __setattr__/__getattr__. Undo raises AttributeError, and the patched value leaks into later tests.
class Config:
"""Stores attributes in a private dict instead of __dict__."""
def __init__(self):
object.__setattr__(self, "_data", {"debug": False})
def __getattr__(self, name):
try:
return self._data[name]
except KeyError:
raise AttributeError(name) from None
def __setattr__(self, name, value):
self._data[name] = value
cfg = Config()
def test_patch(monkeypatch):
monkeypatch.setattr(cfg, "debug", True)
assert cfg.debug is True
def test_restored():
assert cfg.debug is False
On main:
ERROR test_proxy.py::test_patch - AttributeError: 'Config' object has no attribute 'debug'
FAILED test_proxy.py::test_restored - assert True is False
1 failed, 1 passed, 1 error
Just before 0c601d510, the same file passes (2 passed).
The new branch in MonkeyPatch.setattr takes the old value from target.__dict__.get(name, NOTSET) whenever target has a __dict__. It assumes that a plain setattr() writes into that dict. With a custom __setattr__ it doesn't, so the old value is recorded as NOTSET. undo() then calls delattr(cfg, "debug") instead of setting it back to False. That delattr fails, and the value stays patched.
@Shriprasad-P pointed out this exact case in a review on #14969, but it wasn't addressed before the merge. I'm opening this issue so it gets fixed before the next release.
One possible direction: only use the __dict__ lookup when name is actually present in the instance __dict__, or when the type uses object.__setattr__. Otherwise fall back to the value from getattr(), as before.
pytest 9.2.0.dev345+g872117358, Python 3.13.5, Windows 11.
- 主要语言
- Python
- 星标
- 14.6k
- 派生
- 3.5k
- 平均合并
- 3 天 15 小时
- 30 天内合并 PR
- 28
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
pytest-dev/pytest 的其他 Issue
-
`faulthandler_timeout` stops covering a test once setup, call or a subtest fails (`pytest_exception_interact` cancels the timer)可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
pytest-dev/pytest#15180 ·
维护者通常 1 天内回复
-
Assertion diff doesn't pretty-print the built-in frozen dict (Python 3.15)可能已有人在做 @HakeemTheEmperor 于 2 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
pytest-dev/pytest#15172 ·
维护者通常 1 天内回复
-
Allow dotted test filenames (`*.test.py`) via one-line change to `compute_module_name`可能重新可做 @siddy-stack 于 59 天前认领,目前没有进行中的 PR。 未关闭
难度 2/5 1-3 小时 新手友好度 86/100
pytest-dev/pytest#14514 · 6 条评论 ·
维护者通常 1 天内回复
-
Expose CaptureManager as public API可能重新可做 @harsh21234i 于 196 天前认领,目前没有进行中的 PR。 未关闭type: enhancement type: feature-branch
难度 2/5 1-3 小时 新手友好度 68/100
pytest-dev/pytest#14186 · 2 条评论 ·
维护者通常 1 天内回复
-
Docs on warnings filters should note that `ResourceWarning` is often delayed可能已有人在做 @tanishmisra9 于 115 天前认领。 未关闭type: docs
难度 2/5 1-3 小时 新手友好度 68/100
pytest-dev/pytest#9825 · 7 条评论 ·
维护者通常 1 天内回复
查看 pytest-dev/pytest 的全部 Issue
相似的 Issue
-
documentation good first issue
难度 2/5 半天 新手友好度 72/100
sujithputta02/Esapay#13 ·
-
bug
难度 2/5 1-3 小时 新手友好度 70/100
维护者通常 1 天内回复
-
needs-triage
难度 2/5 1-3 小时 新手友好度 72/100
gke-labs/kube-agents#3277 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 68/100
jbacalso24/mimry#291 ·
维护者通常 1 天内回复
-
good first issue
难度 2/5 1 小时以内 新手友好度 82/100
eli-alvarado/us-watchlist-alerts#14 ·
维护者通常 1 天内回复