-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
Open
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement
Description
Summary
Improve the error message in the zoneinfo
module when timezone data cannot be found due to a missing tzdata
package. The current error message doesn't explain why the error occurred or how to resolve it.
Current Behavior
When ZoneInfo
cannot find timezone data (typically because the tzdata
package is not installed and the system doesn't provide timezone data), users see:
>>> from zoneinfo import ZoneInfo
>>> z = ZoneInfo('America/New_York')
Traceback (most recent call last):
...
zoneinfo._common.ZoneInfoNotFoundError: 'No time zone found with key America/New_York'
Problems with current message:
- Doesn't explain why the timezone wasn't found
- Doesn't tell users how to fix the issue
- No guidance on where to find more information
Proposed Enhancement
Enhance the error message when ImportError
occurs (indicating missing tzdata
package) to provide:
- Clear explanation of what went wrong
- Actionable steps to resolve the issue
- Link to documentation for more details
Proposed error message:
>>> from zoneinfo import ZoneInfo
>>> z = ZoneInfo('America/New_York')
Traceback (most recent call last):
...
zoneinfo._common.ZoneInfoNotFoundError: No time zone found with key America/New_York.
This error may occur if timezone data is not available. To resolve this:
• Install the tzdata package: python -m pip install tzdata
• Ensure your operating system has timezone data installed
• Verify the timezone key is correct (e.g., 'America/New_York')
For more information, see:
https://docs.python.org/3/library/zoneinfo.html
Implementation Details
The implementation involves:
-
Separate
ImportError
handling in _common.py:- Currently all exceptions are caught together
- Split
ImportError
to provide specific guidance for missing tzdata
-
Override
__str__
inZoneInfoNotFoundError
:- Fix formatting issue where multi-line messages display with literal
\n
- This occurs because
ZoneInfoNotFoundError
inherits fromKeyError
, which wraps messages inrepr()
by default
- Fix formatting issue where multi-line messages display with literal
Benefits
- Better user experience: Users immediately understand what went wrong
- Reduced support burden: Clear instructions mean fewer questions
- Consistency: Aligns with Python's philosophy of helpful error messages
- Minimal risk: Only affects error message text, not behavior
Testing
- All existing tests pass (254 tests)
- Manually verified improved error message formatting
- No breaking changes to API or behavior
Code Example
# In Lib/zoneinfo/_common.py
except ImportError:
msg = (
f"No time zone found with key {key}.\n\n"
"This error may occur if timezone data is not available. "
"To resolve this:\n"
" • Install the tzdata package: python -m pip install tzdata\n"
" • Ensure your operating system has timezone data installed\n"
" • Verify the timezone key is correct (e.g., 'America/New_York')\n\n"
"For more information, see:\n"
"https://docs.python.org/3/library/zoneinfo.html"
)
raise ZoneInfoNotFoundError(msg)
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
No response
Linked PRs
Metadata
Metadata
Assignees
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement
Projects
Status
No status