Skip to content

Commit 57a0602

Browse files
authored
Add typing to get_next_to_consume and _check_consumer (#5681)
1 parent 383fb55 commit 57a0602

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

pylint/checkers/variables.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ def mark_as_consumed(self, name, consumed_nodes):
619619
else:
620620
del self.to_consume[name]
621621

622-
def get_next_to_consume(self, node):
622+
def get_next_to_consume(self, node: nodes.Name) -> Optional[List[nodes.NodeNG]]:
623623
"""Return a list of the nodes that define `node` from this scope. If it is
624624
uncertain whether a node will be consumed, such as for statements in
625625
except blocks, add it to self.consumed_uncertain instead of returning it.
@@ -1236,18 +1236,14 @@ def _undefined_and_used_before_checker(
12361236
action, found_nodes = self._check_consumer(
12371237
node, stmt, frame, current_consumer, i, base_scope_type
12381238
)
1239-
12401239
if action is VariableVisitConsumerAction.CONTINUE:
12411240
continue
12421241
if action is VariableVisitConsumerAction.CONSUME:
1243-
# pylint: disable-next=fixme
1244-
# TODO: remove assert after _check_consumer return value better typed
1245-
assert found_nodes is not None, "Cannot consume an empty list of nodes."
12461242
# Any nodes added to consumed_uncertain by get_next_to_consume()
12471243
# should be added back so that they are marked as used.
12481244
# They will have already had a chance to emit used-before-assignment.
12491245
# We check here instead of before every single return in _check_consumer()
1250-
found_nodes += current_consumer.consumed_uncertain[node.name]
1246+
found_nodes += current_consumer.consumed_uncertain[node.name] # type: ignore[operator]
12511247
current_consumer.mark_as_consumed(node.name, found_nodes)
12521248
if action in {
12531249
VariableVisitConsumerAction.RETURN,
@@ -1321,7 +1317,16 @@ def _check_consumer(
13211317
current_consumer: NamesConsumer,
13221318
consumer_level: int,
13231319
base_scope_type: Any,
1324-
) -> Tuple[VariableVisitConsumerAction, Optional[Any]]:
1320+
) -> Union[
1321+
Tuple[
1322+
Union[
1323+
Literal[VariableVisitConsumerAction.CONTINUE],
1324+
Literal[VariableVisitConsumerAction.RETURN],
1325+
],
1326+
None,
1327+
],
1328+
Tuple[Literal[VariableVisitConsumerAction.CONSUME], List[nodes.NodeNG]],
1329+
]:
13251330
"""Checks a consumer for conditions that should trigger messages"""
13261331
# If the name has already been consumed, only check it's not a loop
13271332
# variable used outside the loop.
@@ -1354,7 +1359,7 @@ def _check_consumer(
13541359
# return a CONSUME action so that _undefined_and_used_before_checker()
13551360
# will mark them as used
13561361
return (VariableVisitConsumerAction.CONSUME, found_nodes)
1357-
return (VariableVisitConsumerAction.RETURN, found_nodes)
1362+
return (VariableVisitConsumerAction.RETURN, None)
13581363

13591364
self._check_late_binding_closure(node)
13601365

0 commit comments

Comments
 (0)