8000 Improve `InvalidTZPathWarning` warning with a stacklevel · Issue #106233 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

Improve InvalidTZPathWarning warning with a stacklevel #106233

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

Closed
sobolevn opened this issue Jun 29, 2023 · 2 comments
Closed

Improve InvalidTZPathWarning warning with a stacklevel #106233

sobolevn opened this issue Jun 29, 2023 · 2 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@sobolevn
Copy link
Member
sobolevn commented Jun 29, 2023

Given this code:

# ex.py
import zoneinfo

print(zoneinfo.TZPATH)

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.

CC @pganssle

Linked PRs

@sobolevn sobolevn added type-bug An unexpected behavior, bug, or error stdlib Python modules in the Lib dir labels Jun 29, 2023
@sobolevn sobolevn self-assigned this Jun 29, 2023
sobolevn added a commit to sobolevn/cpython that referenced this issue Jun 29, 2023
@serhiy-storchaka
Copy link
Member

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__)

miss-islington pushed a commit to miss-islington/cpython that referenced this issue Feb 6, 2024
…honGH-106234)

(cherry picked from commit d7334e2)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
serhiy-storchaka pushed a commit that referenced this issue Feb 6, 2024
…-106234) (GH-115081)

(cherry picked from commit d7334e2)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
@hugovk
Copy link
Member
hugovk commented Mar 15, 2024

Triage: can this be closed or is there more to do?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants
0