Skip to main content

python-slots-mock-patch-object

Fix for `AttributeError: '<ClassName>' object attribute '<method>' is read-only` raised during `mock.patch.object()` teardown. Use when: (1) a Python test using `unittest.mock.patch.object(instance, "method")` passes the patched code but raises an AttributeError during context-exit (visible in the stack as `mock.py:1635` calling `delattr(self.target, self.attribute)`); (2) the target class declares `__slots__`; (3) the test was working before someone added `__slots__` to a base class or the target class itself; (4) a refactor introduced `__slots__` "for efficiency" and broke a previously-green test suite. Covers root cause (`__slots__` blocks `delattr` because it doesn't allocate `__dict__`, so the attribute can't be removed after the patch sets it), the diagnostic signature, the canonical fix (drop `__slots__` if the class is a mock target), and when `__slots__` IS the right call (when memory matters AND the class is never patched).

跳到安装

来源信息

仓库
cajias/claude-skills
最近来源活动
2026年7月20日 07:29
检测到的 SKILL.md 语言
英语
星标
0
分支
0

安装方式

默认使用会先检查来源的 Prompt;你也可以切换为直接命令,或下载本地副本。

检查来源文件

决定是否安装前,请先阅读 SKILL.md,以及 SkillsMP 当前展示的配套文件。