Skip to content

Keep the sign of negative sub-second durations in in_words() - #1086

Open
kevin9327 wants to merge 1 commit into
python-pendulum:masterfrom
kevin9327:fix-in-words-negative-subseconds
Open

kevin9327 wants to merge 1 commit into
python-pendulum:masterfrom
kevin9327:fix-in-words-negative-subseconds

Conversation

@kevin9327

Copy link
Copy Markdown

Pull Request Check List

  • Added tests for changed code.
  • Updated documentation for changed code. (no doc change needed)

A negative duration shorter than a second loses its sign in in_words():

>>> pendulum.duration(seconds=-1).in_words()
'-1 second'
>>> pendulum.duration(microseconds=-123456).in_words()
'0.12 seconds'   # expected '-0.12 seconds'

The same happens for an interval whose end is less than a second before its start ('0.12 second'). Longer negative values already keep their sign (test_singular_negative_values expects -1 day). The sub-second branch formatted abs(self.microseconds), and microseconds is the only place the sign is kept for these values.

The fix formats self.microseconds without abs() in both Duration.in_words() and Interval.in_words(). Positive values and the zero case (0 microseconds) are unchanged.

Tests: added test_negative_subseconds to tests/duration/test_in_words.py and tests/interval/test_in_words.py. Both fail before the fix and pass after it. The full suite has 1872 passed and 1 failed here, run with PENDULUM_EXTENSIONS=0. The failure is tests/test_main.py::test_instance_with_aware_datetime_any_tzinfo, which also fails on master in this environment. ruff check and ruff format --check (v0.14.11, the pinned version) pass.

I wrote this change with help from an AI assistant. I reviewed it and ran the tests listed above.

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