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 / iterable_context.py @ 745
History | View | Annotate | Download (3.34 KB)
1 |
"""
|
---|---|
2 |
Checks that primitive values are not used in an
|
3 |
iterating/mapping context.
|
4 |
"""
|
5 |
# pylint: disable=missing-docstring,invalid-name,too-few-public-methods,no-init,no-self-use,import-error,unused-argument,bad-mcs-method-argument,wrong-import-position
|
6 |
from __future__ import print_function |
7 |
|
8 |
# primitives
|
9 |
numbers = [1, 2, 3] |
10 |
|
11 |
for i in numbers: |
12 |
pass
|
13 |
|
14 |
for i in iter(numbers): |
15 |
pass
|
16 |
|
17 |
for i in "123": |
18 |
pass
|
19 |
|
20 |
for i in u"123": |
21 |
pass
|
22 |
|
23 |
for i in b"123": |
24 |
pass
|
25 |
|
26 |
for i in bytearray(b"123"): |
27 |
pass
|
28 |
|
29 |
for i in set(numbers): |
30 |
pass
|
31 |
|
32 |
for i in frozenset(numbers): |
33 |
pass
|
34 |
|
35 |
for i in dict(a=1, b=2): |
36 |
pass
|
37 |
|
38 |
# comprehensions
|
39 |
for i in [x for x in range(10)]: |
40 |
pass
|
41 |
|
42 |
for i in {x for x in range(1, 100, 2)}: |
43 |
pass
|
44 |
|
45 |
for i in {x: 10 - x for x in range(10)}: |
46 |
pass
|
47 |
|
48 |
# generators
|
49 |
def powers_of_two(): |
50 |
k = 0
|
51 |
while k < 10: |
52 |
yield 2 ** k |
53 |
k += 1
|
54 |
|
55 |
for i in powers_of_two(): |
56 |
pass
|
57 |
|
58 |
for i in powers_of_two: # [not-an-iterable] |
59 |
pass
|
60 |
|
61 |
# check for custom iterators
|
62 |
class A(object): |
63 |
pass
|
64 |
|
65 |
class B(object): |
66 |
def __iter__(self): |
67 |
return self |
68 |
|
69 |
def __next__(self): |
70 |
return 1 |
71 |
|
72 |
def next(self): |
73 |
return 1 |
74 |
|
75 |
class C(object): |
76 |
"old-style iterator"
|
77 |
def __getitem__(self, k): |
78 |
if k > 10: |
79 |
raise IndexError |
80 |
return k + 1 |
81 |
|
82 |
def __len__(self): |
83 |
return 10 |
84 |
|
85 |
for i in C(): |
86 |
print(i) |
87 |
|
88 |
|
89 |
def test(*args): |
90 |
print(args) |
91 |
|
92 |
|
93 |
test(*A()) # [not-an-iterable]
|
94 |
test(*B()) |
95 |
test(*B) # [not-an-iterable]
|
96 |
for i in A(): # [not-an-iterable] |
97 |
pass
|
98 |
for i in B(): |
99 |
pass
|
100 |
for i in B: # [not-an-iterable] |
101 |
pass
|
102 |
|
103 |
for i in range: # [not-an-iterable] |
104 |
pass
|
105 |
|
106 |
# check that primitive non-iterable types are catched
|
107 |
for i in True: # [not-an-iterable] |
108 |
pass
|
109 |
|
110 |
for i in None: # [not-an-iterable] |
111 |
pass
|
112 |
|
113 |
for i in 8.5: # [not-an-iterable] |
114 |
pass
|
115 |
|
116 |
for i in 10: # [not-an-iterable] |
117 |
pass
|
118 |
|
119 |
|
120 |
# skip uninferable instances
|
121 |
from some_missing_module import Iterable |
122 |
|
123 |
class MyClass(Iterable): |
124 |
pass
|
125 |
|
126 |
m = MyClass() |
127 |
for i in m: |
128 |
print(i) |
129 |
|
130 |
# skip checks if statement is inside mixin/base/abstract class
|
131 |
class ManagedAccessViewMixin(object): |
132 |
access_requirements = None
|
133 |
|
134 |
def get_access_requirements(self): |
135 |
return self.access_requirements |
136 |
|
137 |
def dispatch(self, *_args, **_kwargs): |
138 |
klasses = self.get_access_requirements()
|
139 |
|
140 |
# no error should be emitted here
|
141 |
for requirement in klasses: |
142 |
print(requirement) |
143 |
|
144 |
class BaseType(object): |
145 |
valid_values = None
|
146 |
|
147 |
def validate(self, value): |
148 |
if self.valid_values is None: |
149 |
return True |
150 |
else:
|
151 |
# error should not be emitted here
|
152 |
for v in self.valid_values: |
153 |
if value == v:
|
154 |
return True |
155 |
return False |
156 |
|
157 |
class AbstractUrlMarkManager(object): |
158 |
def __init__(self): |
159 |
self._lineparser = None |
160 |
self._init_lineparser()
|
161 |
# error should not be emitted here
|
162 |
for line in self._lineparser: |
163 |
print(line) |
164 |
|
165 |
def _init_lineparser(self): |
166 |
raise NotImplementedError |
167 |
|
168 |
# class is not named as abstract
|
169 |
# but still is deduceably abstract
|
170 |
class UrlMarkManager(object): |
171 |
def __init__(self): |
172 |
self._lineparser = None |
173 |
self._init_lineparser()
|
174 |
# error should not be emitted here
|
175 |
for line in self._lineparser: |
176 |
print(line) |
177 |
|
178 |
def _init_lineparser(self): |
179 |
raise NotImplementedError |