Skip to content

Commit b404520

Browse files
committed
Revise filtering logic
1 parent 95337f3 commit b404520

File tree

1 file changed

+30
-34
lines changed

1 file changed

+30
-34
lines changed

pylint/checkers/variables.py

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,17 @@ def get_next_to_consume(self, node: nodes.Name) -> list[nodes.NodeNG] | None:
622622
uncertain_nodes_set = set(uncertain_nodes)
623623
found_nodes = [n for n in found_nodes if n not in uncertain_nodes_set]
624624

625+
if found_nodes:
626+
type_checking_nodes = [n for n in found_nodes if in_type_checking_block(n)]
627+
uncertain_nodes_set = set(type_checking_nodes)
628+
# Add type_checking nodes to the list of uncertains if they are not already present
629+
self.consumed_uncertain[node.name] += [
630+
n
631+
for n in type_checking_nodes
632+
if n not in self.consumed_uncertain[node.name]
633+
]
634+
found_nodes = [n for n in found_nodes if n not in uncertain_nodes_set]
635+
625636
return found_nodes
626637

627638
def _inferred_to_define_name_raise_or_return(
@@ -721,7 +732,9 @@ def _uncertain_nodes_if_tests(
721732
for other_node in found_nodes:
722733
if isinstance(other_node, nodes.AssignName):
723734
name = other_node.name
724-
elif isinstance(other_node, (nodes.Import, nodes.ImportFrom)):
735+
elif isinstance(
736+
other_node, (nodes.Import, nodes.ImportFrom)
737+
) and not in_type_checking_block(other_node):
725738
name = node.name
726739
else:
727740
continue
@@ -1763,13 +1776,15 @@ def _check_consumer(
17631776
if found_nodes is None:
17641777
return (VariableVisitConsumerAction.CONTINUE, None)
17651778
if not found_nodes:
1766-
is_reported = self._report_unfound_name_definition(node, current_consumer)
1779+
self._report_unfound_name_definition(node, current_consumer)
17671780
# Mark for consumption any nodes added to consumed_uncertain by
17681781
# get_next_to_consume() because they might not have executed.
1769-
nodes_to_consume = current_consumer.consumed_uncertain[node.name]
1770-
nodes_to_consume = self._filter_type_checking_import_from_consumption(
1771-
node, nodes_to_consume, is_reported
1772-
)
1782+
nodes_to_consume = [
1783+
n
1784+
for n in current_consumer.consumed_uncertain[node.name]
1785+
if not in_type_checking_block(n)
1786+
]
1787+
17731788
return (
17741789
VariableVisitConsumerAction.RETURN,
17751790
nodes_to_consume,
@@ -1942,24 +1957,24 @@ def _report_unfound_name_definition(
19421957
self,
19431958
node: nodes.NodeNG,
19441959
current_consumer: NamesConsumer,
1945-
) -> bool:
1960+
) -> None:
19461961
"""Reports used-before-assignment when all name definition nodes
19471962
get filtered out by NamesConsumer.
19481963
"""
19491964
if (
19501965
self._postponed_evaluation_enabled
19511966
and utils.is_node_in_type_annotation_context(node)
19521967
):
1953-
return False
1968+
return
19541969
if self._is_builtin(node.name):
1955-
return False
1970+
return
19561971
if self._is_variable_annotation_in_function(node):
1957-
return False
1972+
return
19581973
if (
19591974
node.name in self._evaluated_type_checking_scopes
19601975
and node.scope() in self._evaluated_type_checking_scopes[node.name]
19611976
):
1962-
return False
1977+
return
19631978

19641979
confidence = HIGH
19651980
if node.name in current_consumer.names_under_always_false_test:
@@ -1979,34 +1994,15 @@ def _report_unfound_name_definition(
19791994
confidence=confidence,
19801995
)
19811996

1982-
return True
1983-
1984-
def _filter_type_checking_import_from_consumption(
1985-
self,
1986-
node: nodes.NodeNG,
1987-
nodes_to_consume: list[nodes.NodeNG],
1988-
is_reported: bool,
1989-
) -> list[nodes.NodeNG]:
1990-
"""Do not consume type-checking import node as used-before-assignment
1991-
may invoke in different scopes.
1992-
"""
1993-
type_checking_import = next(
1994-
(
1995-
n
1996-
for n in nodes_to_consume
1997-
if isinstance(n, (nodes.Import, nodes.ImportFrom))
1998-
and in_type_checking_block(n)
1999-
),
2000-
None,
2001-
)
20021997
# If used-before-assignment reported for usage of type checking import
20031998
# keep track of its scope
2004-
if type_checking_import and is_reported:
1999+
if node.name in current_consumer.consumed_uncertain and any(
2000+
in_type_checking_block(n)
2001+
for n in current_consumer.consumed_uncertain[node.name]
2002+
):
20052003
self._evaluated_type_checking_scopes.setdefault(node.name, []).append(
20062004
node.scope()
20072005
)
2008-
nodes_to_consume = [n for n in nodes_to_consume if n != type_checking_import]
2009-
return nodes_to_consume
20102006

20112007
@utils.only_required_for_messages("no-name-in-module")
20122008
def visit_import(self, node: nodes.Import) -> None:

0 commit comments

Comments
 (0)