You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Improve InvalidTZPathWarning warning with a stacklevel #106233
And this command to run it: PYTHONTZPATH=ex.py ./python.exe ex.py
Output will be:
» PYTHONTZPATH=ex.py ./python.exe ex.py
/Users/sobolev/Desktop/cpython/Lib/zoneinfo/_tzpath.py:44: InvalidTZPathWarning: Invalid paths specified in PYTHONTZPATH environment variable. Paths should be absolute but found the following relative paths:
ex.py
warnings.warn(
()
Setting stacklevel
From 1 to 5:
/Users/sobolev/Desktop/cpython/Lib/zoneinfo/_tzpath.py:44: InvalidTZPathWarning: Invalid paths specified in PYTHONTZPATH environment variable. Paths should be absolute but found the following relative paths:
ex.py
warnings.warn(
/Users/sobolev/Desktop/cpython/Lib/zoneinfo/_tzpath.py:22: InvalidTZPathWarning: Invalid paths specified in PYTHONTZPATH environment variable. Paths should be absolute but found the following relative paths:
ex.py
base_tzpath = _parse_python_tzpath(env_var)
/Users/sobolev/Desktop/cpython/Lib/zoneinfo/_tzpath.py:176: InvalidTZPathWarning: Invalid paths specified in PYTHONTZPATH environment variable. Paths should be absolute but found the following relative paths:
ex.py
reset_tzpath()
/Users/sobolev/Desktop/cpython/Lib/zoneinfo/__init__.py:10: InvalidTZPathWarning: Invalid paths specified in PYTHONTZPATH environment variable. Paths should be absolute but found the following relative paths:
ex.py
from . import _tzpath
/Users/sobolev/Desktop/cpython/ex.py:1: InvalidTZPathWarning: Invalid paths specified in PYTHONTZPATH environment variable. Paths should be absolute but found the following relative paths:
ex.py
import zoneinfo
Looks like 5 is the best in terms of being informative, where this warning comes from.
I will send a PR with the fix.
reset_tzpath() is a public API. When you call it directly the warning should point at the caller place.
There is an existing test for InvalidTZPathWarning: test_env_variable_relative_paths. Test that it points at the right place:
- with self.assertWarns(self.module.InvalidTZPathWarning):+ with self.assertWarns(self.module.InvalidTZPathWarning) as w:
self.module.reset_tzpath()
+ self.assertEqual(w.filename, __file__)
Activity
pythongh-106233: Fix stacklevel in `zoneinfo.InvalidTZPathWarning`
serhiy-storchaka commentedon Jul 1, 2023
reset_tzpath()is a public API. When you call it directly the warning should point at the caller place.There is an existing test for InvalidTZPathWarning: test_env_variable_relative_paths. Test that it points at the right place:
gh-106233: Fix stacklevel in zoneinfo.InvalidTZPathWarning (GH-106234)
pythongh-106233: Fix stacklevel in zoneinfo.InvalidTZPathWarning (pyt…
[3.12] gh-106233: Fix stacklevel in zoneinfo.InvalidTZPathWarning (GH…
pythongh-106233: Fix stacklevel in zoneinfo.InvalidTZPathWarning (pyt…
hugovk commentedon Mar 15, 2024
Triage: can this be closed or is there more to do?