summaryrefslogtreecommitdiff
path: root/pylint
diff options
context:
space:
mode:
authorDaniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>2021-10-15 13:49:58 +0200
committerGitHub <noreply@github.com>2021-10-15 13:49:58 +0200
commitb1c4735d2897c55e16c5c14b3926ba4b676df8c7 (patch)
tree43e1f97b4f579016070731d9323efc4abf34e367 /pylint
parent9ae82580ec70058a6a3f70ccdeb97a2b1ed4df3d (diff)
downloadpylint-git-b1c4735d2897c55e16c5c14b3926ba4b676df8c7.tar.gz
Make ``undefined-variable`` flag type annotation without value assignment (#5158)
Closes #5140 Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
Diffstat (limited to 'pylint')
-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.