ansible-core/avoid-importlib-resources-abc-deprecation.patch
Maxwell G b700681263
Avoid deprecated importlib.abc.TraversableResources
This fixes python-ansible-compat's FTBFS with python3.12, as its test
suite enforces -Werror.
2023-06-17 18:15:18 +00:00

48 lines
2.1 KiB
Diff

From 2d84891781d6afb26fa5e3df1140602fa0fd7830 Mon Sep 17 00:00:00 2001
From: Maxwell G <maxwell@gtmx.me>
Date: Sat, 17 Jun 2023 17:35:41 +0000
Subject: [PATCH] Avoid usage of deprecated importlib.abc.TraversableResources
This fixes ansible-compat test failures with Python 3.12.
---
.../fragments/81082-deprecated-importlib-abc.yml | 5 +++++
.../utils/collection_loader/_collection_finder.py | 12 +++++++++++-
2 files changed, 16 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..edac8fa6f225b2 100644
--- a/lib/ansible/utils/collection_loader/_collection_finder.py
+++ b/lib/ansible/utils/collection_loader/_collection_finder.py
@@ -40,8 +40,18 @@ 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
except ImportError:
+ # Python < 3.9
TraversableResources = object # type: ignore[assignment,misc]
try: