fix: reject a number on the left-hand side of + as well - #724
Open
yu2971512385-ui wants to merge 1 commit into
Open
yu2971512385-ui wants to merge 1 commit into
yu2971512385-ui wants to merge 1 commit into
Conversation
wait_base.__radd__ turns a non-zero number into a TypeError at the '+',
so that '5 + wait_fixed(1)' fails where it is written rather than
building a combination that raises when it is called. __add__ had no
such check, so the mirror expression went the way __radd__ avoids:
>>> w = tenacity.wait_fixed(1) + 5
>>> w(retry_state)
TypeError: 'int' object is not callable
Return NotImplemented for an operand that is not callable, which lets
Python raise the usual unsupported-operand TypeError. A plain callable
stays accepted, as WaitBaseT and wait_combine both allow one.
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.
wait_base.__radd__deliberately turns a non-zero number into aTypeErrorat the+, with the reasoning spelled out in the comment there:__add__has no such check, so the mirror expression does exactly what that comment is about:The error arrives on the first retry, inside
wait_combine.__call__, pointing at tenacity rather than at the line that built the strategy.5 + tenacity.wait_fixed(1)— the same mistake written the other way round — already fails immediately and is covered bytest_wait_radd_rejects_non_zero_number.Change
__add__returnsNotImplementedwhen the operand is not callable, so Python falls through to the operand's__radd__and raises the usualunsupported operand type(s) for +: 'wait_fixed' and 'int'. The annotation moves fromwait_basetoWaitBaseT, matching whatwait_combineand__radd__already accept, sowait_fixed(1) + (lambda retry_state: 2.0)keeps working.Behaviour after the change:
w + 5wait_combinethat raises when calledTypeErrorat the+5 + wTypeErrorat the+0 + w/sum([w1, w2])wait_combinew1 + w2,w + callablewait_combineTests
test_wait_add_rejects_number— the mirror of the existing__radd__test; it fails without the change.test_wait_add_plain_callable— the callable case keeps building await_combine.pytest tests/is 185 passed, 1 skipped;mypy tenacity/wait.pyis clean (the 3 errorsmypy tenacity/reports inasyncio/__init__.pyare there on a clean checkout too).Disclosure: found and prepared with AI assistance — the asymmetry came out of a sweep over the wait strategy operators; I verified the behaviour and ran the suite and mypy locally.