Remove Python 2 importlib.import_module compat (#81879)

pull/81874/head
Sviatoslav Sydorenko 1 year ago committed by GitHub
parent 92d2c66db2
commit 350a394185
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -9,6 +9,7 @@ from __future__ import annotations
# ansible.cli needs to be imported first, to ensure the source bin/* scripts run that code first
from ansible.cli import CLI
import importlib
import pkgutil
import os
import os.path
@ -29,7 +30,6 @@ from ansible.module_utils.common.text.converters import to_native, to_text
from ansible.module_utils.common.collections import is_sequence
from ansible.module_utils.common.json import json_dump
from ansible.module_utils.common.yaml import yaml_dump
from ansible.module_utils.compat import importlib
from ansible.module_utils.six import string_types
from ansible.parsing.plugin_docs import read_docstub
from ansible.parsing.utils.yaml import from_yaml

@ -10,13 +10,13 @@ import os
import pkgutil
import random
import re
from importlib import import_module
from ansible.module_utils.compat.version import LooseVersion
from ansible import constants as C
from ansible.errors import AnsibleError
from ansible.module_utils.common.text.converters import to_bytes, to_native, to_text
from ansible.module_utils.compat.importlib import import_module
from ansible.plugins.loader import ps_module_utils_loader
from ansible.utils.collection_loader import resource_from_fqcr

@ -3,14 +3,24 @@
from __future__ import annotations
import sys
from ansible.module_utils.common.warnings import deprecate
try:
from importlib import import_module # pylint: disable=unused-import
except ImportError:
# importlib.import_module returns the tail
# whereas __import__ returns the head
# compat to work like importlib.import_module
def import_module(name): # type: ignore[misc]
__import__(name)
return sys.modules[name]
def __getattr__(importable_name):
"""Inject import-time deprecation warnings.
Specifically, for ``import_module()``.
"""
if importable_name == 'import_module':
deprecate(
msg=f'The `ansible.module_utils.compat.importlib.'
f'{importable_name}` function is deprecated.',
version='2.19',
)
from importlib import import_module
return import_module
raise AttributeError(
f'cannot import name {importable_name !r} '
f'has no attribute ({__file__ !s})',
)

@ -14,6 +14,7 @@ import sys
import warnings
from collections import defaultdict, namedtuple
from importlib import import_module
from traceback import format_exc
import ansible.module_utils.compat.typing as t
@ -25,7 +26,6 @@ from ansible import __version__ as ansible_version
from ansible import constants as C
from ansible.errors import AnsibleError, AnsiblePluginCircularRedirect, AnsiblePluginRemovedError, AnsibleCollectionUnsupportedVersionError
from ansible.module_utils.common.text.converters import to_bytes, to_text, to_native
from ansible.module_utils.compat.importlib import import_module
from ansible.module_utils.six import string_types
from ansible.parsing.utils.yaml import from_yaml
from ansible.parsing.yaml.loader import AnsibleLoader

@ -5,9 +5,9 @@ import pkgutil
import pytest
import re
import sys
from importlib import import_module
from ansible.module_utils.six import PY3, string_types
from ansible.module_utils.compat.importlib import import_module
from ansible.modules import ping as ping_module
from ansible.utils.collection_loader import AnsibleCollectionConfig, AnsibleCollectionRef
from ansible.utils.collection_loader._collection_finder import (

Loading…
Cancel
Save