File: flow_analysis.py

package info (click to toggle)
python-jedi 0.10.0~git1%2Bf05c071-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,064 kB
  • ctags: 3,014
  • sloc: python: 16,997; makefile: 149; ansic: 13
file content (257 lines) | stat: -rw-r--r-- 3,546 bytes parent folder | download
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
def foo(x):
    if 1.0:
        return 1
    else:
        return ''

#? int()
foo(1)


#  Exceptions are not analyzed. So check both if branches
def try_except(x):
    try:
        if 0:
            return 1
        else:
            return ''
    except AttributeError:
        return 1.0

#? float() str()
try_except(1)


#  Exceptions are not analyzed. So check both if branches
def try_except(x):
    try:
        if 0:
            return 1
        else:
            return ''
    except AttributeError:
        return 1.0

#? float() str()
try_except(1)


# -----------------
# elif
# -----------------

def elif_flows1(x):
    if False:
        return 1
    elif True:
        return 1.0
    else:
        return ''

#? float()
elif_flows1(1)


def elif_flows2(x):
    try:
        if False:
            return 1
        elif 0:
            return 1.0
        else:
            return ''
    except ValueError:
        return set

#? str() set
elif_flows2(1)


def elif_flows3(x):
    try:
        if True:
            return 1
        elif 0:
            return 1.0
        else:
            return ''
    except ValueError:
        return set

#? int() set
elif_flows3(1)

# -----------------
# mid-difficulty if statements
# -----------------
def check(a):
    if a is None:
        return 1
    return ''
    return set

#? int()
check(None)
#? str()
check('asb')

a = list
if 2 == True:
    a = set
elif 1 == True:
    a = 0

#? int()
a
if check != 1:
    a = ''
#? str()
a
if check == check:
    a = list
#? list
a
if check != check:
    a = set
else:
    a = dict
#? dict
a
if not (check is not check):
    a = 1
#? int()
a


# -----------------
# name resolution
# -----------------

a = list
def elif_name(x):
    try:
        if True:
            a = 1
        elif 0:
            a = 1.0
        else:
            return ''
    except ValueError:
        a = x
    return a

#? int() set
elif_name(set)

if 0:
    a = ''
else:
    a = int

#? int
a

# -----------------
# isinstance
# -----------------

class A(): pass

def isinst(x):
    if isinstance(x, A):
        return dict
    elif isinstance(x, int) and x == 1 or x is True:
        return set
    elif isinstance(x, (float, reversed)):
        return list
    elif not isinstance(x, str):
        return tuple
    return 1

#? dict
isinst(A())
#? set
isinst(True)
#? set
isinst(1)
#? tuple
isinst(2)
#? list
isinst(1.0)
#? tuple
isinst(False)
#? int()
isinst('')

# -----------------
# flows that are not reachable should be able to access parent scopes.
# -----------------

foobar = ''

if 0:
    within_flow = 1.0
    #? float()
    within_flow
    #? str()
    foobar
    if 0:
        nested = 1
        #? int()
        nested
        #? float()
        within_flow
        #? str()
        foobar
    #?
    nested

# -----------------
# True objects like modules
# -----------------

class X():
    pass
if X:
    a = 1
else:
    a = ''
#? int()
a


# -----------------
# Recursion issues
# -----------------

def possible_recursion_error(filename):
    if filename == 'a':
        return filename
    # It seems like without the brackets there wouldn't be a RecursionError.
    elif type(filename) == str:
        return filename


if NOT_DEFINED:
    s = str()
else:
    s = str()
#? str()
possible_recursion_error(s)


# -----------------
# In combination with imports
# -----------------

from import_tree import flow_import

if 1 == flow_import.env:
    a = 1
elif 2 == flow_import.env:
    a = ''
elif 3 == flow_import.env:
    a = 1.0

#? int() str()
a