ansible-core/avoid-importlib-resources-abc-deprecation.patch
2023-06-22 20:15:35 +00:00

64 lines
2.7 KiB
Diff

From bd5b0b4293f454819766437cb6f8a7037affd49e Mon Sep 17 00:00:00 2001
From: Maxwell G <maxwell@gtmx.me>
Date: Tue, 20 Jun 2023 13:54:06 -0500
Subject: [PATCH] Avoid deprecated importlib.abc.TraversableResources (#81082)
* Avoid usage of deprecated importlib.abc.TraversableResources
This fixes ansible-compat test failures with Python 3.12.
* Add deprecated: marker for compat code
Co-authored-by: Matt Davis <6775756+nitzmahone@users.noreply.github.com>
* add declarative deprecation comment to < 3.9 case
Co-authored-by: Matt Clay <matt@mystile.com>
---------
Co-authored-by: Matt Davis <6775756+nitzmahone@users.noreply.github.com>
Co-authored-by: Matt Clay <matt@mystile.com>
---
.../fragments/81082-deprecated-importlib-abc.yml | 5 +++++
.../utils/collection_loader/_collection_finder.py | 13 ++++++++++++-
2 files changed, 17 insertions(+), 1 deletion(-)
create mode 100644 changelogs/fragments/81082-deprecated-importlib-abc.yml
diff --git a/changelogs/fragments/81082-deprecated-importlib-abc.yml b/changelogs/fragments/81082-deprecated-importlib-abc.yml
new file mode 100644
index 00000000000000..6dfd90a16bed66
--- /dev/null
+++ b/changelogs/fragments/81082-deprecated-importlib-abc.yml
@@ -0,0 +1,5 @@
+---
+minor_changes:
+ - Use ``importlib.resources.abc.TraversableResources`` instead of deprecated
+ ``importlib.abc.TraversableResources`` where available
+ (https:/github.com/ansible/ansible/pull/81082).
diff --git a/lib/ansible/utils/collection_loader/_collection_finder.py b/lib/ansible/utils/collection_loader/_collection_finder.py
index fc6744ffdef64e..16d0bcc6e8e92a 100644
--- a/lib/ansible/utils/collection_loader/_collection_finder.py
+++ b/lib/ansible/utils/collection_loader/_collection_finder.py
@@ -40,8 +40,19 @@ def import_module(name): # type: ignore[misc]
reload_module = reload # type: ignore[name-defined] # pylint:disable=undefined-variable
try:
- from importlib.abc import TraversableResources
+ try:
+ # Available on Python >= 3.11
+ # We ignore the import error that will trigger when running mypy with
+ # older Python versions.
+ from importlib.resources.abc import TraversableResources # type: ignore[import]
+ except ImportError:
+ # Used with Python 3.9 and 3.10 only
+ # This member is still available as an alias up until Python 3.14 but
+ # is deprecated as of Python 3.12.
+ from importlib.abc import TraversableResources # deprecated: description='TraversableResources move' python_version='3.10'
except ImportError:
+ # Python < 3.9
+ # deprecated: description='TraversableResources fallback' python_version='3.8'
TraversableResources = object # type: ignore[assignment,misc]
try: