Skip to content

Commit cd3e51f

Browse files
Add optional module and filepath params to add_message
Refs #10880 — allows checkers to override the reported module name and file path in message location, instead of relying on the current file context or node. Co-Authored-By: Claude Opus 4.6 <[email protected]>
1 parent 80c2e8e commit cd3e51f

4 files changed

Lines changed: 37 additions & 4 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
``add_message`` now accepts optional ``module`` and ``filepath`` keyword arguments to override the reported message location.
2+
3+
Refs #10880

pylint/checkers/base_checker.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ def get_full_documentation(
139139
result += "\n"
140140
return result
141141

142+
# pylint: disable-next=too-many-arguments
142143
def add_message(
143144
self,
144145
msgid: str,
@@ -149,9 +150,20 @@ def add_message(
149150
col_offset: int | None = None,
150151
end_lineno: int | None = None,
151152
end_col_offset: int | None = None,
153+
module: str | None = None,
154+
filepath: str | None = None,
152155
) -> None:
153156
self.linter.add_message(
154-
msgid, line, node, args, confidence, col_offset, end_lineno, end_col_offset
157+
msgid,
158+
line=line,
159+
node=node,
160+
args=args,
161+
confidence=confidence,
162+
col_offset=col_offset,
163+
end_lineno=end_lineno,
164+
end_col_offset=end_col_offset,
165+
module=module,
166+
filepath=filepath,
155167
)
156168

157169
def check_consistency(self) -> None:

pylint/lint/pylinter.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,6 +1219,7 @@ def _report_evaluation(self, verbose: bool = False) -> int | None:
12191219
self.reporter.display_reports(sect)
12201220
return note
12211221

1222+
# pylint: disable-next=too-many-arguments
12221223
def _add_one_message(
12231224
self,
12241225
message_definition: MessageDefinition,
@@ -1229,6 +1230,8 @@ def _add_one_message(
12291230
col_offset: int | None,
12301231
end_lineno: int | None,
12311232
end_col_offset: int | None,
1233+
module: str | None = None,
1234+
filepath: str | None = None,
12321235
) -> None:
12331236
"""After various checks have passed a single Message is
12341237
passed to the reporter and added to stats.
@@ -1282,11 +1285,15 @@ def _add_one_message(
12821285
msg %= args
12831286
# get module and object
12841287
if node is None:
1285-
module, obj = self.current_name, ""
1288+
_module, obj = self.current_name, ""
12861289
abspath = self.current_file
12871290
else:
1288-
module, obj = utils.get_module_and_frameid(node)
1291+
_module, obj = utils.get_module_and_frameid(node)
12891292
abspath = node.root().file
1293+
if module is not None:
1294+
_module = module
1295+
if filepath is not None:
1296+
abspath = filepath
12901297
if abspath is not None:
12911298
path = abspath.replace(self.reporter.path_strip_prefix, "", 1)
12921299
else:
@@ -1299,7 +1306,7 @@ def _add_one_message(
12991306
MessageLocationTuple(
13001307
abspath or "",
13011308
path,
1302-
module or "",
1309+
_module or "",
13031310
obj,
13041311
line or 1,
13051312
col_offset or 0,
@@ -1311,6 +1318,7 @@ def _add_one_message(
13111318
)
13121319
)
13131320

1321+
# pylint: disable-next=too-many-arguments
13141322
def add_message(
13151323
self,
13161324
msgid: str,
@@ -1321,6 +1329,8 @@ def add_message(
13211329
col_offset: int | None = None,
13221330
end_lineno: int | None = None,
13231331
end_col_offset: int | None = None,
1332+
module: str | None = None,
1333+
filepath: str | None = None,
13241334
) -> None:
13251335
"""Adds a message given by ID or name.
13261336
@@ -1329,6 +1339,9 @@ def add_message(
13291339
AST checkers must provide the node argument (but may optionally
13301340
provide line if the line number is different), raw and token checkers
13311341
must provide the line argument.
1342+
1343+
The module and filepath parameters allow overriding the module name
1344+
and file path reported in the message location.
13321345
"""
13331346
if confidence is None:
13341347
confidence = interfaces.UNDEFINED
@@ -1343,6 +1356,8 @@ def add_message(
13431356
col_offset,
13441357
end_lineno,
13451358
end_col_offset,
1359+
module=module,
1360+
filepath=filepath,
13461361
)
13471362

13481363
def add_ignored_message(

pylint/testutils/unittest_linter.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def release_messages(self) -> list[MessageTest]:
2828
finally:
2929
self._messages = []
3030

31+
# pylint: disable-next=too-many-arguments
3132
def add_message(
3233
self,
3334
msgid: str,
@@ -39,6 +40,8 @@ def add_message(
3940
col_offset: int | None = None,
4041
end_lineno: int | None = None,
4142
end_col_offset: int | None = None,
43+
module: str | None = None,
44+
filepath: str | None = None,
4245
) -> None:
4346
"""Add a MessageTest to the _messages attribute of the linter class."""
4447
# If confidence is None we set it to UNDEFINED as well in PyLinter

0 commit comments

Comments
 (0)