Skip to content

Add typed parse helpers for datetime, date, time and duration - #998

Open
ChrisJr404 wants to merge 1 commit into
python-pendulum:masterfrom
ChrisJr404:feat/typed-parse-helpers
Open

Add typed parse helpers for datetime, date, time and duration#998
ChrisJr404 wants to merge 1 commit into
python-pendulum:masterfrom
ChrisJr404:feat/typed-parse-helpers

Conversation

@ChrisJr404

Copy link
Copy Markdown

Pull Request Check List

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

Closes #917.

parse() returns a DateTime | Date | Time | Duration union, so in type-checked code you end up writing an isinstance assert every time you already know what you're parsing. This adds four thin helpers, parse_datetime, parse_date, parse_time and parse_duration, that take the same options as parse(), return that one type, and raise ParserError if the string turns out to represent something else.

Two of the checks have to account for the class hierarchy: DateTime subclasses Date and Interval subclasses Duration, so parse_date and parse_duration reject those cases explicitly instead of letting them slip through.

I went with the descriptive names to match the pendulum.date() / pendulum.time() constructors, but happy to rename or trim the set if you'd rather go another way.

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.

parse_dt method that specifically returns a DateTime

1 participant