Skip to content

Commit bad8647

Browse files
committed
gh-140039: Improve ZoneInfo error messages with detailed guidance
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.
1 parent 38c814a commit bad8647

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

Lib/zoneinfo/_common.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,28 @@ def load_tzdata(key):
1717
except ImportError:
1818
# If package_name doesn't exist, it means tzdata is not installed
1919
# or there's an error in the folder name like Amrica/New_York
20-
msg = (
21-
f"No time zone found with key {key}.\n\n"
22-
"This error may occur if timezone data is not available. "
23-
"To resolve this:\n"
24-
" • Install the tzdata package: python -m pip install tzdata\n"
25-
" • Ensure your operating system has timezone data installed\n"
26-
" • Verify the timezone key is correct (e.g., 'America/New_York')\n\n"
27-
"For more information, see:\n"
28-
"https://docs.python.org/3/library/zoneinfo.html"
29-
)
30-
raise ZoneInfoNotFoundError(msg)
20+
exc = ZoneInfoNotFoundError(f"No time zone found with key {key}")
21+
22+
# Check if tzdata is actually missing
23+
try:
24+
import tzdata
25+
except ImportError:
26+
# tzdata is not installed, provide installation instructions
27+
exc.add_note(
28+
"This error may occur if timezone data is not available. "
29+
"To resolve this:"
30+
)
31+
exc.add_note(" - Install the tzdata package: python -m pip install tzdata")
32+
exc.add_note(" - Verify the timezone key is correct (for example, 'America/New_York')")
33+
exc.add_note("")
34+
exc.add_note("For more information, see:")
35+
exc.add_note("https://docs.python.org/3/library/zoneinfo.html")
36+
else:
37+
# tzdata is installed but the key wasn't found
38+
exc.add_note(f"The timezone key '{key}' was not found in the tzdata package.")
39+
exc.add_note("Please verify the timezone key is correct (for example, 'America/New_York').")
40+
41+
raise exc
3142
except (FileNotFoundError, UnicodeEncodeError, IsADirectoryError):
3243
# Other errors that amount to "we cannot find this key":
3344
#
@@ -177,10 +188,3 @@ def from_file(cls, stream):
177188

178189
class ZoneInfoNotFoundError(KeyError):
179190
"""Exception raised when a ZoneInfo key is not found."""
180-
181-
def __str__(self):
182-
# Override __str__ to return the message directly without repr() formatting
183-
# that KeyError applies by default
184-
if self.args:
185-
return str(self.args[0])
186-
return super().__str__()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Improve error message in :mod:`zoneinfo` when timezone data is not found due
2+
to missing :pypi:`tzdata` package. The new error message provides clear
3+
instructions on how to resolve the issue, including installing the package
4+
and links to documentation. Patch by daram62.

0 commit comments

Comments
 (0)