summaryrefslogtreecommitdiff
path: root/pylint/checkers/variables.py
diff options
context:
space:
mode:
Diffstat (limited to 'pylint/checkers/variables.py')
-rw-r--r--pylint/checkers/variables.py18
1 files changed, 18 insertions, 0 deletions
diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py
index b4027d602..44044f76b 100644
--- a/pylint/checkers/variables.py
+++ b/pylint/checkers/variables.py
@@ -1186,6 +1186,8 @@ class VariablesChecker(BaseChecker):
)
elif current_consumer.scope_type == "lambda":
self.add_message("undefined-variable", node=node, args=name)
+ elif self._is_only_type_assignment(node, name, defstmt):
+ self.add_message("undefined-variable", node=node, args=name)
current_consumer.mark_as_consumed(name, found_nodes)
# check it's not a loop variable used outside the loop
@@ -1548,6 +1550,22 @@ class VariablesChecker(BaseChecker):
return maybee0601, annotation_return, use_outer_definition
+ @staticmethod
+ def _is_only_type_assignment(
+ node: nodes.Name, name: str, defstmt: nodes.Statement
+ ) -> bool:
+ """Check if variable only gets assigned a type and never a value"""
+ if not isinstance(defstmt, nodes.AnnAssign) or defstmt.value:
+ return False
+ for ref_node in node.scope().locals[name][1:]:
+ if ref_node.lineno < node.lineno:
+ if not (
+ isinstance(ref_node.parent, nodes.AnnAssign)
+ and ref_node.parent.value
+ ):
+ return False
+ return True
+
def _ignore_class_scope(self, node):
"""
Return True if the node is in a local class scope, as an assignment.