[New-bugs-announce] [issue46124] Deprecation warning in zoneinfo module

Karthikeyan Singaravelan report at bugs.python.org
Sat Dec 18 09:04:21 EST 2021


New submission from Karthikeyan Singaravelan <tir.karthi at gmail.com>:

zoneinfo module currently emits deprecation warnings due to API changes in importlib.resources

./python -Wonce -m test test_zoneinfo
0:00:00 load avg: 0.73 Run tests sequentially
0:00:00 load avg: 0.73 [1/1] test_zoneinfo
/home/karthikeyan/stuff/python/cpython/Lib/zoneinfo/_tzpath.py:121: DeprecationWarning: open_text is deprecated. Use files() instead. Refer to https://importlib-resources.readthedocs.io/en/latest/using.html#migrating-from-legacy for migration advice.
  with resources.open_text("tzdata", "zones") as f:
/home/karthikeyan/stuff/python/cpython/Lib/zoneinfo/_common.py:12: DeprecationWarning: open_binary is deprecated. Use files() instead. Refer to https://importlib-resources.readthedocs.io/en/latest/using.html#migrating-from-legacy for migration advice.
  return importlib.resources.open_binary(package_name, resource_name)
/home/karthikeyan/stuff/python/cpython/Lib/zoneinfo/_tzpath.py:121: DeprecationWarning: open_text is deprecated. Use files() instead. Refer to https://importlib-resources.readthedocs.io/en/latest/using.html#migrating-from-legacy for migration advice.
  with resources.open_text("tzdata", "zones") as f:
/home/karthikeyan/stuff/python/cpython/Lib/zoneinfo/_common.py:12: DeprecationWarning: open_binary is deprecated. Use files() instead. Refer to https://importlib-resources.readthedocs.io/en/latest/using.html#migrating-from-legacy for migration advice.
  return importlib.resources.open_binary(package_name, resource_name)

== Tests result: SUCCESS ==

1 test OK.

Total duration: 376 ms
Tests result: SUCCESS

A fix would be to adapt the _legacy module changes inline like below patch though normalize_path is not public API and need to be inlined too.

diff --git a/Lib/zoneinfo/_common.py b/Lib/zoneinfo/_common.py
index 4c24f01bd7..bfe3fe4c3c 100644
--- a/Lib/zoneinfo/_common.py
+++ b/Lib/zoneinfo/_common.py
@@ -2,14 +2,15 @@
 
 
 def load_tzdata(key):
-    import importlib.resources
+    from importlib.resources import files
+    from importlib._common import normalize_path
 
     components = key.split("/")
     package_name = ".".join(["tzdata.zoneinfo"] + components[:-1])
     resource_name = components[-1]
 
     try:
-        return importlib.resources.open_binary(package_name, resource_name)
+        return (files(package_name) / normalize_path(resource_name)).open('rb')
     except (ImportError, FileNotFoundError, UnicodeEncodeError):
         # There are three types of exception that can be raised that all amount
         # to "we cannot find this key":
diff --git a/Lib/zoneinfo/_tzpath.py b/Lib/zoneinfo/_tzpath.py
index 672560b951..b1efe5d99e 100644
--- a/Lib/zoneinfo/_tzpath.py
+++ b/Lib/zoneinfo/_tzpath.py
@@ -111,14 +111,15 @@ def available_timezones():
         determine if a given file on the time zone search path is to open it
         and check for the "magic string" at the beginning.
     """
-    from importlib import resources
+    from importlib.resources import files
+    from importlib._common import normalize_path
 
     valid_zones = set()
 
     # Start with loading from the tzdata package if it exists: this has a
     # pre-assembled list of zones that only requires opening one file.
     try:
-        with resources.open_text("tzdata", "zones") as f:
+        with (files("tzdata") / normalize_path("zones")).open('r') as f:
             for zone in f:
                 zone = zone.strip()
                 if zone:

----------
components: Library (Lib)
messages: 408851
nosy: jaraco, p-ganssle, xtreak
priority: normal
severity: normal
status: open
title: Deprecation warning in zoneinfo module
type: behavior
versions: Python 3.11

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46124>
_______________________________________


More information about the New-bugs-announce mailing list