Skip to content

Fix arithmetic with standard timedelta operands - #1014

Closed
Eric3-jp wants to merge 1 commit into
python-pendulum:masterfrom
Eric3-jp:fix-duration-standard-timedelta-arithmetic
Closed

Eric3-jp wants to merge 1 commit into
python-pendulum:masterfrom
Eric3-jp:fix-duration-standard-timedelta-arithmetic

Conversation

@Eric3-jp

Copy link
Copy Markdown

Fixes #382.

Duration accepts datetime.timedelta operands in division and remainder operations, but calls the operand's private _to_microseconds() method. Standard-library timedeltas do not have that method, so /, //, %, and divmod() raise AttributeError. The same problem affects Interval through its duration arithmetic.

Convert standard-library timedeltas from their integer day, second, and microsecond fields. Keep the existing conversion for Pendulum durations and intervals, preserving their current semantics. Integer conversion also avoids losing subsecond precision for large divisors.

The regressions compare all four operations with the standard library using positive, negative, and fractional-second divisors, verify zero-division behavior, exercise interval arithmetic, and check a large divisor whose final microsecond affects the quotient.

Validation:

  • Before the change, the 17 initial regression cases failed with AttributeError.
  • Pure-Python test suite on Windows / Python 3.14: 1,863 passed, 5 skipped.
  • mypy --python-version 3.13, using the repository's locked mypy 1.13.0 and dateutil stubs: passed (228 source files).
  • prek run --files src/pendulum/duration.py tests/duration/test_arithmetic.py tests/interval/test_arithmetic.py: all applicable checks passed.
  • The Rust extension variant was not run locally; this change is confined to Python duration arithmetic and its tests.

Pull Request Check List

  • Added tests for changed code.
  • Updated documentation for changed code. No documentation change: this fixes the existing timedelta operand support.

This contribution was prepared with OpenAI Codex assistance.

@Eric3-jp Eric3-jp closed this by deleting the head repository Sep 23, 2026
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.

Error dividing Period by timedelta

1 participant