summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaudiu Popa <pcmanticore@gmail.com>2015-12-08 11:45:23 +0200
committerClaudiu Popa <pcmanticore@gmail.com>2015-12-08 11:45:23 +0200
commit3ca41ff239dc11404af65ca4a3060b92d87668ba (patch)
tree942cd868b61148d8575396f9d08e670f117cfb1a
parent403df92d1710267ae5b7fe215acd9357a8cbf251 (diff)
downloadpylint-3ca41ff239dc11404af65ca4a3060b92d87668ba.tar.gz
Use the new .ctx attribute for subscripts for detecting in which context the node was used. Closes issue #705.
-rw-r--r--pylint/checkers/typecheck.py34
1 files changed, 9 insertions, 25 deletions
diff --git a/pylint/checkers/typecheck.py b/pylint/checkers/typecheck.py
index 076f21a..1c158ab 100644
--- a/pylint/checkers/typecheck.py
+++ b/pylint/checkers/typecheck.py
@@ -51,7 +51,6 @@ _ZOPE_DEPRECATED = (
)
BUILTINS = six.moves.builtins.__name__
STR_FORMAT = "%s.str.format" % BUILTINS
-STORE_CONTEXT, LOAD_CONTEXT, DELETE_CONTEXT = range(3)
def _unflatten(iterable):
@@ -651,9 +650,9 @@ accessed. Python regular expressions are accepted.'}
# The parent of this node will be a Subscript, and the parent of that
# node determines if the Subscript is a get, set, or delete operation.
operation = node.parent.parent
- if isinstance(operation, astroid.Assign):
+ if node.parent.ctx is astroid.Store:
methodname = '__setitem__'
- elif isinstance(operation, astroid.Delete):
+ elif node.parent.ctx is astroid.Del:
methodname = '__delitem__'
else:
methodname = '__getitem__'
@@ -836,29 +835,21 @@ accessed. Python regular expressions are accepted.'}
if operator in ['in', 'not in']:
self._check_membership_test(right)
- @staticmethod
- def _subscript_context(node):
- statement = node.statement()
- if isinstance(statement, astroid.Assign):
- for target in statement.targets:
- if target is node or target.parent_of(node):
- return STORE_CONTEXT
- elif isinstance(statement, astroid.Delete):
- return DELETE_CONTEXT
- return LOAD_CONTEXT
-
@check_messages('unsubscriptable-object', 'unsupported-assignment-operation',
'unsupported-delete-operation')
def visit_subscript(self, node):
+ supported_protocol = None
if isinstance(node.value, (astroid.ListComp, astroid.DictComp)):
return
- context = self._subscript_context(node)
- if context == LOAD_CONTEXT:
+ if node.ctx == astroid.Load:
+ supported_protocol = supports_getitem
msg = 'unsubscriptable-object'
- elif context == STORE_CONTEXT:
+ elif node.ctx == astroid.Store:
+ supported_protocol = supports_setitem
msg = 'unsupported-assignment-operation'
- elif context == DELETE_CONTEXT:
+ elif node.ctx == astroid.Del:
+ supported_protocol = supports_delitem
msg = 'unsupported-delete-operation'
if isinstance(node.value, astroid.SetComp):
@@ -873,13 +864,6 @@ accessed. Python regular expressions are accepted.'}
if inferred is None or inferred is astroid.YES:
return
- supported_protocol = None
- if context == STORE_CONTEXT:
- supported_protocol = supports_setitem
- elif context == LOAD_CONTEXT:
- supported_protocol = supports_getitem
- elif context == DELETE_CONTEXT:
- supported_protocol = supports_delitem
if not supported_protocol(inferred):
self.add_message(msg, args=node.value.as_string(), node=node.value)