Skip to content

Refactor _collect_block_lines #6560

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 40 additions & 31 deletions pylint/utils/file_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@ def _collect_block_lines(
"""
for child in node.get_children():
self._collect_block_lines(msgs_store, child, msg_state)
first = node.fromlineno
last = node.tolineno
# first child line number used to distinguish between disable
# which are the first child of scoped node with those defined later.
# For instance in the code below:
Expand All @@ -116,37 +114,48 @@ def _collect_block_lines(
):
firstchildlineno = node.body[0].fromlineno
else:
firstchildlineno = last
firstchildlineno = node.tolineno
for msgid, lines in msg_state.items():
for lineno, state in list(lines.items()):
original_lineno = lineno
if first > lineno or last < lineno:
for msg in msgs_store.get_message_definitions(msgid):
self._set_message_state_in_block(msg, lines, node, firstchildlineno)

def _set_message_state_in_block(
self,
msg: MessageDefinition,
lines: dict[int, bool],
node: nodes.NodeNG,
firstchildlineno: int,
) -> None:
"""Set the state of a message in a block of lines."""
first = node.fromlineno
last = node.tolineno
for lineno, state in list(lines.items()):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the call to list necessary ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be a nice lint warning πŸ˜„

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, it necessary...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looked like an artifact from a 2to3 migration, my bad :)

original_lineno = lineno
if first > lineno or last < lineno:
continue
# Set state for all lines for this block, if the
# warning is applied to nodes.
if msg.scope == WarningScope.NODE:
if lineno > firstchildlineno:
state = True
first_, last_ = node.block_range(lineno)
else:
first_ = lineno
last_ = last
for line in range(first_, last_ + 1):
# do not override existing entries
if line in self._module_msgs_state.get(msg.msgid, ()):
continue
# Set state for all lines for this block, if the
# warning is applied to nodes.
message_definitions = msgs_store.get_message_definitions(msgid)
for message_definition in message_definitions:
if message_definition.scope == WarningScope.NODE:
if lineno > firstchildlineno:
state = True
first_, last_ = node.block_range(lineno)
else:
first_ = lineno
last_ = last
for line in range(first_, last_ + 1):
# do not override existing entries
if line in self._module_msgs_state.get(msgid, ()):
continue
if line in lines: # state change in the same block
state = lines[line]
original_lineno = line
if not state:
self._suppression_mapping[(msgid, line)] = original_lineno
try:
self._module_msgs_state[msgid][line] = state
except KeyError:
self._module_msgs_state[msgid] = {line: state}
del lines[lineno]
if line in lines: # state change in the same block
state = lines[line]
original_lineno = line
if not state:
self._suppression_mapping[(msg.msgid, line)] = original_lineno
try:
self._module_msgs_state[msg.msgid][line] = state
except KeyError:
self._module_msgs_state[msg.msgid] = {line: state}
del lines[lineno]

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