summaryrefslogtreecommitdiff
path: root/test/input/func_invalid_sequence_index.py
blob: c1d6e7283dd1d8507f197439a3b52a79d8aff35e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
"""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'

def function1():
    """list index is a function"""
    return TESTLIST[id]

def function2():
    """list index is a str constant"""
    return TESTLIST['0']

def function3():
    """list index is None"""
    return TESTLIST[None]

def function4():
    """list index is a float expression"""
    return TESTLIST[float(0)]

def function5():
    """list index is an int constant"""
    return TESTLIST[0]  # no error

def function6():
    """list index is a integer expression"""
    return TESTLIST[int(0.0)] # no error

def function7():
    """list index is a slice"""
    return TESTLIST[slice(1, 2, 3)] # no error

def function8():
    """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 function9():
    """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 function10():
    """list index does not implement __index__"""
    class NonIndexType(object):
        """Class without __index__ method"""
        pass

    return TESTLIST[NonIndexType()]

# Repeat a handful of tests to ensure non-list types are caught
def function11():
    """Tuple index is None"""
    return TESTTUPLE[None]

def function12():
    """Tuple index is an int constant"""
    return TESTTUPLE[0]

def function13():
    """String index is None"""
    return TESTSTR[None]

def function14():
    """String index is an int constant"""
    return TESTSTR[0]

def function15():
    """Index of subclass of tuple is None"""
    class TupleTest(tuple):
        """Subclass of tuple"""
        pass
    return TupleTest()[None]

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