summaryrefslogtreecommitdiffstats
path: root/src/ansiblelint/errors.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-26 06:24:57 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-26 06:24:57 +0000
commit1faea9a6c75f33109e8f66b57b432fdad57b3f46 (patch)
tree4184ce38ac0cf9d5a46bbbae03c87be82927f12b /src/ansiblelint/errors.py
parentAdding upstream version 6.17.2. (diff)
downloadansible-lint-1faea9a6c75f33109e8f66b57b432fdad57b3f46.tar.xz
ansible-lint-1faea9a6c75f33109e8f66b57b432fdad57b3f46.zip
Adding upstream version 24.6.1.upstream/24.6.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/ansiblelint/errors.py')
-rw-r--r--src/ansiblelint/errors.py40
1 files changed, 23 insertions, 17 deletions
diff --git a/src/ansiblelint/errors.py b/src/ansiblelint/errors.py
index c8458b8..5ee2d6f 100644
--- a/src/ansiblelint/errors.py
+++ b/src/ansiblelint/errors.py
@@ -1,4 +1,5 @@
"""Exceptions and error representations."""
+
from __future__ import annotations
import functools
@@ -6,7 +7,6 @@ from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Any
from ansiblelint._internal.rules import BaseRule, RuntimeErrorRule
-from ansiblelint.config import options
from ansiblelint.file_utils import Lintable
if TYPE_CHECKING:
@@ -27,15 +27,9 @@ class WarnSource:
message: str | None = None
-class StrictModeError(RuntimeError):
- """Raise when we encounter a warning in strict mode."""
-
- def __init__(
- self,
- message: str = "Warning treated as error due to --strict option.",
- ):
- """Initialize a StrictModeError instance."""
- super().__init__(message)
+@dataclass(frozen=True)
+class RuleMatchTransformMeta:
+ """Additional metadata about a match error to be used during transformation."""
# pylint: disable=too-many-instance-attributes
@@ -54,7 +48,6 @@ class MatchError(ValueError):
# order matters for these:
message: str = field(init=True, repr=False, default="")
lintable: Lintable = field(init=True, repr=False, default=Lintable(name=""))
- filename: str = field(init=True, repr=False, default="")
tag: str = field(init=True, repr=False, default="")
lineno: int = 1
@@ -65,13 +58,11 @@ class MatchError(ValueError):
rule: BaseRule = field(hash=False, default=RuntimeErrorRule())
ignored: bool = False
fixed: bool = False # True when a transform has resolved this MatchError
+ transform_meta: RuleMatchTransformMeta | None = None
def __post_init__(self) -> None:
"""Can be use by rules that can report multiple errors type, so we can still filter by them."""
- if not self.lintable and self.filename:
- self.lintable = Lintable(self.filename)
- elif self.lintable and not self.filename:
- self.filename = self.lintable.name
+ self.filename = self.lintable.name
# We want to catch accidental MatchError() which contains no useful
# information. When no arguments are passed, the '_message' field is
@@ -104,11 +95,22 @@ class MatchError(ValueError):
msg = "MatchError called incorrectly as column numbers start with 1"
raise RuntimeError(msg)
+ self.lineno += self.lintable.line_offset
+
+ # We make the lintable aware that we found a match inside it, as this
+ # can be used to skip running other rules that do require current one
+ # to pass.
+ self.lintable.matches.append(self)
+
@functools.cached_property
def level(self) -> str:
"""Return the level of the rule: error, warning or notice."""
- if not self.ignored and {self.tag, self.rule.id, *self.rule.tags}.isdisjoint(
- options.warn_list,
+ if (
+ not self.ignored
+ and self.rule.options
+ and {self.tag, self.rule.id, *self.rule.tags}.isdisjoint(
+ self.rule.options.warn_list,
+ )
):
return "error"
return "warning"
@@ -128,6 +130,10 @@ class MatchError(ValueError):
self.details,
)
+ def __str__(self) -> str:
+ """Return a MatchError instance string representation."""
+ return self.__repr__()
+
@property
def position(self) -> str:
"""Return error positioning, with column number if available."""