summaryrefslogtreecommitdiff
path: root/pylint/test/functional/invalid_slice_index.py
blob: 63ba252b777d4f28bf313dd888c2d098d33d5055 (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
"""Errors for invalid slice indices"""
# pylint: disable=too-few-public-methods, no-self-use


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