88import sys
99from dataclasses import dataclass
1010from pathlib import Path
11- from typing import TYPE_CHECKING , Any , NamedTuple
11+ from typing import TYPE_CHECKING , NamedTuple
1212
1313import black
1414import jinja2
1515from ansible .errors import AnsibleError , AnsibleFilterError , AnsibleParserError
1616from ansible .parsing .yaml .objects import AnsibleUnicode
1717from jinja2 .exceptions import TemplateSyntaxError
1818
19- from ansiblelint .constants import LINE_NUMBER_KEY
2019from ansiblelint .errors import RuleMatchTransformMeta
2120from ansiblelint .file_utils import Lintable
2221from ansiblelint .rules import AnsibleLintRule , TransformMixin
@@ -195,7 +194,7 @@ def matchtask(
195194 result .append (
196195 self .create_matcherror (
197196 message = str (exc ),
198- lineno = _get_error_line ( task , path ),
197+ lineno = task . get_error_line ( path ),
199198 filename = file ,
200199 tag = f"{ self .id } [invalid]" ,
201200 ),
@@ -214,7 +213,7 @@ def matchtask(
214213 value = v ,
215214 reformatted = reformatted ,
216215 ),
217- lineno = _get_error_line ( task , path ),
216+ lineno = task . get_error_line ( path ),
218217 details = details ,
219218 filename = file ,
220219 tag = f"{ self .id } [{ tag } ]" ,
@@ -233,12 +232,13 @@ def matchtask(
233232
234233 def matchyaml (self , file : Lintable ) -> list [MatchError ]:
235234 """Return matches for variables defined in vars files."""
236- data : dict [str , Any ] = {}
237235 raw_results : list [MatchError ] = []
238236 results : list [MatchError ] = []
239237
240238 if str (file .kind ) == "vars" :
241239 data = parse_yaml_from_file (str (file .path ))
240+ if not isinstance (data , dict ):
241+ return results
242242 for key , v , _path in nested_items_path (data ):
243243 if isinstance (v , AnsibleUnicode ):
244244 reformatted , details , tag = self .check_whitespace (
@@ -406,7 +406,7 @@ def uncook(value: str, *, implicit: bool = False) -> str:
406406 except jinja2 .exceptions .TemplateSyntaxError as exc :
407407 return "" , str (exc .message ), "invalid"
408408 # pylint: disable=c-extension-no-member
409- except (NotImplementedError , black . parsing . InvalidInput ) as exc :
409+ except (NotImplementedError , ValueError ) as exc :
410410 # black is not able to recognize all valid jinja2 templates, so we
411411 # just ignore InvalidInput errors.
412412 # NotImplementedError is raised internally for expressions with
@@ -898,17 +898,3 @@ def _do_template(*args, **kwargs): # type: ignore[no-untyped-def] # Templar.do_
898898 with mock .patch .object (Templar , "do_template" , _do_template ):
899899 results = Runner (lintable , rules = collection ).run ()
900900 assert len (results ) == 0
901-
902-
903- def _get_error_line (task : dict [str , Any ], path : list [str | int ]) -> int :
904- """Return error line number."""
905- line = task [LINE_NUMBER_KEY ]
906- ctx = task
907- for _ in path :
908- ctx = ctx [_ ]
909- if LINE_NUMBER_KEY in ctx :
910- line = ctx [LINE_NUMBER_KEY ]
911- if not isinstance (line , int ):
912- msg = "Line number is not an integer"
913- raise TypeError (msg )
914- return line
0 commit comments