Repository navigation
fix(typing): keep classmethod binding for @retry decorated methods - #759
Open
devtechedge wants to merge 1 commit into
Open
devtechedge wants to merge 1 commit into
devtechedge wants to merge 1 commit into
Conversation
Contributor
|
Tick the box to add this pull request to the merge queue (same as
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #756
Thanks for keeping the typing on
retryso tight, it made this one easy to pin down.Since the
_RetryDecoratedprotocol change, stacking@classmethodon top of@retrybreaks under pyright and mypy, because neither checker applies classmethod binding to a protocol object, soclsis never dropped from the call signature.This adds an extra
retryoverload for callables that take a first positional parameter, returning a new_RetryDecoratedMethod[S, P, R]protocol.Its
__get__has a self-typed overload that drops the first parameter when it istype[T], soFoo.method(1)type-checks under@classmethod.Instance methods still bind
selfthe usual way.The
.retry,.statisticsand.retry_withattributes stay typed on the new protocol.There is no runtime change, only annotations.
One trade-off: a plain function whose first parameter is annotated
type[X]and is stored as a class attribute will type-check as if it were bound.That felt like a rare pattern compared to
@classmethod+@retry, but happy to tighten it if you see a better shape.I added
assert_typeregression tests for sync and async classmethods and instance methods intests/test_tenacity.pyandtests/test_asyncio.py.They fail on main under both mypy and pyright and pass with this change.
The full test suite,
poe mypy(strict config),poe lintandpoe docsall pass locally.There is a reno note under
releasenotes/notes/.This is really a typing workaround, so it can be dropped if pyright and mypy start applying classmethod binding to protocol objects upstream.
Open to any changes you would prefer here.