Skip to content

Commit c09730b

Browse files
authored
Pass msg_store and node to FileState (#6558)
1 parent cec1c6a commit c09730b

File tree

5 files changed

+58
-12
lines changed

5 files changed

+58
-12
lines changed

pylint/lint/parallel.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ def check_parallel(
159159
mapreduce_data,
160160
) in pool.imap_unordered(_worker_check_single_file, files):
161161
linter.file_state.base_name = base_name
162+
linter.file_state._is_base_filestate = False
162163
linter.set_current_module(module, file_path)
163164
for msg in messages:
164165
linter.reporter.handle_message(msg)

pylint/lint/pylinter.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,13 @@ def __init__(
279279
self._dynamic_plugins: set[str] = set()
280280
"""Set of loaded plugin names."""
281281

282+
# Attributes related to registering messages and their handling
283+
self.msgs_store = MessageDefinitionStore()
284+
self.msg_status = 0
285+
self._by_id_managed_msgs: list[ManagedMessage] = []
286+
282287
# Attributes related to visiting files
283-
self.file_state = FileState()
288+
self.file_state = FileState("", self.msgs_store, is_base_filestate=True)
284289
self.current_name: str | None = None
285290
self.current_file: str | None = None
286291
self._ignore_file = False
@@ -300,11 +305,6 @@ def __init__(
300305
"""List of message symbols on which pylint should fail, set by --fail-on."""
301306
self._error_mode = False
302307

303-
# Attributes related to registering messages and their handling
304-
self.msgs_store = MessageDefinitionStore()
305-
self.msg_status = 0
306-
self._by_id_managed_msgs: list[ManagedMessage] = []
307-
308308
reporters.ReportsHandlerMixIn.__init__(self)
309309
checkers.BaseChecker.__init__(self, self)
310310
# provided reports
@@ -694,7 +694,7 @@ def _check_file(
694694

695695
self._ignore_file = False
696696

697-
self.file_state = FileState(file.modpath)
697+
self.file_state = FileState(file.modpath, self.msgs_store, ast_node)
698698
# fix the current file (if the source file was not available or
699699
# if it's actually a c extension)
700700
self.current_file = ast_node.file
@@ -969,7 +969,11 @@ def generate_reports(self) -> int | None:
969969
# Display whatever messages are left on the reporter.
970970
self.reporter.display_messages(report_nodes.Section())
971971

972-
if self.file_state.base_name is not None:
972+
# TODO: 3.0: Remove second half of if-statement
973+
if (
974+
not self.file_state._is_base_filestate
975+
and self.file_state.base_name is not None
976+
):
973977
# load previous results if any
974978
previous_stats = load_results(self.file_state.base_name)
975979
self.reporter.on_close(self.stats, previous_stats)
@@ -994,7 +998,8 @@ def _report_evaluation(self) -> int | None:
994998
# check with at least check 1 statements (usually 0 when there is a
995999
# syntax error preventing pylint from further processing)
9961000
note = None
997-
assert self.file_state.base_name
1001+
# TODO: 3.0: Remove assertion
1002+
assert self.file_state.base_name is not None
9981003
previous_stats = load_results(self.file_state.base_name)
9991004
if self.stats.statement == 0:
10001005
return note

pylint/utils/file_state.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import collections
88
import sys
9+
import warnings
910
from collections import defaultdict
1011
from collections.abc import Iterator
1112
from typing import TYPE_CHECKING, Dict
@@ -33,15 +34,41 @@
3334
class FileState:
3435
"""Hold internal state specific to the currently analyzed file."""
3536

36-
def __init__(self, modname: str | None = None) -> None:
37+
def __init__(
38+
self,
39+
modname: str | None = None,
40+
msg_store: MessageDefinitionStore | None = None,
41+
node: nodes.Module | None = None,
42+
*,
43+
is_base_filestate: bool = False,
44+
) -> None:
45+
if modname is None:
46+
warnings.warn(
47+
"FileState needs a string as modname argument. "
48+
"This argument will be required in pylint 3.0",
49+
DeprecationWarning,
50+
)
51+
if msg_store is None:
52+
warnings.warn(
53+
"FileState needs a 'MessageDefinitionStore' as msg_store argument. "
54+
"This argument will be required in pylint 3.0",
55+
DeprecationWarning,
56+
)
3757
self.base_name = modname
3858
self._module_msgs_state: MessageStateDict = {}
3959
self._raw_module_msgs_state: MessageStateDict = {}
4060
self._ignored_msgs: defaultdict[
4161
tuple[str, int], set[int]
4262
] = collections.defaultdict(set)
4363
self._suppression_mapping: dict[tuple[str, int], int] = {}
44-
self._effective_max_line_number: int | None = None
64+
self._module = node
65+
if node:
66+
self._effective_max_line_number = node.tolineno
67+
else:
68+
self._effective_max_line_number = None
69+
self._msgs_store = msg_store
70+
self._is_base_filestate = is_base_filestate
71+
"""If this FileState is the base state made during initialization of PyLinter."""
4572

4673
def collect_block_lines(
4774
self, msgs_store: MessageDefinitionStore, module_node: nodes.Module

tests/lint/unittest_lint.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ def reporter():
191191
def initialized_linter(linter: PyLinter) -> PyLinter:
192192
linter.open()
193193
linter.set_current_module("toto", "mydir/toto")
194-
linter.file_state = FileState("toto")
194+
linter.file_state = FileState("toto", linter.msgs_store)
195195
return linter
196196

197197

tests/test_deprecation.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222
ITokenChecker,
2323
)
2424
from pylint.lint import PyLinter
25+
from pylint.message import MessageDefinitionStore
2526
from pylint.reporters import BaseReporter
2627
from pylint.reporters.ureports.nodes import Section
28+
from pylint.utils import FileState
2729

2830

2931
def test_mapreducemixin() -> None:
@@ -87,3 +89,14 @@ def test_load_and_save_results() -> None:
8789
save_results(object(), "") # type: ignore[arg-type]
8890
with pytest.warns(DeprecationWarning):
8991
load_results("")
92+
93+
94+
def test_filestate() -> None:
95+
"""Test that FileState needs its arguments."""
96+
with pytest.warns(DeprecationWarning):
97+
FileState()
98+
with pytest.warns(DeprecationWarning):
99+
FileState("foo")
100+
with pytest.warns(DeprecationWarning):
101+
FileState(msg_store=MessageDefinitionStore())
102+
FileState("foo", MessageDefinitionStore())

0 commit comments

Comments
 (0)