Skip to content

Commit 54f255a

Browse files
committed
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.
1 parent e62ef52 commit 54f255a

File tree

2 files changed

+22
-32
lines changed

2 files changed

+22
-32
lines changed

src/ansiblelint/rules/schema.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import sys
88
from typing import TYPE_CHECKING, Any
99

10-
from ansiblelint.errors import MatchError
1110
from ansiblelint.file_utils import Lintable
1211
from ansiblelint.rules import AnsibleLintRule
1312
from ansiblelint.schemas.__main__ import JSON_SCHEMAS
@@ -16,6 +15,7 @@
1615

1716
if TYPE_CHECKING:
1817
from ansiblelint.config import Options
18+
from ansiblelint.errors import MatchError
1919
from ansiblelint.utils import Task
2020

2121

@@ -120,11 +120,10 @@ def _get_field_matches(
120120
if not has_jinja(plugin_value) and plugin_value not in values:
121121
msg = f"'{key}' must be one of the currently available values: {', '.join(values)}"
122122
results.append(
123-
MatchError(
123+
self.create_matcherror(
124124
message=msg,
125125
lineno=data.get("__line__", 1),
126-
lintable=file,
127-
rule=self,
126+
filename=file,
128127
details=ValidateSchemaRule.description,
129128
tag=f"schema[{file.kind}]",
130129
),
@@ -149,10 +148,9 @@ def matchtask(
149148
msg = pre_checks["task"][key]["msg"]
150149
tag = pre_checks["task"][key]["tag"]
151150
results.append(
152-
MatchError(
151+
self.create_matcherror(
153152
message=msg,
154-
lintable=file,
155-
rule=self,
153+
filename=file,
156154
details=ValidateSchemaRule.description,
157155
tag=f"schema[{tag}]",
158156
),
@@ -178,10 +176,9 @@ def matchyaml(self, file: Lintable) -> list[MatchError]:
178176
return []
179177

180178
result.append(
181-
MatchError(
179+
self.create_matcherror(
182180
message=error,
183-
lintable=file,
184-
rule=self,
181+
filename=file,
185182
details=ValidateSchemaRule.description,
186183
tag=f"schema[{file.kind}]",
187184
),

src/ansiblelint/rules/var_naming.py

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
PLAYBOOK_ROLE_KEYWORDS,
1818
RC,
1919
)
20-
from ansiblelint.errors import MatchError
2120
from ansiblelint.file_utils import Lintable
2221
from ansiblelint.rules import AnsibleLintRule, RulesCollection
2322
from ansiblelint.runner import Runner
@@ -26,6 +25,7 @@
2625
from ansiblelint.utils import parse_yaml_from_file
2726

2827
if TYPE_CHECKING:
28+
from ansiblelint.errors import MatchError
2929
from ansiblelint.utils import Task
3030

3131

@@ -121,11 +121,10 @@ def get_var_naming_matcherror(
121121
) -> MatchError | None:
122122
"""Return a MatchError if the variable name is not valid, otherwise None."""
123123
if not isinstance(ident, str): # pragma: no cover
124-
return MatchError(
124+
return self.create_matcherror(
125125
tag="var-naming[non-string]",
126126
message="Variables names must be strings.",
127-
rule=self,
128-
lintable=file,
127+
filename=file,
129128
)
130129

131130
if ident in ANNOTATION_KEYS or ident in self.allowed_special_names:
@@ -134,35 +133,31 @@ def get_var_naming_matcherror(
134133
try:
135134
ident.encode("ascii")
136135
except UnicodeEncodeError:
137-
return MatchError(
136+
return self.create_matcherror(
138137
tag="var-naming[non-ascii]",
139138
message=f"Variables names must be ASCII. ({ident})",
140-
rule=self,
141-
lintable=file,
139+
filename=file,
142140
)
143141

144142
if keyword.iskeyword(ident):
145-
return MatchError(
143+
return self.create_matcherror(
146144
tag="var-naming[no-keyword]",
147145
message=f"Variables names must not be Python keywords. ({ident})",
148-
rule=self,
149-
lintable=file,
146+
filename=file,
150147
)
151148

152149
if ident in self.reserved_names:
153-
return MatchError(
150+
return self.create_matcherror(
154151
tag="var-naming[no-reserved]",
155152
message=f"Variables names must not be Ansible reserved names. ({ident})",
156-
rule=self,
157-
lintable=file,
153+
filename=file,
158154
)
159155

160156
if ident in self.read_only_names:
161-
return MatchError(
157+
return self.create_matcherror(
162158
tag="var-naming[read-only]",
163159
message=f"This special variable is read-only. ({ident})",
164-
rule=self,
165-
lintable=file,
160+
filename=file,
166161
)
167162

168163
# We want to allow use of jinja2 templating for variable names
@@ -172,11 +167,10 @@ def get_var_naming_matcherror(
172167
if not bool(self.re_pattern.match(ident)) and (
173168
not prefix or not prefix.from_fqcn
174169
):
175-
return MatchError(
170+
return self.create_matcherror(
176171
tag="var-naming[pattern]",
177172
message=f"Variables names should match {self.re_pattern_str} regex. ({ident})",
178-
rule=self,
179-
lintable=file,
173+
filename=file,
180174
)
181175

182176
if (
@@ -185,11 +179,10 @@ def get_var_naming_matcherror(
185179
and not has_jinja(prefix.value)
186180
and is_fqcn_or_name(prefix.value)
187181
):
188-
return MatchError(
182+
return self.create_matcherror(
189183
tag="var-naming[no-role-prefix]",
190184
message=f"Variables names from within roles should use {prefix.value}_ as a prefix.",
191-
rule=self,
192-
lintable=file,
185+
filename=file,
193186
)
194187
return None
195188

0 commit comments

Comments
 (0)