Fix Duration multiplication by a float dropping years and months#975
Open
gaoflow wants to merge 1 commit into
Open
Fix Duration multiplication by a float dropping years and months#975gaoflow wants to merge 1 commit into
gaoflow wants to merge 1 commit into
Conversation
Duration.__mul__ with a float built the result only from _to_microseconds(), which excludes the years and months components, so Duration(years=1) * 2.0 returned an empty Duration even though Duration(years=1) * 2 (int) and Duration(years=1) / 2.0 (float) both keep them. Scale years and months by the float ratio as well, mirroring the existing __truediv__ implementation.
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.
The bug
Multiplying a
Durationby a float silently drops theyearsandmonthscomponents, even though multiplying by anint— and dividing by a float — both keep them:Duration(years=2, months=4) * 2.0returns an emptyDuration(). Since* 2and* 2.0are mathematically the same operation, this is a clear inconsistency.Root cause
Duration.__mul__(src/pendulum/duration.py) builds the float result solely from_to_microseconds(), which by design excludesyears/months, and hardcodes them to0:__truediv__and__floordiv__already handle this correctly by scalingyears/monthsalongside the microseconds.Fix
Scale
yearsandmonthsby the float ratio too, mirroring the existing__truediv__float branch:Now
duration(years=1) * 2.0 == duration(years=1) * 2, and a non-integer factor roundsyears/monthsthe same way float division already does.Tests
Added
test_multiply_float(mirrors the existingtest_dividefloat case): a float factor matches the integer result, is commutative (2.0 * it), a whole-valued float equals the int product exactly, and a non-integer factor rounds years/months. The new test fails onmaster(years 0 != 4) and passes with the fix; the fulltests/durationsuite stays green.ruffandmypyclean.