summaryrefslogtreecommitdiff
path: root/tests/functional/n/non/non_iterator_returned.py
blob: de83f68a2c430e4a748662b337eaa7181b86ce39 (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
"""Check non-iterators returned by __iter__ """

# pylint: disable=too-few-public-methods, missing-docstring, useless-object-inheritance, consider-using-with


class FirstGoodIterator(object):
    """ yields in iterator. """

    def __iter__(self):
        for index in range(10):
            yield index


class SecondGoodIterator(object):
    """ __iter__ and next """

    def __iter__(self):
        return self

    def __next__(self):
        """ Infinite iterator, but still an iterator """
        return 1

    def next(self):
        """Same as __next__, but for Python 2."""
        return 1


class ThirdGoodIterator(object):
    """ Returns other iterator, not the current instance """

    def __iter__(self):
        return SecondGoodIterator()


class FourthGoodIterator(object):
    """ __iter__ returns iter(...) """

    def __iter__(self):
        return iter(range(10))


class IteratorMetaclass(type):
    def __next__(cls):
        return 1

    def next(cls):
        return 2


class IteratorClass(object, metaclass=IteratorMetaclass):
    """Iterable through the metaclass."""


class FifthGoodIterator(object):
    """__iter__ returns a class which uses an iterator-metaclass."""

    def __iter__(self):
        return IteratorClass


class FileBasedIterator(object):
    def __init__(self, path):
        self.path = path
        self.file = None

    def __iter__(self):
        if self.file is not None:
            self.file.close()
        self.file = open(self.path, encoding="utf-8")
        # self file has two inferred values: None and <instance of 'file'>
        # we don't want to emit error in this case
        return self.file


class FirstBadIterator(object):
    """ __iter__ returns a list """

    def __iter__(self):  # [non-iterator-returned]
        return []


class SecondBadIterator(object):
    """ __iter__ without next """

    def __iter__(self):  # [non-iterator-returned]
        return self


class ThirdBadIterator(object):
    """ __iter__ returns an instance of another non-iterator """

    def __iter__(self):  # [non-iterator-returned]
        return SecondBadIterator()


class FourthBadIterator(object):
    """__iter__ returns a class."""

    def __iter__(self):  # [non-iterator-returned]
        return ThirdBadIterator