Skip to content

Forward self keyword through retry controllers - #761

Open
AryanRG wants to merge 2 commits into
jd:mainfrom
AryanRG:fix/forward-self-keyword
Open

AryanRG wants to merge 2 commits into
jd:mainfrom
AryanRG:fix/forward-self-keyword

Conversation

@AryanRG

@AryanRG AryanRG commented Oct 17, 2026

Copy link
Copy Markdown

Calling a retried method through its class with self supplied by keyword fails before the target runs:

from tenacity import retry, stop_after_attempt

class Service:
    @retry(stop=stop_after_attempt(2))
    def call(self, value):
        return value

service = Service()
print(Service.call(self=service, value=42))

Before this change, this raises TypeError: Retrying.__call__() got multiple values for argument 'self'; afterwards it prints 42. The same receiver collision affects AsyncRetrying and TornadoRetrying, including direct controller calls and wraps().

Make only the controller receiver positional-only in the abstract interface and three built-in implementations. The target's self keyword then reaches **kwargs, while direct target selection using controller(fn=target, ...) remains supported. Retry decisions and execution bodies are unchanged. Regression tests cover direct positional/keyword targets, wraps, and retry, including actual retries and callback kwargs; synchronous and asyncio cases also cover disabled retries. A Reno fixes entry is included and was verified in the generated changelog and rendered HTML.

There is a compatibility tradeoff: the rare explicit unbound call Retrying.__call__(self=controller, fn=target) must become Retrying.__call__(controller, fn=target). The analogous asyncio and Tornado calls have the same change, and class-level signature inspection now reports the receiver as positional-only. Normal bound controller calls retain their signatures. Third-party subclasses overriding __call__ need their own fix; a target keyword named fn remains outside this change. Tornado's existing direct enabled=False issue is also outside scope (#716).

Validation on macOS with CPython 3.12.15, against base 32ac68f0941319c0d2f4fb927f96e4ee29bb46c6:

  • Exact-base runtime with these regression tests: 16 failed, 5 passed, 178 deselected. Twelve parameterized failures and four Tornado subtest failures demonstrate the receiver collision; disabled wrapper controls already pass.
  • Patched focused selection (pytest -q tests/test_tenacity.py tests/test_asyncio.py tests/test_tornado.py -k self_keyword_is_forwarded): 17 passed, 178 deselected, four subtests passed.
  • poe all: 202 tests passed; Ruff lint and format checks passed; strict mypy passed for 19 source files; 35 doctests passed; both warning-as-error Sphinx builds passed.
  • reno lint, uv build, and uvx twine check --strict dist/* passed.

The local runtime checks cover Python 3.12 only; the supported interpreter/OS matrix and installed-wheel behavior were not verified locally. Local results do not establish hosted CI success or maintainer acceptance.

AI tools assisted with preparing the implementation, tests, and this description. A separate automated review reproduced the before/after failure, reran the local aggregate checks, and assessed the compatibility tradeoff.

@mergify

mergify Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

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.

1 participant