diff options
author | David Shea <dshea@redhat.com> | 2014-07-01 14:19:12 -0400 |
---|---|---|
committer | David Shea <dshea@redhat.com> | 2014-07-01 14:19:12 -0400 |
commit | 12bbddf5f91b219f9153e7ed583f91babd4b6100 (patch) | |
tree | 0ca2087cb33fb0c565f7bd0dff5b75900b270d10 | |
parent | 2512c24d2e129ba6acb73f323048e3adb9b6cdae (diff) | |
download | pylint-12bbddf5f91b219f9153e7ed583f91babd4b6100.tar.gz |
Emit an error when a list index is a non-integer type
-rw-r--r-- | checkers/typecheck.py | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/checkers/typecheck.py b/checkers/typecheck.py index 25f7612..97e20e0 100644 --- a/checkers/typecheck.py +++ b/checkers/typecheck.py @@ -74,6 +74,9 @@ MSGS = { ('Used when a function call does not pass a mandatory' ' keyword-only argument.'), {'minversion': (3, 0)}), + 'E1126': ('List index is non-integer type %s', + 'non-integer-list-index', + 'Used when a list is indexed with a non-integer type'), } def _determine_callable(callable_obj): @@ -445,6 +448,26 @@ accessed. Python regular expressions are accepted.'} if defval is None and not assigned: self.add_message('missing-kwoa', node=node, args=(name, callable_name)) + @check_messages('non-integer-list-index') + def visit_index(self, node): + if not node.parent or not node.parent.value: + return + + # Look for index operations where the parent is a list. + # If the types can be determined, only allow indices to be ints or + # int constants. + + index_type = safe_infer(node) + parent_type = safe_infer(node.parent.value) + + if type(parent_type) == astroid.List and \ + not(index_type == astroid.YES or \ + (type(index_type) == astroid.Const and \ + type(index_type.value) == int) or \ + (type(index_type) == astroid.Instance and \ + index_type._proxied.name == 'int')): + self.add_message('non-integer-list-index', node=node, + args=(index_type,)) def register(linter): """required method to auto register this checker """ |