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 / using_constant_test.py @ 745
History | View | Annotate | Download (2.71 KB)
1 |
"""Verify if constant tests are used inside if statements."""
|
---|---|
2 |
# pylint: disable=invalid-name, missing-docstring,too-few-public-methods
|
3 |
# pylint: disable=no-init,expression-not-assigned
|
4 |
|
5 |
|
6 |
import collections |
7 |
|
8 |
|
9 |
def function(): |
10 |
yield
|
11 |
|
12 |
|
13 |
class Class(object): |
14 |
|
15 |
def method(self): |
16 |
pass
|
17 |
|
18 |
|
19 |
instance = Class() |
20 |
|
21 |
if collections: # [using-constant-test] |
22 |
pass
|
23 |
|
24 |
# GenExpr
|
25 |
if (node for node in range(10)): # [using-constant-test] |
26 |
pass
|
27 |
|
28 |
if lambda: None: # [using-constant-test] |
29 |
pass
|
30 |
|
31 |
if function: # [using-constant-test] |
32 |
pass
|
33 |
|
34 |
if Class: # [using-constant-test] |
35 |
pass
|
36 |
|
37 |
if 2: # [using-constant-test] |
38 |
pass
|
39 |
|
40 |
if True: # [using-constant-test] |
41 |
pass
|
42 |
|
43 |
if '': # [using-constant-test] |
44 |
pass
|
45 |
|
46 |
if b'': # [using-constant-test] |
47 |
pass
|
48 |
|
49 |
if 2.0: # [using-constant-test] |
50 |
pass
|
51 |
|
52 |
if {}: # [using-constant-test] |
53 |
pass
|
54 |
|
55 |
if {1, 2, 3}: # [using-constant-test] |
56 |
pass
|
57 |
|
58 |
if (1, 2, 3): # [using-constant-test] |
59 |
pass
|
60 |
|
61 |
if (): # [using-constant-test] |
62 |
pass
|
63 |
|
64 |
# Generator
|
65 |
generator = function() |
66 |
if generator: # [using-constant-test] |
67 |
pass
|
68 |
|
69 |
if 1 if 2 else 3: # [using-constant-test] |
70 |
pass
|
71 |
|
72 |
def test_comprehensions(): |
73 |
[data for data in range(100) if len] # [using-constant-test] |
74 |
[data for data in range(100) if 1] # [using-constant-test] |
75 |
(data for data in range(100) if len) # [using-constant-test] |
76 |
(data for data in range(100) if 1) # [using-constant-test] |
77 |
{data for data in range(100) if len} # [using-constant-test] |
78 |
{data: 1 for data in range(100) if len} # [using-constant-test] |
79 |
|
80 |
|
81 |
|
82 |
# For these, we require to do inference, even though the result can be a
|
83 |
# constant value. For some of them, we could determine that the test
|
84 |
# is constant, such as 2 + 3, but the components of the BinOp
|
85 |
# can be anything else (2 + somefunccall).
|
86 |
|
87 |
name = 42
|
88 |
if name:
|
89 |
pass
|
90 |
|
91 |
# UnboundMethod / Function
|
92 |
if Class.method:
|
93 |
pass
|
94 |
|
95 |
# BoundMethod
|
96 |
if instance.method:
|
97 |
pass
|
98 |
|
99 |
if 3 + 4: |
100 |
pass
|
101 |
|
102 |
if 3 and 4: |
103 |
pass
|
104 |
|
105 |
if not 3: |
106 |
pass
|
107 |
|
108 |
if instance.method():
|
109 |
pass
|
110 |
|
111 |
# pylint: disable=misplaced-comparison-constant
|
112 |
if 2 < 3: |
113 |
pass
|
114 |
|
115 |
if tuple((1, 2, 3)): |
116 |
pass
|
117 |
|
118 |
if dict(): |
119 |
pass
|
120 |
|
121 |
if tuple(): |
122 |
pass
|
123 |
|
124 |
if [1, 2, 3][:1]: |
125 |
pass
|
126 |
|
127 |
def test(*args): |
128 |
if args:
|
129 |
return 42 |
130 |
|
131 |
def test_good_comprehension_checks(): |
132 |
[data for data in range(100)] |
133 |
[data for data in range(100) if data] |
134 |
[data for data in range(100) if len(data)] |
135 |
(data for data in range(100) if data) |
136 |
(data for data in range(100) if len(data)) |
137 |
{data for data in range(100) if data} |
138 |
{data for data in range(100) if len(data)} |
139 |
{data: 1 for data in range(100) if data} |
140 |
{data: 1 for data in range(100)} |