diff --git a/test/lib/ansible_test/_internal/__init__.py b/test/lib/ansible_test/_internal/__init__.py index 43d10c027d9..69d93aa06db 100644 --- a/test/lib/ansible_test/_internal/__init__.py +++ b/test/lib/ansible_test/_internal/__init__.py @@ -89,10 +89,10 @@ def main(cli_args=None): # type: (t.Optional[t.List[str]]) -> None display.review_warnings() config.success = True except ApplicationWarning as ex: - display.warning(u'%s' % ex) + display.warning('%s' % ex) sys.exit(0) except ApplicationError as ex: - display.fatal(u'%s' % ex) + display.fatal('%s' % ex) sys.exit(1) except KeyboardInterrupt: sys.exit(2) diff --git a/test/lib/ansible_test/_internal/commands/coverage/__init__.py b/test/lib/ansible_test/_internal/commands/coverage/__init__.py index 3d27b64bb6e..434582af9a5 100644 --- a/test/lib/ansible_test/_internal/commands/coverage/__init__.py +++ b/test/lib/ansible_test/_internal/commands/coverage/__init__.py @@ -250,7 +250,7 @@ def enumerate_powershell_lines( try: coverage_run = read_json_file(path) except Exception as ex: # pylint: disable=locally-disabled, broad-except - display.error(u'%s' % ex) + display.error('%s' % ex) return for filename, hits in coverage_run.items(): diff --git a/test/lib/ansible_test/_internal/commands/integration/__init__.py b/test/lib/ansible_test/_internal/commands/integration/__init__.py index d5f497b5416..bd0f32c2fec 100644 --- a/test/lib/ansible_test/_internal/commands/integration/__init__.py +++ b/test/lib/ansible_test/_internal/commands/integration/__init__.py @@ -263,7 +263,7 @@ def integration_test_environment( root_temp_dir = os.path.join(ResultType.TMP.path, 'integration') prefix = '%s-' % target.name - suffix = u'-\u00c5\u00d1\u015a\u00cc\u03b2\u0141\u00c8' + suffix = '-\u00c5\u00d1\u015a\u00cc\u03b2\u0141\u00c8' if args.no_temp_unicode or 'no/temp_unicode/' in target.aliases: display.warning('Disabling unicode in the temp work dir is a temporary debugging feature that may be removed in the future without notice.') diff --git a/test/lib/ansible_test/_internal/commands/integration/cloud/vcenter.py b/test/lib/ansible_test/_internal/commands/integration/cloud/vcenter.py index 2093b461c89..36a65b7b415 100644 --- a/test/lib/ansible_test/_internal/commands/integration/cloud/vcenter.py +++ b/test/lib/ansible_test/_internal/commands/integration/cloud/vcenter.py @@ -100,7 +100,7 @@ class VcenterEnvironment(CloudEnvironment): parser = configparser.ConfigParser() parser.read(self.config_path) # static - env_vars = dict() + env_vars = {} ansible_vars = dict( resource_prefix=self.resource_prefix, ) diff --git a/test/lib/ansible_test/_internal/commands/sanity/__init__.py b/test/lib/ansible_test/_internal/commands/sanity/__init__.py index 05d8efd95cf..19cc3006afe 100644 --- a/test/lib/ansible_test/_internal/commands/sanity/__init__.py +++ b/test/lib/ansible_test/_internal/commands/sanity/__init__.py @@ -1010,7 +1010,7 @@ class SanityCodeSmellTest(SanitySingleVersion): return SanityFailure(self.name, messages=messages) if stderr or status: - summary = u'%s' % SubprocessError(cmd=cmd, status=status, stderr=stderr, stdout=stdout) + summary = '%s' % SubprocessError(cmd=cmd, status=status, stderr=stderr, stdout=stdout) return SanityFailure(self.name, summary=summary) messages = settings.process_errors([], paths) diff --git a/test/lib/ansible_test/_internal/commands/sanity/ansible_doc.py b/test/lib/ansible_test/_internal/commands/sanity/ansible_doc.py index 0b421ed376d..96de8130a9a 100644 --- a/test/lib/ansible_test/_internal/commands/sanity/ansible_doc.py +++ b/test/lib/ansible_test/_internal/commands/sanity/ansible_doc.py @@ -102,7 +102,7 @@ class AnsibleDocTest(SanitySingleVersion): status = ex.status if status: - summary = u'%s' % SubprocessError(cmd=cmd, status=status, stderr=stderr) + summary = '%s' % SubprocessError(cmd=cmd, status=status, stderr=stderr) return SanityFailure(self.name, summary=summary) if stdout: @@ -113,7 +113,7 @@ class AnsibleDocTest(SanitySingleVersion): stderr = re.sub(r'\[WARNING]: [^ ]+ [^ ]+ has been removed\n', '', stderr).strip() if stderr: - summary = u'Output on stderr from ansible-doc is considered an error.\n\n%s' % SubprocessError(cmd, stderr=stderr) + summary = 'Output on stderr from ansible-doc is considered an error.\n\n%s' % SubprocessError(cmd, stderr=stderr) return SanityFailure(self.name, summary=summary) if args.explain: diff --git a/test/lib/ansible_test/_internal/commands/sanity/pslint.py b/test/lib/ansible_test/_internal/commands/sanity/pslint.py index 254b7778c31..6eb2e5d60b1 100644 --- a/test/lib/ansible_test/_internal/commands/sanity/pslint.py +++ b/test/lib/ansible_test/_internal/commands/sanity/pslint.py @@ -96,9 +96,9 @@ class PslintTest(SanityVersionNeutral): cwd = data_context().content.root + '/' # replace unicode smart quotes and ellipsis with ascii versions - stdout = re.sub(u'[\u2018\u2019]', "'", stdout) - stdout = re.sub(u'[\u201c\u201d]', '"', stdout) - stdout = re.sub(u'[\u2026]', '...', stdout) + stdout = re.sub('[\u2018\u2019]', "'", stdout) + stdout = re.sub('[\u201c\u201d]', '"', stdout) + stdout = re.sub('[\u2026]', '...', stdout) messages = json.loads(stdout) diff --git a/test/lib/ansible_test/_internal/commands/sanity/pylint.py b/test/lib/ansible_test/_internal/commands/sanity/pylint.py index e1f983e5839..edc50e86f9a 100644 --- a/test/lib/ansible_test/_internal/commands/sanity/pylint.py +++ b/test/lib/ansible_test/_internal/commands/sanity/pylint.py @@ -221,7 +221,7 @@ class PylintTest(SanitySingleVersion): if parser.has_section('ansible-test'): config = dict(parser.items('ansible-test')) else: - config = dict() + config = {} disable_plugins = set(i.strip() for i in config.get('disable-plugins', '').split(',') if i) load_plugins = set(plugin_names + ['pylint.extensions.mccabe']) - disable_plugins diff --git a/test/lib/ansible_test/_internal/encoding.py b/test/lib/ansible_test/_internal/encoding.py index 189b44c01f7..3c6314dfbf8 100644 --- a/test/lib/ansible_test/_internal/encoding.py +++ b/test/lib/ansible_test/_internal/encoding.py @@ -5,15 +5,13 @@ import typing as t ENCODING = 'utf-8' -Text = type(u'') - def to_optional_bytes(value, errors='strict'): # type: (t.Optional[t.AnyStr], str) -> t.Optional[bytes] """Return the given value as bytes encoded using UTF-8 if not already bytes, or None if the value is None.""" return None if value is None else to_bytes(value, errors) -def to_optional_text(value, errors='strict'): # type: (t.Optional[t.AnyStr], str) -> t.Optional[t.Text] +def to_optional_text(value, errors='strict'): # type: (t.Optional[t.AnyStr], str) -> t.Optional[str] """Return the given value as text decoded using UTF-8 if not already text, or None if the value is None.""" return None if value is None else to_text(value, errors) @@ -23,18 +21,18 @@ def to_bytes(value, errors='strict'): # type: (t.AnyStr, str) -> bytes if isinstance(value, bytes): return value - if isinstance(value, Text): + if isinstance(value, str): return value.encode(ENCODING, errors) raise Exception('value is not bytes or text: %s' % type(value)) -def to_text(value, errors='strict'): # type: (t.AnyStr, str) -> t.Text +def to_text(value, errors='strict'): # type: (t.AnyStr, str) -> str """Return the given value as text decoded using UTF-8 if not already text.""" if isinstance(value, bytes): return value.decode(ENCODING, errors) - if isinstance(value, Text): + if isinstance(value, str): return value raise Exception('value is not bytes or text: %s' % type(value)) diff --git a/test/lib/ansible_test/_internal/http.py b/test/lib/ansible_test/_internal/http.py index 1375d23ed3a..dbe310698ef 100644 --- a/test/lib/ansible_test/_internal/http.py +++ b/test/lib/ansible_test/_internal/http.py @@ -92,7 +92,7 @@ class HttpClient: break except SubprocessError as ex: if ex.status in retry_on_status and attempts < max_attempts: - display.warning(u'%s' % ex) + display.warning('%s' % ex) time.sleep(sleep_seconds) continue diff --git a/test/lib/ansible_test/_internal/target.py b/test/lib/ansible_test/_internal/target.py index 879a7944ec6..652359d7897 100644 --- a/test/lib/ansible_test/_internal/target.py +++ b/test/lib/ansible_test/_internal/target.py @@ -40,7 +40,7 @@ def find_target_completion(target_func, prefix, short): # type: (t.Callable[[], matches = list(walk_completion_targets(targets, prefix, short)) return matches except Exception as ex: # pylint: disable=locally-disabled, broad-except - return [u'%s' % ex] + return ['%s' % ex] def walk_completion_targets(targets, prefix, short=False): # type: (t.Iterable[CompletionTarget], str, bool) -> t.Tuple[str, ...] diff --git a/test/lib/ansible_test/_internal/util.py b/test/lib/ansible_test/_internal/util.py index 25cca59d40c..9409b1e5933 100644 --- a/test/lib/ansible_test/_internal/util.py +++ b/test/lib/ansible_test/_internal/util.py @@ -52,7 +52,7 @@ from .constants import ( ) C = t.TypeVar('C') -TType = t.TypeVar('TType') +TBase = t.TypeVar('TBase') TKey = t.TypeVar('TKey') TValue = t.TypeVar('TValue') @@ -449,8 +449,8 @@ def raw_command( data_bytes = to_optional_bytes(data) stdout_bytes, stderr_bytes = communicate_with_process(process, data_bytes, stdout == subprocess.PIPE, stderr == subprocess.PIPE, capture=capture, force_stdout=force_stdout) - stdout_text = to_optional_text(stdout_bytes, str_errors) or u'' - stderr_text = to_optional_text(stderr_bytes, str_errors) or u'' + stdout_text = to_optional_text(stdout_bytes, str_errors) or '' + stderr_text = to_optional_text(stderr_bytes, str_errors) or '' else: process.wait() stdout_text, stderr_text = None, None @@ -1036,19 +1036,19 @@ def sanitize_host_name(name): return re.sub('[^A-Za-z0-9]+', '-', name)[:63].strip('-') -def get_generic_type(base_type, generic_base_type): # type: (t.Type, t.Type[TType]) -> t.Optional[t.Type[TType]] +def get_generic_type(base_type, generic_base_type): # type: (t.Type, t.Type[TValue]) -> t.Optional[t.Type[TValue]] """Return the generic type arg derived from the generic_base_type type that is associated with the base_type type, if any, otherwise return None.""" # noinspection PyUnresolvedReferences type_arg = t.get_args(base_type.__orig_bases__[0])[0] return None if isinstance(type_arg, generic_base_type) else type_arg -def get_type_associations(base_type, generic_base_type): # type: (t.Type[TType], t.Type[TValue]) -> t.List[t.Tuple[t.Type[TValue], t.Type[TType]]] +def get_type_associations(base_type, generic_base_type): # type: (t.Type[TBase], t.Type[TValue]) -> t.List[t.Tuple[t.Type[TValue], t.Type[TBase]]] """Create and return a list of tuples associating generic_base_type derived types with a corresponding base_type derived type.""" return [item for item in [(get_generic_type(sc_type, generic_base_type), sc_type) for sc_type in get_subclasses(base_type)] if item[1]] -def get_type_map(base_type, generic_base_type): # type: (t.Type[TType], t.Type[TValue]) -> t.Dict[t.Type[TValue], t.Type[TType]] +def get_type_map(base_type, generic_base_type): # type: (t.Type[TBase], t.Type[TValue]) -> t.Dict[t.Type[TValue], t.Type[TBase]] """Create and return a mapping of generic_base_type derived types to base_type derived types.""" return {item[0]: item[1] for item in get_type_associations(base_type, generic_base_type)} diff --git a/test/lib/ansible_test/_util/controller/sanity/code-smell/changelog.py b/test/lib/ansible_test/_util/controller/sanity/code-smell/changelog.py index 983eaeb4266..924e5afeb00 100644 --- a/test/lib/ansible_test/_util/controller/sanity/code-smell/changelog.py +++ b/test/lib/ansible_test/_util/controller/sanity/code-smell/changelog.py @@ -1,3 +1,4 @@ +"""Check changelog fragment naming, syntax, etc.""" from __future__ import annotations import os @@ -6,6 +7,7 @@ import subprocess def main(): + """Main entry point.""" paths = sys.argv[1:] or sys.stdin.read().splitlines() allowed_extensions = ('.yml', '.yaml') diff --git a/test/lib/ansible_test/_util/controller/sanity/code-smell/empty-init.py b/test/lib/ansible_test/_util/controller/sanity/code-smell/empty-init.py index e0dd41cd5cd..01aef6973bb 100644 --- a/test/lib/ansible_test/_util/controller/sanity/code-smell/empty-init.py +++ b/test/lib/ansible_test/_util/controller/sanity/code-smell/empty-init.py @@ -1,3 +1,4 @@ +"""Require empty __init__.py files.""" from __future__ import annotations import os @@ -5,6 +6,7 @@ import sys def main(): + """Main entry point.""" for path in sys.argv[1:] or sys.stdin.read().splitlines(): if os.path.getsize(path) > 0: print('%s: empty __init__.py required' % path) diff --git a/test/lib/ansible_test/_util/controller/sanity/code-smell/future-import-boilerplate.py b/test/lib/ansible_test/_util/controller/sanity/code-smell/future-import-boilerplate.py index dcb02e7adb9..7b39c370f90 100644 --- a/test/lib/ansible_test/_util/controller/sanity/code-smell/future-import-boilerplate.py +++ b/test/lib/ansible_test/_util/controller/sanity/code-smell/future-import-boilerplate.py @@ -1,3 +1,4 @@ +"""Enforce proper usage of __future__ imports.""" from __future__ import annotations import ast @@ -5,6 +6,7 @@ import sys def main(): + """Main entry point.""" for path in sys.argv[1:] or sys.stdin.read().splitlines(): with open(path, 'rb') as path_fd: lines = path_fd.read().splitlines() @@ -21,7 +23,7 @@ def main(): break if missing: - with open(path) as file: + with open(path, encoding='utf-8') as file: contents = file.read() # noinspection PyBroadException diff --git a/test/lib/ansible_test/_util/controller/sanity/code-smell/line-endings.py b/test/lib/ansible_test/_util/controller/sanity/code-smell/line-endings.py index 99417b6e720..31f97ad8d14 100644 --- a/test/lib/ansible_test/_util/controller/sanity/code-smell/line-endings.py +++ b/test/lib/ansible_test/_util/controller/sanity/code-smell/line-endings.py @@ -1,9 +1,11 @@ +"""Require Unix line endings.""" from __future__ import annotations import sys def main(): + """Main entry point.""" for path in sys.argv[1:] or sys.stdin.read().splitlines(): with open(path, 'rb') as path_fd: contents = path_fd.read() diff --git a/test/lib/ansible_test/_util/controller/sanity/code-smell/metaclass-boilerplate.py b/test/lib/ansible_test/_util/controller/sanity/code-smell/metaclass-boilerplate.py index 21cb0017dff..8bdcfc9a94a 100644 --- a/test/lib/ansible_test/_util/controller/sanity/code-smell/metaclass-boilerplate.py +++ b/test/lib/ansible_test/_util/controller/sanity/code-smell/metaclass-boilerplate.py @@ -1,3 +1,4 @@ +"""Require __metaclass__ boilerplate for code that supports Python 2.x.""" from __future__ import annotations import ast @@ -5,6 +6,7 @@ import sys def main(): + """Main entry point.""" for path in sys.argv[1:] or sys.stdin.read().splitlines(): with open(path, 'rb') as path_fd: lines = path_fd.read().splitlines() @@ -20,7 +22,7 @@ def main(): break if missing: - with open(path) as file: + with open(path, encoding='utf-8') as file: contents = file.read() # noinspection PyBroadException diff --git a/test/lib/ansible_test/_util/controller/sanity/code-smell/no-assert.py b/test/lib/ansible_test/_util/controller/sanity/code-smell/no-assert.py index 71883c9f611..8c1c02769af 100644 --- a/test/lib/ansible_test/_util/controller/sanity/code-smell/no-assert.py +++ b/test/lib/ansible_test/_util/controller/sanity/code-smell/no-assert.py @@ -1,3 +1,4 @@ +"""Disallow use of assert.""" from __future__ import annotations import re @@ -7,8 +8,9 @@ ASSERT_RE = re.compile(r'^\s*assert[^a-z0-9_:]') def main(): + """Main entry point.""" for path in sys.argv[1:] or sys.stdin.read().splitlines(): - with open(path, 'r') as file: + with open(path, 'r', encoding='utf-8') as file: for i, line in enumerate(file.readlines()): matches = ASSERT_RE.findall(line) diff --git a/test/lib/ansible_test/_util/controller/sanity/code-smell/no-basestring.py b/test/lib/ansible_test/_util/controller/sanity/code-smell/no-basestring.py index bb564564ef2..74e38d72a35 100644 --- a/test/lib/ansible_test/_util/controller/sanity/code-smell/no-basestring.py +++ b/test/lib/ansible_test/_util/controller/sanity/code-smell/no-basestring.py @@ -1,3 +1,4 @@ +"""Disallow use of basestring isinstance checks.""" from __future__ import annotations import re @@ -5,8 +6,9 @@ import sys def main(): + """Main entry point.""" for path in sys.argv[1:] or sys.stdin.read().splitlines(): - with open(path, 'r') as path_fd: + with open(path, 'r', encoding='utf-8') as path_fd: for line, text in enumerate(path_fd.readlines()): match = re.search(r'(isinstance.*basestring)', text) diff --git a/test/lib/ansible_test/_util/controller/sanity/code-smell/no-dict-iteritems.py b/test/lib/ansible_test/_util/controller/sanity/code-smell/no-dict-iteritems.py index 1b728de630c..b4e40029aaf 100644 --- a/test/lib/ansible_test/_util/controller/sanity/code-smell/no-dict-iteritems.py +++ b/test/lib/ansible_test/_util/controller/sanity/code-smell/no-dict-iteritems.py @@ -1,3 +1,4 @@ +"""Disallow use of the dict.iteritems function.""" from __future__ import annotations import re @@ -5,8 +6,9 @@ import sys def main(): + """Main entry point.""" for path in sys.argv[1:] or sys.stdin.read().splitlines(): - with open(path, 'r') as path_fd: + with open(path, 'r', encoding='utf-8') as path_fd: for line, text in enumerate(path_fd.readlines()): match = re.search(r'(?