diff --git a/test/lib/ansible_test/_util/controller/sanity/pylint/plugins/deprecated.py b/test/lib/ansible_test/_util/controller/sanity/pylint/plugins/deprecated.py index 5f9c90fea9f..79b8bf1569f 100644 --- a/test/lib/ansible_test/_util/controller/sanity/pylint/plugins/deprecated.py +++ b/test/lib/ansible_test/_util/controller/sanity/pylint/plugins/deprecated.py @@ -6,6 +6,7 @@ from __future__ import annotations import datetime import re +import typing as t import astroid @@ -144,18 +145,6 @@ class AnsibleDeprecatedChecker(BaseChecker): }), ) - def __init__(self, *args, **kwargs): - self.collection_version = None - self.collection_name = None - super().__init__(*args, **kwargs) - - def set_option(self, optname, value, action=None, optdict=None): - super().set_option(optname, value, action, optdict) - if optname == 'collection-version' and value is not None: - self.collection_version = SemanticVersion(self.config.collection_version) - if optname == 'collection-name' and value is not None: - self.collection_name = self.config.collection_name - def _check_date(self, node, date): if not isinstance(date, str): self.add_message('ansible-invalid-deprecated-date', node=node, args=(date,)) @@ -205,6 +194,16 @@ class AnsibleDeprecatedChecker(BaseChecker): except ValueError: self.add_message('collection-invalid-deprecated-version', node=node, args=(version,)) + @property + def collection_name(self) -> t.Optional[str]: + """Return the collection name, or None if ansible-core is being tested.""" + return self.config.collection_name + + @property + def collection_version(self) -> t.Optional[SemanticVersion]: + """Return the collection version, or None if ansible-core is being tested.""" + return SemanticVersion(self.config.collection_version) if self.config.collection_version is not None else None + @check_messages(*(MSGS.keys())) def visit_call(self, node): """Visit a call node.""" diff --git a/test/lib/ansible_test/_util/controller/sanity/validate-modules/validate_modules/main.py b/test/lib/ansible_test/_util/controller/sanity/validate-modules/validate_modules/main.py index dde9744d548..671b9b1e3f7 100644 --- a/test/lib/ansible_test/_util/controller/sanity/validate-modules/validate_modules/main.py +++ b/test/lib/ansible_test/_util/controller/sanity/validate-modules/validate_modules/main.py @@ -679,14 +679,14 @@ class ModuleValidator(Validator): def _ensure_imports_below_docs(self, doc_info, first_callable): try: min_doc_line = min( - [doc_info[key]['lineno'] for key in doc_info if doc_info[key]['lineno']] + doc_info[key]['lineno'] for key in doc_info if doc_info[key]['lineno'] ) except ValueError: # We can't perform this validation, as there are no DOCs provided at all return max_doc_line = max( - [doc_info[key]['end_lineno'] for key in doc_info if doc_info[key]['end_lineno']] + doc_info[key]['end_lineno'] for key in doc_info if doc_info[key]['end_lineno'] ) import_lines = [] diff --git a/test/lib/ansible_test/_util/target/sanity/compile/compile.py b/test/lib/ansible_test/_util/target/sanity/compile/compile.py index 7890a9b2aae..bd2446f6d8f 100644 --- a/test/lib/ansible_test/_util/target/sanity/compile/compile.py +++ b/test/lib/ansible_test/_util/target/sanity/compile/compile.py @@ -12,28 +12,33 @@ Text = type(u'') def main(): """Main program entry point.""" for path in sys.argv[1:] or sys.stdin.read().splitlines(): - with open(path, 'rb') as source_fd: - source = source_fd.read() + compile_source(path) - try: - compile(source, path, 'exec', dont_inherit=True) - except SyntaxError as ex: - extype, message, lineno, offset = type(ex), ex.text, ex.lineno, ex.offset - except BaseException as ex: # pylint: disable=broad-except - extype, message, lineno, offset = type(ex), str(ex), 0, 0 - else: - continue - # In some situations offset can be None. This can happen for syntax errors on Python 2.6 - # (__future__ import following after a regular import). - offset = offset or 0 +def compile_source(path): + """Compile the specified source file, printing an error if one occurs.""" + with open(path, 'rb') as source_fd: + source = source_fd.read() - result = "%s:%d:%d: %s: %s" % (path, lineno, offset, extype.__name__, safe_message(message)) + try: + compile(source, path, 'exec', dont_inherit=True) + except SyntaxError as ex: + extype, message, lineno, offset = type(ex), ex.text, ex.lineno, ex.offset + except BaseException as ex: # pylint: disable=broad-except + extype, message, lineno, offset = type(ex), str(ex), 0, 0 + else: + return - if sys.version_info <= (3,): - result = result.encode(ENCODING, ERRORS) + # In some situations offset can be None. This can happen for syntax errors on Python 2.6 + # (__future__ import following after a regular import). + offset = offset or 0 - print(result) + result = "%s:%d:%d: %s: %s" % (path, lineno, offset, extype.__name__, safe_message(message)) + + if sys.version_info <= (3,): + result = result.encode(ENCODING, ERRORS) + + print(result) def safe_message(value):