Skip to content

Reject ISO durations with empty time sections - #1012

Open
ZelinZhu-Richard wants to merge 1 commit into
python-pendulum:masterfrom
ZelinZhu-Richard:fix/empty-duration-time-sections
Open

ZelinZhu-Richard wants to merge 1 commit into
python-pendulum:masterfrom
ZelinZhu-Richard:fix/empty-duration-time-sections

Conversation

@ZelinZhu-Richard

@ZelinZhu-Richard ZelinZhu-Richard commented Sep 19, 2026

Copy link
Copy Markdown

pendulum.parse("PT", strict=True) returns a zero duration, and pendulum.parse("P1DT", strict=True) returns one day, although neither input has a time component after T.

Both parsers accept the separator without requiring a following hours, minutes, or seconds component. Check component presence instead of numeric value so explicit zeros such as PT0S and P1DT0S remain valid. Invalid inputs use the existing low-level ValueError and public ParserError paths.

Addresses the empty-time-section part of #833. Repeated components, long-number parsing, overflow, and duration arithmetic remain outside this change.

Tested separately with the native extension and Python parser:

  • test_parse_iso8601_duration_invalid and test_parse_duration_without_time_components: reject empty time sections, including P1.5DT, where hours derived from a date fraction must not count as a time component.
  • test_parse_iso8601_duration and test_parse_duration_time_components: preserve explicit zero components and valid date/time combinations.
  • Each parser passed 115 focused tests and the full suite: 1,866 passed, 3 skipped. The new rejection cases failed before the fix in both parsers.
  • Typing and all configured lint/format checks passed.

Pull Request Check List

  • Added tests for changed code.
  • Updated documentation for changed code.

No documentation change is needed for this parser validation fix.

AI assistance: this change was developed with an AI coding tool; I reviewed and tested it.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants