Skip to content

Commit d3c7632

Browse files
authored
Refactor _collect_block_lines (#6560)
1 parent 2f7580d commit d3c7632

File tree

1 file changed

+40
-31
lines changed

1 file changed

+40
-31
lines changed

pylint/utils/file_state.py

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,6 @@ def _collect_block_lines(
9393
"""
9494
for child in node.get_children():
9595
self._collect_block_lines(msgs_store, child, msg_state)
96-
first = node.fromlineno
97-
last = node.tolineno
9896
# first child line number used to distinguish between disable
9997
# which are the first child of scoped node with those defined later.
10098
# For instance in the code below:
@@ -116,37 +114,48 @@ def _collect_block_lines(
116114
):
117115
firstchildlineno = node.body[0].fromlineno
118116
else:
119-
firstchildlineno = last
117+
firstchildlineno = node.tolineno
120118
for msgid, lines in msg_state.items():
121-
for lineno, state in list(lines.items()):
122-
original_lineno = lineno
123-
if first > lineno or last < lineno:
119+
for msg in msgs_store.get_message_definitions(msgid):
120+
self._set_message_state_in_block(msg, lines, node, firstchildlineno)
121+
122+
def _set_message_state_in_block(
123+
self,
124+
msg: MessageDefinition,
125+
lines: dict[int, bool],
126+
node: nodes.NodeNG,
127+
firstchildlineno: int,
128+
) -> None:
129+
"""Set the state of a message in a block of lines."""
130+
first = node.fromlineno
131+
last = node.tolineno
132+
for lineno, state in list(lines.items()):
133+
original_lineno = lineno
134+
if first > lineno or last < lineno:
135+
continue
136+
# Set state for all lines for this block, if the
137+
# warning is applied to nodes.
138+
if msg.scope == WarningScope.NODE:
139+
if lineno > firstchildlineno:
140+
state = True
141+
first_, last_ = node.block_range(lineno)
142+
else:
143+
first_ = lineno
144+
last_ = last
145+
for line in range(first_, last_ + 1):
146+
# do not override existing entries
147+
if line in self._module_msgs_state.get(msg.msgid, ()):
124148
continue
125-
# Set state for all lines for this block, if the
126-
# warning is applied to nodes.
127-
message_definitions = msgs_store.get_message_definitions(msgid)
128-
for message_definition in message_definitions:
129-
if message_definition.scope == WarningScope.NODE:
130-
if lineno > firstchildlineno:
131-
state = True
132-
first_, last_ = node.block_range(lineno)
133-
else:
134-
first_ = lineno
135-
last_ = last
136-
for line in range(first_, last_ + 1):
137-
# do not override existing entries
138-
if line in self._module_msgs_state.get(msgid, ()):
139-
continue
140-
if line in lines: # state change in the same block
141-
state = lines[line]
142-
original_lineno = line
143-
if not state:
144-
self._suppression_mapping[(msgid, line)] = original_lineno
145-
try:
146-
self._module_msgs_state[msgid][line] = state
147-
except KeyError:
148-
self._module_msgs_state[msgid] = {line: state}
149-
del lines[lineno]
149+
if line in lines: # state change in the same block
150+
state = lines[line]
151+
original_lineno = line
152+
if not state:
153+
self._suppression_mapping[(msg.msgid, line)] = original_lineno
154+
try:
155+
self._module_msgs_state[msg.msgid][line] = state
156+
except KeyError:
157+
self._module_msgs_state[msg.msgid] = {line: state}
158+
del lines[lineno]
150159

151160
def set_msg_status(self, msg: MessageDefinition, line: int, status: bool) -> None:
152161
"""Set status (enabled/disable) for a given message at a given line."""

0 commit comments

Comments
 (0)