-
-
Notifications
You must be signed in to change notification settings - Fork 33.1k
gh-140039: Improve error message in zoneinfo when tzdata is missing #140040
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
…sing Add more helpful error messages when ZoneInfo cannot find timezone data, specifically when the ImportError occurs due to missing tzdata package. The new error message provides: - Clear explanation of why the error occurred - Step-by-step instructions to resolve the issue - Link to official documentation for more information Also override __str__ in ZoneInfoNotFoundError to properly display multi-line error messages (KeyError wraps messages in repr() by default). This improves user experience by making it immediately clear what went wrong and how to fix it, rather than just stating the key wasn't found.
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
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.
Some comments
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
This commit enhances the error messages when a timezone key is not found in the zoneinfo module by using PEP-678 exception notes to provide clear, actionable guidance to users. Key improvements: - Uses PEP-678 add_note() for better error message formatting - Checks if tzdata package is actually missing before suggesting installation - Provides different messages for missing tzdata vs incorrect timezone keys - Uses hyphens instead of unicode bullet points for better compatibility - Changes 'e.g.' to 'for example' for clarity - Removes OS timezone data message that was confusing for Windows users The error message now intelligently detects whether: 1. tzdata is not installed (suggests: pip install tzdata) 2. tzdata is installed but key is wrong (suggests: verify the key) This makes debugging timezone issues much easier for Python developers.
0ff2283
to
bad8647
Compare
Please do not use the Update Branch button unless necessary (e.g. fixing conflicts, jogging the CI, or very old PRs) as it uses valuable resources. For more information see the devguide. Also, please do not force push, it makes reviewing more difficult. |
Sorry about the force push! I'm new to CPython contribution and wasn't aware of this guideline. 🥲 |
- Change 'To resolve this:' to 'Try:' to avoid implying guaranteed solution - Remove else block as ZoneInfoNotFoundError is clear enough without extra notes - Simplify message structure for better readability
Would it be better if we add detailed message to some other errors except ImportError. e.g. adding a specific error message for |
# If package_name doesn't exist, it means tzdata is not installed | ||
# or there's an error in the folder name like Amrica/New_York | ||
exc = ZoneInfoNotFoundError(f"No time zone found with key {key}") | ||
|
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.
also delete the tab spaces to pass the CI lint test here and line 34.
Sounds good!! Thanks for the quick follow-up |
exc.add_note("This error may occur if timezone data is not available.") | ||
exc.add_note("Try:") | ||
exc.add_note(" - Installing the tzdata package: python -m pip install tzdata") | ||
exc.add_note(" - Verifying the timezone key is correct (for example, 'America/New_York')") | ||
exc.add_note("") | ||
exc.add_note("For more information, see:") | ||
exc.add_note("https://docs.python.org/3/library/zoneinfo.html") |
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.
Shouldn't we just add one note with a multiline string?
Enhance error messages when
ZoneInfo
cannot find timezone data by providing clear,actionable guidance using PEP-678 exception notes.
Changes
1. Enhanced ImportError handling in
Lib/zoneinfo/_common.py
:ImportError
from other exceptions to provide specific guidanceadd_note()
for better error message formattingtzdata
is actually missing vs. incorrect timezone keytzdata
is not installed: suggests installation with piptzdata
is installed but key is wrong: suggests verifying the key2. Improvements based on code review:
gh-issue-140039
formatExample
Before:
After (when tzdata is not installed):
After (when tzdata is installed but key is wrong):
Testing
Related Issue
Closes #140039