Statistics
| Revision:

gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / resources-plugin / scripting / lib / pylint / test / functional / non_iterator_returned.py @ 745

History | View | Annotate | Download (2.19 KB)

1
"""Check non-iterators returned by __iter__ """
2

    
3
# pylint: disable=too-few-public-methods, missing-docstring, no-self-use
4

    
5
import six
6

    
7
class FirstGoodIterator(object):
8
    """ yields in iterator. """
9

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

    
14
class SecondGoodIterator(object):
15
    """ __iter__ and next """
16

    
17
    def __iter__(self):
18
        return self
19

    
20
    def __next__(self):
21
        """ Infinite iterator, but still an iterator """
22
        return 1
23

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

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

    
31
    def __iter__(self):
32
        return SecondGoodIterator()
33

    
34
class FourthGoodIterator(object):
35
    """ __iter__ returns iter(...) """
36

    
37
    def __iter__(self):
38
        return iter(range(10))
39

    
40

    
41
class IteratorMetaclass(type):
42
    def __next__(cls):
43
        return 1
44

    
45
    def next(cls):
46
        return 2
47

    
48

    
49
@six.add_metaclass(IteratorMetaclass)
50
class IteratorClass(object):
51
    """Iterable through the metaclass."""
52

    
53

    
54
class FifthGoodIterator(object):
55
    """__iter__ returns a class which uses an iterator-metaclass."""
56
    def __iter__(self):
57
        return IteratorClass
58

    
59
class FileBasedIterator(object):
60
    def __init__(self, path):
61
        self.path = path
62
        self.file = None
63

    
64
    def __iter__(self):
65
        if self.file is not None:
66
            self.file.close()
67
        self.file = open(self.path)
68
        # self file has two infered values: None and <instance of 'file'>
69
        # we don't want to emit error in this case
70
        return self.file
71

    
72

    
73
class FirstBadIterator(object):
74
    """ __iter__ returns a list """
75

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

    
79
class SecondBadIterator(object):
80
    """ __iter__ without next """
81

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

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

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

    
91
class FourthBadIterator(object):
92
    """__iter__ returns a class."""
93

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