From 7450e87615b0d9b1503a0f966bd3327901decb75 Mon Sep 17 00:00:00 2001 From: Matt Clay Date: Wed, 11 Aug 2021 15:44:50 -0700 Subject: [PATCH] Fix up string splitting to prepare for pylint update. (#75479) * Use rsplit with maxsplit in BaseCacheModule. * Use maxsplit for accessing first item. * ansible-test - More efficient splitting. --- changelogs/fragments/code-cleanup.yml | 2 ++ lib/ansible/plugins/cache/__init__.py | 2 +- .../ansible_test/_internal/commands/integration/__init__.py | 2 +- .../_internal/commands/integration/cloud/__init__.py | 2 +- test/units/executor/test_task_executor.py | 4 ++-- 5 files changed, 7 insertions(+), 5 deletions(-) diff --git a/changelogs/fragments/code-cleanup.yml b/changelogs/fragments/code-cleanup.yml index a7a68418b7a..2309daab50b 100644 --- a/changelogs/fragments/code-cleanup.yml +++ b/changelogs/fragments/code-cleanup.yml @@ -4,3 +4,5 @@ minor_changes: - unicode utils - Fix ``__all__`` which was incorrectly declared as a string instead of a tuple. - connection base - Avoid using deprecated ``@abstractproperty`` decorator. - user module - Remove unused code. + - cache base - More efficient string splitting. + - ansible-test - More efficient string splitting. diff --git a/lib/ansible/plugins/cache/__init__.py b/lib/ansible/plugins/cache/__init__.py index f164acee7b2..bd023a5c348 100644 --- a/lib/ansible/plugins/cache/__init__.py +++ b/lib/ansible/plugins/cache/__init__.py @@ -47,7 +47,7 @@ class BaseCacheModule(AnsiblePlugin): if not hasattr(self, '_load_name'): display.deprecated('Rather than importing custom CacheModules directly, use ansible.plugins.loader.cache_loader', version='2.14', collection_name='ansible.builtin') - self._load_name = self.__module__.split('.')[-1] + self._load_name = self.__module__.rsplit('.', 1)[-1] self._load_name = resource_from_fqcr(self.__module__) super(BaseCacheModule, self).__init__() self.set_options(var_options=args, direct=kwargs) diff --git a/test/lib/ansible_test/_internal/commands/integration/__init__.py b/test/lib/ansible_test/_internal/commands/integration/__init__.py index 84ea1874d81..75a504bc1a8 100644 --- a/test/lib/ansible_test/_internal/commands/integration/__init__.py +++ b/test/lib/ansible_test/_internal/commands/integration/__init__.py @@ -1130,7 +1130,7 @@ class EnvironmentDescription: versions = [''] versions += SUPPORTED_PYTHON_VERSIONS - versions += list(set(v.split('.')[0] for v in SUPPORTED_PYTHON_VERSIONS)) + versions += list(set(v.split('.', 1)[0] for v in SUPPORTED_PYTHON_VERSIONS)) version_check = os.path.join(ANSIBLE_TEST_DATA_ROOT, 'versions.py') python_paths = dict((v, find_executable('python%s' % v, required=False)) for v in sorted(versions)) diff --git a/test/lib/ansible_test/_internal/commands/integration/cloud/__init__.py b/test/lib/ansible_test/_internal/commands/integration/cloud/__init__.py index 5c9c30de417..8d4bbec1ea2 100644 --- a/test/lib/ansible_test/_internal/commands/integration/cloud/__init__.py +++ b/test/lib/ansible_test/_internal/commands/integration/cloud/__init__.py @@ -173,7 +173,7 @@ class CloudBase(ABC): def __init__(self, args): # type: (IntegrationConfig) -> None self.args = args - self.platform = self.__module__.split('.')[-1] + self.platform = self.__module__.rsplit('.', 1)[-1] def config_callback(files): # type: (t.List[t.Tuple[str, str]]) -> None """Add the config file to the payload file list.""" diff --git a/test/units/executor/test_task_executor.py b/test/units/executor/test_task_executor.py index b27faf9b616..8c01b339c10 100644 --- a/test/units/executor/test_task_executor.py +++ b/test/units/executor/test_task_executor.py @@ -246,7 +246,7 @@ class TestTaskExecutor(unittest.TestCase): mock_connection = MagicMock() mock_templar = MagicMock() action = 'namespace.netconf_suffix' - module_prefix = action.split('_')[0] + module_prefix = action.split('_', 1)[0] te._task.action = action handler = te._get_action_handler(mock_connection, mock_templar) @@ -281,7 +281,7 @@ class TestTaskExecutor(unittest.TestCase): mock_connection = MagicMock() mock_templar = MagicMock() action = 'namespace.prefix_suffix' - module_prefix = action.split('_')[0] + module_prefix = action.split('_', 1)[0] te._task.action = action handler = te._get_action_handler(mock_connection, mock_templar)