diff --git a/changelogs/fragments/217-duplicate-imports.yml b/changelogs/fragments/217-duplicate-imports.yml new file mode 100644 index 00000000000..ca6f8533474 --- /dev/null +++ b/changelogs/fragments/217-duplicate-imports.yml @@ -0,0 +1,2 @@ +minor_changes: +- various AWS plugins and module_utils - Cleanup unused imports (https://github.com/ansible-collections/amazon.aws/pull/217). diff --git a/plugins/lookup/aws_account_attribute.py b/plugins/lookup/aws_account_attribute.py index a80ca292875..ca9c57c9bc4 100644 --- a/plugins/lookup/aws_account_attribute.py +++ b/plugins/lookup/aws_account_attribute.py @@ -52,8 +52,6 @@ (or all attributes if one is not specified). """ -import os - from ansible.errors import AnsibleError try: @@ -63,13 +61,8 @@ raise AnsibleError("The lookup aws_account_attribute requires boto3 and botocore.") from ansible.module_utils._text import to_native -from ansible.module_utils.six import string_types -from ansible.plugins import AnsiblePlugin from ansible.plugins.lookup import LookupBase -from ..module_utils.ec2 import boto3_conn -from ..module_utils.ec2 import get_aws_connection_info - def _boto3_conn(region, credentials): boto_profile = credentials.pop('aws_profile', None) diff --git a/plugins/lookup/aws_secret.py b/plugins/lookup/aws_secret.py index f8b978fd23f..1bc1f1968f7 100644 --- a/plugins/lookup/aws_secret.py +++ b/plugins/lookup/aws_secret.py @@ -92,7 +92,6 @@ except ImportError: raise AnsibleError("The lookup aws_secret requires boto3 and botocore.") -from ansible.plugins import AnsiblePlugin from ansible.plugins.lookup import LookupBase from ansible.module_utils._text import to_native from ansible_collections.amazon.aws.plugins.module_utils.core import is_boto3_error_code diff --git a/plugins/module_utils/acm.py b/plugins/module_utils/acm.py index ed298dc82d4..0fc86516e4a 100644 --- a/plugins/module_utils/acm.py +++ b/plugins/module_utils/acm.py @@ -29,10 +29,8 @@ """ Common Amazon Certificate Manager facts shared between modules """ -import traceback try: - import botocore from botocore.exceptions import BotoCoreError, ClientError except ImportError: pass diff --git a/plugins/module_utils/cloudfront_facts.py b/plugins/module_utils/cloudfront_facts.py index 4498f57c625..994b84da63c 100644 --- a/plugins/module_utils/cloudfront_facts.py +++ b/plugins/module_utils/cloudfront_facts.py @@ -34,8 +34,6 @@ except ImportError: pass -from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict - from .ec2 import AWSRetry from .ec2 import boto3_tag_list_to_ansible_dict diff --git a/plugins/module_utils/direct_connect.py b/plugins/module_utils/direct_connect.py index 22cc204ad37..abcbcfd232d 100644 --- a/plugins/module_utils/direct_connect.py +++ b/plugins/module_utils/direct_connect.py @@ -39,8 +39,6 @@ except ImportError: pass -from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict - from .ec2 import AWSRetry diff --git a/plugins/module_utils/ec2.py b/plugins/module_utils/ec2.py index 8bc29f49a7d..e22789922c5 100644 --- a/plugins/module_utils/ec2.py +++ b/plugins/module_utils/ec2.py @@ -39,14 +39,15 @@ from ansible.module_utils.ansible_release import __version__ from ansible.module_utils.basic import env_fallback from ansible.module_utils.basic import missing_required_lib -from ansible.module_utils.common.dict_transformations import _camel_to_snake -from ansible.module_utils.common.dict_transformations import _snake_to_camel -from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict -from ansible.module_utils.common.dict_transformations import snake_dict_to_camel_dict from ansible.module_utils.six import binary_type from ansible.module_utils.six import string_types from ansible.module_utils.six import text_type from ansible.module_utils.six import integer_types +# Used to live here, moved into ansible.module_utils.common.dict_transformations +from ansible.module_utils.common.dict_transformations import _camel_to_snake # pylint: disable=unused-import +from ansible.module_utils.common.dict_transformations import _snake_to_camel # pylint: disable=unused-import +from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict # pylint: disable=unused-import +from ansible.module_utils.common.dict_transformations import snake_dict_to_camel_dict # pylint: disable=unused-import from .cloud import CloudRetry @@ -581,7 +582,6 @@ def get_sg_id(sg, boto3): if len(unmatched) > 0: # If we have unmatched names that look like an ID, assume they are - import re sec_group_id_list[:] = [sg for sg in unmatched if re.match('sg-[a-fA-F0-9]+$', sg)] still_unmatched = [sg for sg in unmatched if not re.match('sg-[a-fA-F0-9]+$', sg)] if len(still_unmatched) > 0: diff --git a/plugins/module_utils/elbv2.py b/plugins/module_utils/elbv2.py index 8ae12e01172..2a53ce5293b 100644 --- a/plugins/module_utils/elbv2.py +++ b/plugins/module_utils/elbv2.py @@ -12,8 +12,6 @@ except ImportError: pass -from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict - from .ec2 import AWSRetry from .ec2 import ansible_dict_to_boto3_tag_list from .ec2 import boto3_tag_list_to_ansible_dict diff --git a/plugins/module_utils/urls.py b/plugins/module_utils/urls.py index 387d1f5ca62..e00f485c5d2 100644 --- a/plugins/module_utils/urls.py +++ b/plugins/module_utils/urls.py @@ -18,7 +18,6 @@ from ansible.module_utils.urls import open_url from .ec2 import HAS_BOTO3 -from .ec2 import boto3_conn from .ec2 import get_aws_connection_info diff --git a/tests/integration/targets/module_utils_waiter/roles/get_waiter/library/example_module.py b/tests/integration/targets/module_utils_waiter/roles/get_waiter/library/example_module.py index bcbb4204684..4e16fb1bc9d 100644 --- a/tests/integration/targets/module_utils_waiter/roles/get_waiter/library/example_module.py +++ b/tests/integration/targets/module_utils_waiter/roles/get_waiter/library/example_module.py @@ -9,11 +9,6 @@ __metaclass__ = type -try: - from botocore.exceptions import BotoCoreError, ClientError -except ImportError: - pass # Handled by AnsibleAWSModule - from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry from ansible_collections.amazon.aws.plugins.module_utils.waiters import get_waiter diff --git a/tests/unit/compat/builtins.py b/tests/unit/compat/builtins.py index f60ee678228..349d310e86d 100644 --- a/tests/unit/compat/builtins.py +++ b/tests/unit/compat/builtins.py @@ -26,7 +26,7 @@ # One unittest needs to import builtins via __import__() so we need to have # the string that represents it try: - import __builtin__ + import __builtin__ # pylint: disable=unused-import except ImportError: BUILTINS = 'builtins' else: diff --git a/tests/unit/module_utils/ec2/test_aws.py b/tests/unit/module_utils/ec2/test_aws.py index 875ef440575..91170b9e693 100644 --- a/tests/unit/module_utils/ec2/test_aws.py +++ b/tests/unit/module_utils/ec2/test_aws.py @@ -8,7 +8,9 @@ __metaclass__ = type try: - import boto3 + # We explicitly want to know if boto3/botocore are available, they're used + # by the code we're testing even if we don't directly use them. + import boto3 # pylint: disable=unused-import import botocore HAS_BOTO3 = True except Exception: diff --git a/tests/unit/modules/test_cloudformation.py b/tests/unit/modules/test_cloudformation.py index 85d5f7fdcf2..6ee1fcf95d5 100644 --- a/tests/unit/modules/test_cloudformation.py +++ b/tests/unit/modules/test_cloudformation.py @@ -9,7 +9,9 @@ import pytest -from ansible_collections.amazon.aws.tests.unit.utils.amazon_placebo_fixtures import placeboify, maybe_sleep +# Magic... +from ansible_collections.amazon.aws.tests.unit.utils.amazon_placebo_fixtures import maybe_sleep, placeboify # pylint: disable=unused-import + from ansible_collections.amazon.aws.plugins.module_utils.ec2 import boto_exception from ansible_collections.amazon.aws.plugins.modules import cloudformation as cfn_module diff --git a/tests/unit/plugins/lookup/test_aws_secret.py b/tests/unit/plugins/lookup/test_aws_secret.py index 1f3957419ee..dd27bbf60af 100644 --- a/tests/unit/plugins/lookup/test_aws_secret.py +++ b/tests/unit/plugins/lookup/test_aws_secret.py @@ -21,7 +21,6 @@ import pytest import datetime -import sys from copy import copy from ansible.errors import AnsibleError