@@ -93,8 +93,6 @@ def _collect_block_lines(
93
93
"""
94
94
for child in node .get_children ():
95
95
self ._collect_block_lines (msgs_store , child , msg_state )
96
- first = node .fromlineno
97
- last = node .tolineno
98
96
# first child line number used to distinguish between disable
99
97
# which are the first child of scoped node with those defined later.
100
98
# For instance in the code below:
@@ -116,37 +114,48 @@ def _collect_block_lines(
116
114
):
117
115
firstchildlineno = node .body [0 ].fromlineno
118
116
else :
119
- firstchildlineno = last
117
+ firstchildlineno = node . tolineno
120
118
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 , ()):
124
148
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 ]
150
159
151
160
def set_msg_status (self , msg : MessageDefinition , line : int , status : bool ) -> None :
152
161
"""Set status (enabled/disable) for a given message at a given line."""
0 commit comments