diff options
Diffstat (limited to 'tests/functional/i/invalid/s')
8 files changed, 409 insertions, 0 deletions
diff --git a/tests/functional/i/invalid/s/invalid_sequence_index.py b/tests/functional/i/invalid/s/invalid_sequence_index.py new file mode 100644 index 000000000..d11155c02 --- /dev/null +++ b/tests/functional/i/invalid/s/invalid_sequence_index.py @@ -0,0 +1,242 @@ +"""Errors for invalid sequence indices""" +# pylint: disable=too-few-public-methods, no-self-use, import-error, missing-docstring, useless-object-inheritance, unnecessary-pass +import six +from unknown import Unknown + +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] # [invalid-sequence-index] + +def function2(): + """list index is None""" + return TESTLIST[None] # [invalid-sequence-index] + +def function3(): + """list index is a float expression""" + return TESTLIST[float(0)] # [invalid-sequence-index] + +def function4(): + """list index is a str constant""" + return TESTLIST['0'] # [invalid-sequence-index] + +def function5(): + """list index does not implement __index__""" + class NonIndexType(object): + """Class without __index__ method""" + pass + + return TESTLIST[NonIndexType()] # [invalid-sequence-index] + +def function6(): + """Tuple index is None""" + return TESTTUPLE[None] # [invalid-sequence-index] + +def function7(): + """String index is None""" + return TESTSTR[None] # [invalid-sequence-index] + +def function8(): + """Index of subclass of tuple is None""" + class TupleTest(tuple): + """Subclass of tuple""" + pass + return TupleTest()[None] # [invalid-sequence-index] + +# getitem tests with good indices +def function9(): + """list index is an int constant""" + return TESTLIST[0] # no error + +def function10(): + """list index is an 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 # [invalid-sequence-index] + TESTLIST[0] = 0 # no error + +def function20(): + """Delete with None and integer indicies""" + del TESTLIST[None] # [invalid-sequence-index] + 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 # [invalid-sequence-index] + del test[None] # [invalid-sequence-index] + + # 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() + + # failure on the getitem with None + test[None][0] = 0 # [invalid-sequence-index] + # failure on the getitem with None + del test[None] # [invalid-sequence-index] + + 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() + + # failure on the getitem with None + test[None][0] = 0 # [invalid-sequence-index] + # setitem with invalid index + test[None] = 0 # [invalid-sequence-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 overridden, 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() + + # setitem with invalid index + test[None] = 0 # [invalid-sequence-index] + # delitem with invalid index + del test[None] # [invalid-sequence-index] + + test[None][0] = 0 # getitem overridden, 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] # [invalid-sequence-index] + +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 + +def function27(): + """Don't warn in the case where the indexed object has unknown base classes.""" + class UnknownBase(Unknown): + pass + slices = UnknownBase["aaaa"] + UnknownBase()[object] + ext_slices = UnknownBase[..., 0] + UnknownBase()[..., 0] + return slices, ext_slices + + +def function28(): + """Don't emit for classes with the right implementation.""" + + class Meta(type): + def __getitem__(cls, arg): + return 24 + + @six.add_metaclass(Meta) + class Works(object): + pass + + @six.add_metaclass(Meta) + class Error(list): + pass + + return Works['hello'] + Error['hello'] diff --git a/tests/functional/i/invalid/s/invalid_sequence_index.txt b/tests/functional/i/invalid/s/invalid_sequence_index.txt new file mode 100644 index 000000000..baf1b2167 --- /dev/null +++ b/tests/functional/i/invalid/s/invalid_sequence_index.txt @@ -0,0 +1,19 @@ +invalid-sequence-index:13:11:function1:Sequence index is not an int, slice, or instance with __index__ +invalid-sequence-index:17:11:function2:Sequence index is not an int, slice, or instance with __index__ +invalid-sequence-index:21:11:function3:Sequence index is not an int, slice, or instance with __index__ +invalid-sequence-index:25:11:function4:Sequence index is not an int, slice, or instance with __index__ +invalid-sequence-index:33:11:function5:Sequence index is not an int, slice, or instance with __index__ +invalid-sequence-index:37:11:function6:Sequence index is not an int, slice, or instance with __index__ +invalid-sequence-index:41:11:function7:Sequence index is not an int, slice, or instance with __index__ +invalid-sequence-index:48:11:function8:Sequence index is not an int, slice, or instance with __index__ +invalid-sequence-index:128:4:function19:Sequence index is not an int, slice, or instance with __index__ +invalid-sequence-index:133:8:function20:Sequence index is not an int, slice, or instance with __index__ +invalid-sequence-index:144:4:function21:Sequence index is not an int, slice, or instance with __index__ +invalid-sequence-index:145:8:function21:Sequence index is not an int, slice, or instance with __index__ +invalid-sequence-index:160:4:function22:Sequence index is not an int, slice, or instance with __index__ +invalid-sequence-index:162:8:function22:Sequence index is not an int, slice, or instance with __index__ +invalid-sequence-index:178:4:function23:Sequence index is not an int, slice, or instance with __index__ +invalid-sequence-index:180:4:function23:Sequence index is not an int, slice, or instance with __index__ +invalid-sequence-index:196:4:function24:Sequence index is not an int, slice, or instance with __index__ +invalid-sequence-index:198:8:function24:Sequence index is not an int, slice, or instance with __index__ +invalid-sequence-index:208:11:function25:Sequence index is not an int, slice, or instance with __index__ diff --git a/tests/functional/i/invalid/s/invalid_slice_index.py b/tests/functional/i/invalid/s/invalid_slice_index.py new file mode 100644 index 000000000..1eb2fda9c --- /dev/null +++ b/tests/functional/i/invalid/s/invalid_slice_index.py @@ -0,0 +1,71 @@ +"""Errors for invalid slice indices""" +# pylint: disable=too-few-public-methods, no-self-use,missing-docstring,expression-not-assigned, useless-object-inheritance, unnecessary-pass + + +TESTLIST = [1, 2, 3] + +# Invalid indices +def function1(): + """functions used as indices""" + return TESTLIST[id:id:] # [invalid-slice-index,invalid-slice-index] + +def function2(): + """strings used as indices""" + return TESTLIST['0':'1':] # [invalid-slice-index,invalid-slice-index] + +def function3(): + """class without __index__ used as index""" + + class NoIndexTest(object): + """Class with no __index__ method""" + pass + + return TESTLIST[NoIndexTest()::] # [invalid-slice-index] + +# 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 + + +def function9(): + """Use a custom class that knows how to handle string slices""" + class StringSlice: + def __getitem__(self, item): + return item + + StringSlice()["a":"b":"c"] + StringSlice()["a":"b":"c", "a":"b"] + StringSlice()[slice("a", "b", "c")] diff --git a/tests/functional/i/invalid/s/invalid_slice_index.txt b/tests/functional/i/invalid/s/invalid_slice_index.txt new file mode 100644 index 000000000..e715138b4 --- /dev/null +++ b/tests/functional/i/invalid/s/invalid_slice_index.txt @@ -0,0 +1,5 @@ +invalid-slice-index:10:0:function1:Slice index is not an int, None, or instance with __index__ +invalid-slice-index:10:0:function1:Slice index is not an int, None, or instance with __index__ +invalid-slice-index:14:0:function2:Slice index is not an int, None, or instance with __index__ +invalid-slice-index:14:0:function2:Slice index is not an int, None, or instance with __index__ +invalid-slice-index:23:0:function3:Slice index is not an int, None, or instance with __index__ diff --git a/tests/functional/i/invalid/s/invalid_star_assignment_target.py b/tests/functional/i/invalid/s/invalid_star_assignment_target.py new file mode 100644 index 000000000..452f9a728 --- /dev/null +++ b/tests/functional/i/invalid/s/invalid_star_assignment_target.py @@ -0,0 +1,4 @@ +"""Test for *a = b """
+
+*FIRST = [1, 2, 3] # [invalid-star-assignment-target]
+(*FIRST, ) = [1, 2, 3]
diff --git a/tests/functional/i/invalid/s/invalid_star_assignment_target.txt b/tests/functional/i/invalid/s/invalid_star_assignment_target.txt new file mode 100644 index 000000000..eb9f27c90 --- /dev/null +++ b/tests/functional/i/invalid/s/invalid_star_assignment_target.txt @@ -0,0 +1 @@ +invalid-star-assignment-target:3:0::Starred assignment target must be in a list or tuple diff --git a/tests/functional/i/invalid/s/invalid_str_returned.py b/tests/functional/i/invalid/s/invalid_str_returned.py new file mode 100644 index 000000000..00ef22046 --- /dev/null +++ b/tests/functional/i/invalid/s/invalid_str_returned.py @@ -0,0 +1,64 @@ +"""Check invalid value returned by __str__ """ + +# pylint: disable=too-few-public-methods,missing-docstring,no-self-use,import-error, useless-object-inheritance +import six + +from missing import Missing + + +class FirstGoodStr(object): + """__str__ returns <type 'str'>""" + + def __str__(self): + return "some str" + + +class SecondGoodStr(object): + """__str__ returns <type 'str'>""" + + def __str__(self): + return str(123) + + +class StrMetaclass(type): + def __str__(cls): + return "some str" + + +@six.add_metaclass(StrMetaclass) +class ThirdGoodStr(object): + """Str through the metaclass.""" + + +class FirstBadStr(object): + """ __str__ returns bytes """ + + def __str__(self): # [invalid-str-returned] + return b"123" + + +class SecondBadStr(object): + """ __str__ returns int """ + + def __str__(self): # [invalid-str-returned] + return 1 + + +class ThirdBadStr(object): + """ __str__ returns node which does not have 'value' in AST """ + + def __str__(self): # [invalid-str-returned] + return lambda: "some str" + + +class AmbiguousStr(object): + """ Uninferable return value """ + + __str__ = lambda self: Missing + + +class AnotherAmbiguousStr(object): + """Potential uninferable return value""" + + def __str__(self): + return str(Missing) diff --git a/tests/functional/i/invalid/s/invalid_str_returned.txt b/tests/functional/i/invalid/s/invalid_str_returned.txt new file mode 100644 index 000000000..36b7ec57c --- /dev/null +++ b/tests/functional/i/invalid/s/invalid_str_returned.txt @@ -0,0 +1,3 @@ +invalid-str-returned:36:4:FirstBadStr.__str__:__str__ does not return str +invalid-str-returned:43:4:SecondBadStr.__str__:__str__ does not return str +invalid-str-returned:50:4:ThirdBadStr.__str__:__str__ does not return str |