Skip to content

Commit 3dd9d9a

Browse files
rtobarJulienPalard
authored andcommitted
Communicate error objects, not strings
In order to improve error reporting we first need to communicate typed error objects from the checking functions instead of error messages, where we can't (easily and reliably) recover the error details anymore. Error objects know how to format themselves, so we print them directly, and let them do their internal formatting. Signed-off-by: Rodrigo Tobar <[email protected]>
1 parent dc415b9 commit 3dd9d9a

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

sphinxlint/__main__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ def print_results(results):
122122
"""Print results (or a message if nothing is to be printed)."""
123123
qty = 0
124124
for result in results:
125-
for line in result:
126-
print(line)
125+
for error in result:
126+
print(error)
127127
qty += 1
128128
if qty == 0:
129129
print("No problems found.")

sphinxlint/sphinxlint.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
from collections import Counter
2+
from dataclasses import dataclass
23
from os.path import splitext
34

45
from sphinxlint.utils import hide_non_rst_blocks, po2rst
56

67

8+
@dataclass(frozen=True)
9+
class LintError:
10+
"""A linting error found by one of the checkers"""
11+
12+
filename: str
13+
line_no: int
14+
msg: str
15+
checker_name: str
16+
17+
def __str__(self):
18+
return f"{self.filename}:{self.line_no}: {self.msg} ({self.checker_name})"
19+
20+
721
class CheckersOptions:
822
"""Configuration options for checkers."""
923

@@ -31,7 +45,7 @@ def check_text(filename, text, checkers, options=None):
3145
for lno, msg in check(
3246
filename, lines_with_rst_only if check.rst_only else lines, options
3347
):
34-
errors.append(f"{filename}:{lno}: {msg} ({check.name})")
48+
errors.append(LintError(filename, lno, msg, check.name))
3549
return errors
3650

3751

0 commit comments

Comments
 (0)