summaryrefslogtreecommitdiff
path: root/tests/functional/i/invalid/invalid_slice_index.py
blob: 2e5d2cdb0542df170fda6ef924e4f5045fe083da (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
"""Errors for invalid slice indices"""
# pylint: disable=too-few-public-methods,missing-docstring,expression-not-assigned,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:
        """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:
        """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:
        """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")]