镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Fix spying on cached instance methods - #658

Open
HocVoNgThai wants to merge 1 commit into
pytest-dev:mainfrom
HocVoNgThai:fix/spy-cached-method-binding
Open

HocVoNgThai wants to merge 1 commit into
pytest-dev:mainfrom
HocVoNgThai:fix/spy-cached-method-binding

Conversation

@HocVoNgThai

Copy link
Copy Markdown

Spying on an instance method decorated with functools.lru_cache or functools.cache through its class replaces its binding descriptor with a mock that does not supply self. Calls through an instance then raise TypeError.

For example, if Worker.calculate(self, value) is cached and returns value * 2, worker.calculate(3) works until mocker.spy(Worker, "calculate") is installed. This change keeps the result 6, records (worker, 3), and preserves cache hits.

Use the spy's function wrapper as the autospec for callable class descriptors so instance binding is preserved. Add regressions for both cache decorators, multiple instances, cache hits, reset/stop, and cached static/class methods.

Closes #657

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Spying on a cached method through its class drops self

1 participant