From f03ffbd863b35034207fcb1a1dd385e475d15cb6 Mon Sep 17 00:00:00 2001 From: Conner Crosby Date: Fri, 15 Nov 2024 18:49:56 -0500 Subject: [PATCH] Utilize create_matcherror more in other rules The imports of the MatchError class have been moved under the TYPE_CHECKING conditional because the rules changed now only rely solely on the class for typing, and no longer require it at runtime. --- src/ansiblelint/rules/schema.py | 17 ++++++------- src/ansiblelint/rules/var_naming.py | 37 ++++++++++++----------------- 2 files changed, 22 insertions(+), 32 deletions(-) diff --git a/src/ansiblelint/rules/schema.py b/src/ansiblelint/rules/schema.py index 10f1f5a77d..329510e920 100644 --- a/src/ansiblelint/rules/schema.py +++ b/src/ansiblelint/rules/schema.py @@ -7,7 +7,6 @@ import sys from typing import TYPE_CHECKING, Any -from ansiblelint.errors import MatchError from ansiblelint.file_utils import Lintable from ansiblelint.rules import AnsibleLintRule from ansiblelint.schemas.__main__ import JSON_SCHEMAS @@ -16,6 +15,7 @@ if TYPE_CHECKING: from ansiblelint.config import Options + from ansiblelint.errors import MatchError from ansiblelint.utils import Task @@ -120,11 +120,10 @@ def _get_field_matches( if not has_jinja(plugin_value) and plugin_value not in values: msg = f"'{key}' must be one of the currently available values: {', '.join(values)}" results.append( - MatchError( + self.create_matcherror( message=msg, lineno=data.get("__line__", 1), - lintable=file, - rule=self, + filename=file, details=ValidateSchemaRule.description, tag=f"schema[{file.kind}]", ), @@ -149,10 +148,9 @@ def matchtask( msg = pre_checks["task"][key]["msg"] tag = pre_checks["task"][key]["tag"] results.append( - MatchError( + self.create_matcherror( message=msg, - lintable=file, - rule=self, + filename=file, details=ValidateSchemaRule.description, tag=f"schema[{tag}]", ), @@ -178,10 +176,9 @@ def matchyaml(self, file: Lintable) -> list[MatchError]: return [] result.append( - MatchError( + self.create_matcherror( message=error, - lintable=file, - rule=self, + filename=file, details=ValidateSchemaRule.description, tag=f"schema[{file.kind}]", ), diff --git a/src/ansiblelint/rules/var_naming.py b/src/ansiblelint/rules/var_naming.py index 5adcae9065..f2f4d0eed5 100644 --- a/src/ansiblelint/rules/var_naming.py +++ b/src/ansiblelint/rules/var_naming.py @@ -17,7 +17,6 @@ PLAYBOOK_ROLE_KEYWORDS, RC, ) -from ansiblelint.errors import MatchError from ansiblelint.file_utils import Lintable from ansiblelint.rules import AnsibleLintRule, RulesCollection from ansiblelint.runner import Runner @@ -26,6 +25,7 @@ from ansiblelint.utils import parse_yaml_from_file if TYPE_CHECKING: + from ansiblelint.errors import MatchError from ansiblelint.utils import Task @@ -121,11 +121,10 @@ def get_var_naming_matcherror( ) -> MatchError | None: """Return a MatchError if the variable name is not valid, otherwise None.""" if not isinstance(ident, str): # pragma: no cover - return MatchError( + return self.create_matcherror( tag="var-naming[non-string]", message="Variables names must be strings.", - rule=self, - lintable=file, + filename=file, ) if ident in ANNOTATION_KEYS or ident in self.allowed_special_names: @@ -134,35 +133,31 @@ def get_var_naming_matcherror( try: ident.encode("ascii") except UnicodeEncodeError: - return MatchError( + return self.create_matcherror( tag="var-naming[non-ascii]", message=f"Variables names must be ASCII. ({ident})", - rule=self, - lintable=file, + filename=file, ) if keyword.iskeyword(ident): - return MatchError( + return self.create_matcherror( tag="var-naming[no-keyword]", message=f"Variables names must not be Python keywords. ({ident})", - rule=self, - lintable=file, + filename=file, ) if ident in self.reserved_names: - return MatchError( + return self.create_matcherror( tag="var-naming[no-reserved]", message=f"Variables names must not be Ansible reserved names. ({ident})", - rule=self, - lintable=file, + filename=file, ) if ident in self.read_only_names: - return MatchError( + return self.create_matcherror( tag="var-naming[read-only]", message=f"This special variable is read-only. ({ident})", - rule=self, - lintable=file, + filename=file, ) # We want to allow use of jinja2 templating for variable names @@ -172,11 +167,10 @@ def get_var_naming_matcherror( if not bool(self.re_pattern.match(ident)) and ( not prefix or not prefix.from_fqcn ): - return MatchError( + return self.create_matcherror( tag="var-naming[pattern]", message=f"Variables names should match {self.re_pattern_str} regex. ({ident})", - rule=self, - lintable=file, + filename=file, ) if ( @@ -185,11 +179,10 @@ def get_var_naming_matcherror( and not has_jinja(prefix.value) and is_fqcn_or_name(prefix.value) ): - return MatchError( + return self.create_matcherror( tag="var-naming[no-role-prefix]", message=f"Variables names from within roles should use {prefix.value}_ as a prefix.", - rule=self, - lintable=file, + filename=file, ) return None