-
Notifications
You must be signed in to change notification settings - Fork 823
refactor: replace if/elif chain with match/case in datetime formatting #7013
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR refactors the datetimeformat function in jinja_helpers.py to use Python 3.10+ match-case syntax instead of traditional if-elif-else statements, improving code readability and maintainability.
Key Changes
- Replaced if-elif-else chain with match-case statement for format string handling
- Maintained functional equivalence with the original implementation
- Added pattern matching with the
|operator for "shortdatetime" and "shortdate" cases
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
|
|
Copilot
AI
Nov 7, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extra blank line added. While not a functional issue, this adds unnecessary whitespace. Consider removing this blank line to maintain consistency with the other case branches (lines 298, 301, 304, 307) which don't have trailing blank lines.
| return v.replace(tzinfo=tz).date() | ||
| return v.astimezone(tz).date() | ||
|
|
||
| value_date = _as_date_in_tz(convert_value, convert_tzinfo) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this PR! Regarding this helper method wouldn't be simpler if this call was
value_date = convert_value.date()The edge cases that the as_date_in_tz function checks for have been handled in earlier steps. For example the check for Date objects is defined in line 248
Summary
This pull request refactors the date and time formatting logic to use Python’s structural pattern matching (
match/case, PEP 634) instead of multipleif/elifstatements.The new implementation improves readability and maintainability while preserving all existing behavior and localization rules.
Motivation
The original function handled several format types (
shortdatetime,longdatetime,date,time,datetime,year, etc.) using a long chain of conditional statements.By switching to
match/case, the control flow becomes clearer and easier to extend, making each format explicitly defined and simplifying future maintenance.Changes
if/elifchain with amatch format:statement.shortdatetime,shortdate,longdatetime,date,time,datetime,year).formatisshortdatetimeorshortdate.locale) and timezone (tzinfo) behavior identical to the previous implementation.DateTimeFormatErrorfor unsupported formats, same as before.Example