Refactor parsers to raise ParseError#38
Merged
chetmancini merged 3 commits intomasterfrom Feb 22, 2026
Merged
Conversation
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.
This PR makes parse_date, parse_datetime, and parse_iso8601 consistently raise ParseError instead of returning None on parse failures. It removes duplicated strict parser entrypoints, adds shared parsing helpers, and improves parse error details for invalid calendar dates and ISO failures. The package exports now include ParseError, tests were updated to validate the exception-based contract and error metadata, and docs were updated to match the new behavior. It also hardens the parse_date docstring example so doctests are stable in CI. Validation run locally: uv run ruff check, uv run ty check, uv run pytest -q, and make doctest.
This pull request introduces robust error handling for date and datetime parsing in the
dateutilslibrary by replacing silent failures with explicit exceptions. The most significant change is the addition of a newParseErrorexception, which is now raised when parsing fails, providing detailed context about the error. The documentation and tests have been updated to reflect and validate this new behavior.Error handling and API changes
ParseErrorexception class for failed parsing operations, replacing previous behavior where parsing would returnNonefor invalid inputs. This exception includes details such as the parser name, input value, reason, and attempted formats. (dateutils/dateutils.py,dateutils/__init__.py) [1] [2] [3]parse_date,parse_datetime, andparse_iso8601functions to raiseParseErroron failure instead of returningNone. This includes handling invalid calendar dates, unknown month names, and missing or mismatched formats. (dateutils/dateutils.py) [1] [2] [3] [4] [5] [6] [7]Documentation updates
README.mdto document the new error handling approach, including examples of catchingParseErrorand its detailed error messages. (README.md) [1] [2] [3] [4]Test suite improvements
ParseErrorinstead ofNonefor invalid date and datetime inputs. Added new tests to verify error details and edge cases. (tests/test_dateutils.py) [1] [2]Internal refactoring
dateutils/dateutils.py) [1] [2] [3]These changes make date and datetime parsing more reliable and user-friendly, as errors are now surfaced explicitly with actionable information.