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 / unsubscriptable_value.py @ 745
History | View | Annotate | Download (2.56 KB)
1 |
"""
|
---|---|
2 |
Checks that value used in a subscript supports subscription
|
3 |
(i.e. defines __getitem__ method).
|
4 |
"""
|
5 |
# pylint: disable=missing-docstring,pointless-statement,expression-not-assigned,wrong-import-position
|
6 |
# pylint: disable=too-few-public-methods,import-error,invalid-name,wrong-import-order
|
7 |
import six |
8 |
|
9 |
# primitives
|
10 |
numbers = [1, 2, 3] |
11 |
numbers[0]
|
12 |
"123"[0] |
13 |
u"123"[0] |
14 |
b"123"[0] |
15 |
bytearray(b"123")[0] |
16 |
dict(a=1, b=2)['a'] |
17 |
(1, 2, 3)[0] |
18 |
|
19 |
# list/dict comprehensions are fine
|
20 |
[x for x in range(10)][0] |
21 |
{x: 10 - x for x in range(10)}[0] |
22 |
|
23 |
|
24 |
# instances
|
25 |
class NonSubscriptable(object): |
26 |
pass
|
27 |
|
28 |
class Subscriptable(object): |
29 |
def __getitem__(self, key): |
30 |
return key + key
|
31 |
|
32 |
NonSubscriptable()[0] # [unsubscriptable-object] |
33 |
NonSubscriptable[0] # [unsubscriptable-object] |
34 |
Subscriptable()[0]
|
35 |
Subscriptable[0] # [unsubscriptable-object] |
36 |
|
37 |
# generators are not subscriptable
|
38 |
def powers_of_two(): |
39 |
k = 0
|
40 |
while k < 10: |
41 |
yield 2 ** k |
42 |
k += 1
|
43 |
|
44 |
powers_of_two()[0] # [unsubscriptable-object] |
45 |
powers_of_two[0] # [unsubscriptable-object] |
46 |
|
47 |
|
48 |
# check that primitive non subscriptable types are catched
|
49 |
True[0] # [unsubscriptable-object] |
50 |
None[0] # [unsubscriptable-object] |
51 |
8.5[0] # [unsubscriptable-object] |
52 |
10[0] # [unsubscriptable-object] |
53 |
|
54 |
# sets are not subscriptable
|
55 |
{x ** 2 for x in range(10)}[0] # [unsubscriptable-object] |
56 |
set(numbers)[0] # [unsubscriptable-object] |
57 |
frozenset(numbers)[0] # [unsubscriptable-object] |
58 |
|
59 |
# skip instances with unknown base classes
|
60 |
from some_missing_module import LibSubscriptable |
61 |
|
62 |
class MaybeSubscriptable(LibSubscriptable): |
63 |
pass
|
64 |
|
65 |
MaybeSubscriptable()[0]
|
66 |
|
67 |
# subscriptable classes (through metaclasses)
|
68 |
|
69 |
class MetaSubscriptable(type): |
70 |
def __getitem__(cls, key): |
71 |
return key + key
|
72 |
|
73 |
class SubscriptableClass(six.with_metaclass(MetaSubscriptable, object)): |
74 |
pass
|
75 |
|
76 |
SubscriptableClass[0]
|
77 |
SubscriptableClass()[0] # [unsubscriptable-object] |
78 |
|
79 |
# functions are not subscriptable
|
80 |
def test(*args, **kwargs): |
81 |
return args, kwargs
|
82 |
|
83 |
test()[0]
|
84 |
test[0] # [unsubscriptable-object] |
85 |
|
86 |
# deque
|
87 |
from collections import deque |
88 |
deq = deque(maxlen=10)
|
89 |
deq.append(42)
|
90 |
deq[0]
|
91 |
|
92 |
|
93 |
class AbstractClass(object): |
94 |
|
95 |
def __init__(self): |
96 |
self.ala = {i for i in range(10)} |
97 |
self.bala = [i for i in range(10)] |
98 |
self.portocala = None |
99 |
|
100 |
def test_unsubscriptable(self): |
101 |
self.bala[0] |
102 |
self.portocala[0] |
103 |
|
104 |
|
105 |
class ClassMixin(object): |
106 |
|
107 |
def __init__(self): |
108 |
self.ala = {i for i in range(10)} |
109 |
self.bala = [i for i in range(10)] |
110 |
self.portocala = None |
111 |
|
112 |
def test_unsubscriptable(self): |
113 |
self.bala[0] |
114 |
self.portocala[0] |
115 |
|