diff options
author | David Shea <dshea@redhat.com> | 2014-07-07 10:10:56 -0400 |
---|---|---|
committer | David Shea <dshea@redhat.com> | 2014-07-07 10:10:56 -0400 |
commit | 2a45373090e129807c45c4b7d9eb35a4ea94f117 (patch) | |
tree | d7544884f79972dbdca136468d49912d61893ce4 | |
parent | 9b46e20c59729b46135ce777c6b30a164927798a (diff) | |
download | pylint-2a45373090e129807c45c4b7d9eb35a4ea94f117.tar.gz |
Clean up some stylistic issues in the sequence and slice index type checks.
-rw-r--r-- | checkers/typecheck.py | 23 | ||||
-rw-r--r-- | test/input/func_invalid_sequence_index.py | 27 | ||||
-rw-r--r-- | test/input/func_invalid_slice_index.py | 13 | ||||
-rw-r--r-- | test/messages/func_invalid_sequence_index.txt | 16 | ||||
-rw-r--r-- | test/messages/func_invalid_slice_index.txt | 10 |
5 files changed, 43 insertions, 46 deletions
diff --git a/checkers/typecheck.py b/checkers/typecheck.py index 395e1b7..f0c8034 100644 --- a/checkers/typecheck.py +++ b/checkers/typecheck.py @@ -86,11 +86,6 @@ MSGS = { with an __index__ method.'), } -if sys.version_info >= (3,0): - BUILTINS = 'builtins' -else: - BUILTINS = '__builtin__' - def _determine_callable(callable_obj): # Ordering is important, since BoundMethod is a subclass of UnboundMethod, # and Function inherits Lambda. @@ -550,8 +545,11 @@ accessed. Python regular expressions are accepted.'} # sequence types but skip classes that override __getitem__ and # which may allow non-integer indices. try: - getitem = parent_type.getattr('__getitem__')[0] - except (astroid.NotFoundError, IndexError, TypeError): + getitems = parent_type.getattr('__getitem__') + if getitems is astroid.YES: + return + getitem = getitems[0] + except (astroid.NotFoundError, IndexError): return if not isinstance(getitem, astroid.Function): @@ -560,11 +558,10 @@ accessed. Python regular expressions are accepted.'} if not getitem.parent: return - if not isinstance(getitem.parent.parent, astroid.Module) or \ - getitem.parent.parent.name != BUILTINS: + if getitem.root() != BUILTINS: return - if not getitem.parent.name in sequence_types: + if getitem.parent.name not in sequence_types: return index_type = safe_infer(node) @@ -583,10 +580,9 @@ accessed. Python regular expressions are accepted.'} try: index_type.getattr('__index__') + return except astroid.NotFoundError: pass - else: - return # Anything else is an error self.add_message('invalid-sequence-index', node=node) @@ -619,10 +615,9 @@ accessed. Python regular expressions are accepted.'} try: index_type.getattr('__index__') + return except astroid.NotFoundError: pass - else: - return # Anything else is an error self.add_message('invalid-slice-index', node=node) diff --git a/test/input/func_invalid_sequence_index.py b/test/input/func_invalid_sequence_index.py index 29a8a62..c1d6e72 100644 --- a/test/input/func_invalid_sequence_index.py +++ b/test/input/func_invalid_sequence_index.py @@ -1,4 +1,5 @@ """Errors for invalid sequence indices""" +# pylint: disable=too-few-public-methods, no-self-use __revision__ = 0 @@ -36,9 +37,9 @@ def function7(): def function8(): """list index implements __index__""" - class IndexType(object): # pylint: disable=too-few-public-methods + class IndexType(object): """Class with __index__ method""" - def __index__(self): # pylint: disable=no-self-use + def __index__(self): """Allow objects of this class to be used as slice indices""" return 0 @@ -46,13 +47,13 @@ def function8(): def function9(): """list index implements __index__ in a superclass""" - class IndexType(object): # pylint: disable=too-few-public-methods + class IndexType(object): """Class with __index__ method""" - def __index__(self): # pylint: disable=no-self-use + def __index__(self): """Allow objects of this class to be used as slice indices""" return 0 - class IndexSubType(IndexType): # pylint: disable=too-few-public-methods + class IndexSubType(IndexType): """Class with __index__ in parent""" pass @@ -60,7 +61,7 @@ def function9(): def function10(): """list index does not implement __index__""" - class NonIndexType(object): # pylint: disable=too-few-public-methods + class NonIndexType(object): """Class without __index__ method""" pass @@ -85,36 +86,36 @@ def function14(): def function15(): """Index of subclass of tuple is None""" - class TupleTest(tuple): # pylint: disable=too-few-public-methods + class TupleTest(tuple): """Subclass of tuple""" pass return TupleTest()[None] def function16(): """Index of subclass of tuple is an int constant""" - class TupleTest(tuple): # pylint: disable=too-few-public-methods + class TupleTest(tuple): """Subclass of tuple""" pass return TupleTest()[0] # no error def function17(): """Index of subclass of tuple with custom __getitem__ is None""" - class TupleTest(tuple): # pylint: disable=too-few-public-methods + class TupleTest(tuple): """Subclass of tuple with custom __getitem__""" - def __getitem__(self, index): # pylint: disable=no-self-use + def __getitem__(self, index): """Allow non-integer indices""" return 0 return TupleTest()[None] # no error def function18(): """Index of subclass of tuple with __getitem__ in superclass is None""" - class TupleTest(tuple): # pylint: disable=too-few-public-methods + class TupleTest(tuple): """Subclass of tuple with custom __getitem__""" - def __getitem__(self, index): # pylint: disable=no-self-use + def __getitem__(self, index): """Allow non-integer indices""" return 0 - class SubTupleTest(TupleTest): # pylint: disable=too-few-public-methods + class SubTupleTest(TupleTest): """Subclass of a subclass of tuple""" pass diff --git a/test/input/func_invalid_slice_index.py b/test/input/func_invalid_slice_index.py index 583fb94..fa72b8c 100644 --- a/test/input/func_invalid_slice_index.py +++ b/test/input/func_invalid_slice_index.py @@ -1,4 +1,5 @@ """Errors for invalid slice indices""" +# pylint: disable=too-few-public-methods, no-self-use __revision__ = 0 @@ -15,7 +16,7 @@ def function2(): def function3(): """class without __index__ used as index""" - class NoIndexTest(object): # pylint: disable=too-few-public-methods + class NoIndexTest(object): """Class with no __index__ method""" pass @@ -31,9 +32,9 @@ def function5(): def function6(): """class with __index__ used as index""" - class IndexTest(object): # pylint: disable=too-few-public-methods + class IndexTest(object): """Class with __index__ method""" - def __index__(self): # pylint: disable=no-self-use + def __index__(self): """Allow objects of this class to be used as slice indices""" return 0 @@ -41,13 +42,13 @@ def function6(): def function7(): """class with __index__ in superclass used as index""" - class IndexType(object): # pylint: disable=too-few-public-methods + class IndexType(object): """Class with __index__ method""" - def __index__(self): # pylint: disable=no-self-use + def __index__(self): """Allow objects of this class to be used as slice indices""" return 0 - class IndexSubType(IndexType): # pylint: disable=too-few-public-methods + class IndexSubType(IndexType): """Class with __index__ in parent""" pass diff --git a/test/messages/func_invalid_sequence_index.txt b/test/messages/func_invalid_sequence_index.txt index d7b72fd..79a5956 100644 --- a/test/messages/func_invalid_sequence_index.txt +++ b/test/messages/func_invalid_sequence_index.txt @@ -1,8 +1,8 @@ -E: 11:function1: Sequence index is not an int, slice, or instance with __index__ -E: 15:function2: Sequence index is not an int, slice, or instance with __index__ -E: 19:function3: Sequence index is not an int, slice, or instance with __index__ -E: 23:function4: Sequence index is not an int, slice, or instance with __index__ -E: 67:function10: Sequence index is not an int, slice, or instance with __index__ -E: 72:function11: Sequence index is not an int, slice, or instance with __index__ -E: 80:function13: Sequence index is not an int, slice, or instance with __index__ -E: 91:function15: Sequence index is not an int, slice, or instance with __index__ +E: 12:function1: Sequence index is not an int, slice, or instance with __index__ +E: 16:function2: Sequence index is not an int, slice, or instance with __index__ +E: 20:function3: Sequence index is not an int, slice, or instance with __index__ +E: 24:function4: Sequence index is not an int, slice, or instance with __index__ +E: 68:function10: Sequence index is not an int, slice, or instance with __index__ +E: 73:function11: Sequence index is not an int, slice, or instance with __index__ +E: 81:function13: Sequence index is not an int, slice, or instance with __index__ +E: 92:function15: Sequence index is not an int, slice, or instance with __index__ diff --git a/test/messages/func_invalid_slice_index.txt b/test/messages/func_invalid_slice_index.txt index 2ffcd59..d6882b1 100644 --- a/test/messages/func_invalid_slice_index.txt +++ b/test/messages/func_invalid_slice_index.txt @@ -1,5 +1,5 @@ -E: 9:function1: Slice index is not an int, None, or instance with __index__ -E: 9:function1: Slice index is not an int, None, or instance with __index__ -E: 13:function2: Slice index is not an int, None, or instance with __index__ -E: 13:function2: Slice index is not an int, None, or instance with __index__ -E: 22:function3: Slice index is not an int, None, or instance with __index__ +E: 10:function1: Slice index is not an int, None, or instance with __index__ +E: 10:function1: Slice index is not an int, None, or instance with __index__ +E: 14:function2: Slice index is not an int, None, or instance with __index__ +E: 14:function2: Slice index is not an int, None, or instance with __index__ +E: 23:function3: Slice index is not an int, None, or instance with __index__ |