summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorDavid Shea <dshea@redhat.com>2014-07-08 09:10:45 -0400
committerDavid Shea <dshea@redhat.com>2014-07-08 09:10:45 -0400
commit4824b1c229cd9bcf82de03078f44a6e76779413c (patch)
treecc009072324f0a39f6b25d9ee0f568872e7495b2 /test
parentfac1865c061d62ddd5a8473a6f6d3fed9134d047 (diff)
parent110ad5ce0e67d042dacde86d5b3150b7b3064dfc (diff)
downloadpylint-4824b1c229cd9bcf82de03078f44a6e76779413c.tar.gz
Merge upstream default into list-index-checker
Diffstat (limited to 'test')
-rw-r--r--test/input/func_invalid_sequence_index.py210
-rw-r--r--test/input/func_invalid_slice_index.py61
-rw-r--r--test/messages/func_invalid_sequence_index.txt19
-rw-r--r--test/messages/func_invalid_slice_index.txt5
4 files changed, 295 insertions, 0 deletions
diff --git a/test/input/func_invalid_sequence_index.py b/test/input/func_invalid_sequence_index.py
new file mode 100644
index 0000000..b60e0b5
--- /dev/null
+++ b/test/input/func_invalid_sequence_index.py
@@ -0,0 +1,210 @@
+"""Errors for invalid sequence indices"""
+# pylint: disable=too-few-public-methods, no-self-use
+
+__revision__ = 0
+
+TESTLIST = [1, 2, 3]
+TESTTUPLE = (1, 2, 3)
+TESTSTR = '123'
+
+# getitem tests with bad indices
+def function1():
+ """list index is a function"""
+ return TESTLIST[id]
+
+def function2():
+ """list index is None"""
+ return TESTLIST[None]
+
+def function3():
+ """list index is a float expression"""
+ return TESTLIST[float(0)]
+
+def function4():
+ """list index is a str constant"""
+ return TESTLIST['0']
+
+def function5():
+ """list index does not implement __index__"""
+ class NonIndexType(object):
+ """Class without __index__ method"""
+ pass
+
+ return TESTLIST[NonIndexType()]
+
+def function6():
+ """Tuple index is None"""
+ return TESTTUPLE[None]
+
+def function7():
+ """String index is None"""
+ return TESTSTR[None]
+
+def function8():
+ """Index of subclass of tuple is None"""
+ class TupleTest(tuple):
+ """Subclass of tuple"""
+ pass
+ return TupleTest()[None]
+
+# getitem tests with good indices
+def function9():
+ """list index is an int constant"""
+ return TESTLIST[0] # no error
+
+def function10():
+ """list index is a integer expression"""
+ return TESTLIST[int(0.0)] # no error
+
+def function11():
+ """list index is a slice"""
+ return TESTLIST[slice(1, 2, 3)] # no error
+
+def function12():
+ """list index implements __index__"""
+ class IndexType(object):
+ """Class with __index__ method"""
+ def __index__(self):
+ """Allow objects of this class to be used as slice indices"""
+ return 0
+
+ return TESTLIST[IndexType()] # no error
+
+def function13():
+ """list index implements __index__ in a superclass"""
+ class IndexType(object):
+ """Class with __index__ method"""
+ def __index__(self):
+ """Allow objects of this class to be used as slice indices"""
+ return 0
+
+ class IndexSubType(IndexType):
+ """Class with __index__ in parent"""
+ pass
+
+ return TESTLIST[IndexSubType()] # no error
+
+def function14():
+ """Tuple index is an int constant"""
+ return TESTTUPLE[0]
+
+def function15():
+ """String index is an int constant"""
+ return TESTSTR[0]
+
+def function16():
+ """Index of subclass of tuple is an int constant"""
+ 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):
+ """Subclass of tuple with custom __getitem__"""
+ 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):
+ """Subclass of tuple with custom __getitem__"""
+ def __getitem__(self, index):
+ """Allow non-integer indices"""
+ return 0
+
+ class SubTupleTest(TupleTest):
+ """Subclass of a subclass of tuple"""
+ pass
+
+ return SubTupleTest()[None] # no error
+
+# Test with set and delete statements
+def function19():
+ """Set with None and integer indices"""
+ TESTLIST[None] = 0
+ TESTLIST[0] = 0 # no error
+
+def function20():
+ """Delete with None and integer indicies"""
+ del TESTLIST[None]
+ del TESTLIST[0] # no error
+
+def function21():
+ """Set and delete on a subclass of list"""
+ class ListTest(list):
+ """Inherit all list get/set/del handlers"""
+ pass
+ test = ListTest()
+
+ # Set and delete with invalid indices
+ test[None] = 0
+ del test[None]
+
+ # Set and delete with valid indices
+ test[0] = 0 # no error
+ del test[0] # no error
+
+def function22():
+ """Get, set, and delete on a subclass of list that overrides __setitem__"""
+ class ListTest(list):
+ """Override setitem but not get or del"""
+ def __setitem__(self, key, value):
+ pass
+ test = ListTest()
+
+ test[None][0] = 0 # failure on the getitem with None
+ del test[None]
+
+ test[0][0] = 0 # getitem with int and setitem with int, no error
+ test[None] = 0 # setitem overridden, no error
+ test[0] = 0 # setitem with int, no error
+ del test[0] # delitem with int, no error
+
+def function23():
+ """Get, set, and delete on a subclass of list that overrides __delitem__"""
+ class ListTest(list):
+ """Override delitem but not get or set"""
+ def __delitem__(self, key):
+ pass
+ test = ListTest()
+
+ test[None][0] = 0 # failure on the getitem with None
+ test[None] = 0 # setitem with invalid index
+
+ test[0][0] = 0 # getitem with int and setitem with int, no error
+ test[0] = 0 # setitem with int, no error
+ del test[None] # delitem overriden, no error
+ del test[0] # delitem with int, no error
+
+def function24():
+ """Get, set, and delete on a subclass of list that overrides __getitem__"""
+ class ListTest(list):
+ """Override gelitem but not del or set"""
+ def __getitem__(self, key):
+ pass
+ test = ListTest()
+
+ test[None] = 0 # setitem with invalid index
+ del test[None] # delitem with invalid index
+
+ test[None][0] = 0 # getitem overriden, no error
+ test[0][0] = 0 # getitem with int and setitem with int, no error
+ test[0] = 0 # setitem with int, no error
+ del test[0] # delitem with int, no error
+
+# Teest ExtSlice usage
+def function25():
+ """Extended slice used with a list"""
+ return TESTLIST[..., 0]
+
+def function26():
+ """Extended slice used with an object that implements __getitem__"""
+ class ExtSliceTest(object):
+ """Permit extslice syntax by implementing __getitem__"""
+ def __getitem__(self, index):
+ return 0
+ return ExtSliceTest[..., 0] # no error
diff --git a/test/input/func_invalid_slice_index.py b/test/input/func_invalid_slice_index.py
new file mode 100644
index 0000000..32f2f2d
--- /dev/null
+++ b/test/input/func_invalid_slice_index.py
@@ -0,0 +1,61 @@
+"""Errors for invalid slice indices"""
+# pylint: disable=too-few-public-methods, no-self-use
+
+__revision__ = 0
+
+TESTLIST = [1, 2, 3]
+
+# Invalid indices
+def function1():
+ """functions used as indices"""
+ return TESTLIST[id:id:]
+
+def function2():
+ """strings used as indices"""
+ return TESTLIST['0':'1':]
+
+def function3():
+ """class without __index__ used as index"""
+
+ class NoIndexTest(object):
+ """Class with no __index__ method"""
+ pass
+
+ return TESTLIST[NoIndexTest()::]
+
+# Valid indices
+def function4():
+ """integers used as indices"""
+ return TESTLIST[0:0:0] # no error
+
+def function5():
+ """None used as indices"""
+ return TESTLIST[None:None:None] # no error
+
+def function6():
+ """class with __index__ used as index"""
+ class IndexTest(object):
+ """Class with __index__ method"""
+ def __index__(self):
+ """Allow objects of this class to be used as slice indices"""
+ return 0
+
+ return TESTLIST[IndexTest():None:None] # no error
+
+def function7():
+ """class with __index__ in superclass used as index"""
+ class IndexType(object):
+ """Class with __index__ method"""
+ def __index__(self):
+ """Allow objects of this class to be used as slice indices"""
+ return 0
+
+ class IndexSubType(IndexType):
+ """Class with __index__ in parent"""
+ pass
+
+ return TESTLIST[IndexSubType():None:None] # no error
+
+def function8():
+ """slice object used as index"""
+ return TESTLIST[slice(1, 2, 3)] # no error
diff --git a/test/messages/func_invalid_sequence_index.txt b/test/messages/func_invalid_sequence_index.txt
new file mode 100644
index 0000000..db9edab
--- /dev/null
+++ b/test/messages/func_invalid_sequence_index.txt
@@ -0,0 +1,19 @@
+E: 13:function1: Sequence index is not an int, slice, or instance with __index__
+E: 17:function2: Sequence index is not an int, slice, or instance with __index__
+E: 21:function3: Sequence index is not an int, slice, or instance with __index__
+E: 25:function4: Sequence index is not an int, slice, or instance with __index__
+E: 33:function5: Sequence index is not an int, slice, or instance with __index__
+E: 37:function6: Sequence index is not an int, slice, or instance with __index__
+E: 41:function7: Sequence index is not an int, slice, or instance with __index__
+E: 48:function8: Sequence index is not an int, slice, or instance with __index__
+E:128:function19: Sequence index is not an int, slice, or instance with __index__
+E:133:function20: Sequence index is not an int, slice, or instance with __index__
+E:144:function21: Sequence index is not an int, slice, or instance with __index__
+E:145:function21: Sequence index is not an int, slice, or instance with __index__
+E:159:function22: Sequence index is not an int, slice, or instance with __index__
+E:160:function22: Sequence index is not an int, slice, or instance with __index__
+E:175:function23: Sequence index is not an int, slice, or instance with __index__
+E:176:function23: Sequence index is not an int, slice, or instance with __index__
+E:191:function24: Sequence index is not an int, slice, or instance with __index__
+E:192:function24: Sequence index is not an int, slice, or instance with __index__
+E:202:function25: 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
new file mode 100644
index 0000000..d5b9e86
--- /dev/null
+++ b/test/messages/func_invalid_slice_index.txt
@@ -0,0 +1,5 @@
+E: 11:function1: Slice index is not an int, None, or instance with __index__
+E: 11:function1: Slice index is not an int, None, or instance with __index__
+E: 15:function2: Slice index is not an int, None, or instance with __index__
+E: 15:function2: Slice index is not an int, None, or instance with __index__
+E: 24:function3: Slice index is not an int, None, or instance with __index__