sources for model.py [rev. unknown]
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
""" model - type system model for apigen
"""
# we implement all the types which are in the types.*, naming
# scheme after pypy's
import py
import types
set = py.builtin.set
# __extend__ and pairtype?
class SomeObject(object):
    typedef = types.ObjectType
    
    def __repr__(self):
        return "<%s>" % self.__class__.__name__[4:]
        return str(self.typedef)[7:-2]
    
    def unionof(self, other):
        if isinstance(other, SomeImpossibleValue):
            return self
        if isinstance(other, SomeUnion):
            return other.unionof(self)
        if self == other:
            return self
        return SomeUnion([self, other])
    
    def gettypedef(self):
        return self.typedef
    
    def __hash__(self):
        return hash(self.__class__)
    
    def __eq__(self, other):
        return self.__class__ == other.__class__
    
    def __ne__(self, other):
        return not self == other
        
    # this is to provide possibility of eventually linking some stuff
    def striter(self):
        yield str(self)
class SomeUnion(object):
    # empty typedef
    def __init__(self, possibilities):
        self.possibilities = set(possibilities)
    
    def unionof(self, other):
        if isinstance(other, SomeUnion):
            return SomeUnion(self.possibilities.union(other.possibilities))
        return SomeUnion(list(self.possibilities) + [other])
    
    def __eq__(self, other):
        if type(other) is not SomeUnion:
            return False
        return self.possibilities == other.possibilities
    
    def __ne__(self, other):
        return not self == other
    
    def __repr__(self):
        return "AnyOf(%s)" % ", ".join([str(i) for i in list(self.possibilities)])
    
    def gettypedef(self):
        return (None, None)
    
    def striter(self):
        yield "AnyOf("
        for num, i in enumerate(self.possibilities):
            yield i
            if num != len(self.possibilities) - 1:
                yield ", "
        yield ")"
class SomeBoolean(SomeObject):
    typedef = types.BooleanType
class SomeBuffer(SomeObject):
    typedef = types.BufferType
class SomeBuiltinFunction(SomeObject):
    typedef = types.BuiltinFunctionType
#class SomeBuiltinMethod(SomeObject):
#    typedef = types.BuiltinMethodType
class SomeClass(SomeObject):
    typedef = types.ClassType
    
    def __init__(self, cls):
        self.cls = cls
        self.name = cls.__name__
        self.id = id(cls)
    def __getstate__(self):
        return (self.name, self.id)
    def __setstate__(self, state):
        self.name, self.id = state
        self.cls = None
    
    def __hash__(self):
        return hash("Class") ^ hash(self.id)
    
    def __eq__(self, other):
        if type(other) is not SomeClass:
            return False
        return self.id == other.id
    
    def unionof(self, other):
        if type(other) is not SomeClass or self.id is not other.id:
            return super(SomeClass, self).unionof(other)
        return self
    
    def __repr__(self):
        return "Class %s" % self.name
    
class SomeCode(SomeObject):
    typedef = types.CodeType
class SomeComplex(SomeObject):
    typedef = types.ComplexType
class SomeDictProxy(SomeObject):
    typedef = types.DictProxyType
class SomeDict(SomeObject):
    typedef = types.DictType
class SomeEllipsis(SomeObject):
    typedef = types.EllipsisType
class SomeFile(SomeObject):
    typedef = types.FileType
class SomeFloat(SomeObject):
    typedef = types.FloatType
class SomeFrame(SomeObject):
    typedef = types.FrameType
class SomeFunction(SomeObject):
    typedef = types.FunctionType
class SomeGenerator(SomeObject):
    typedef = types.GeneratorType
class SomeInstance(SomeObject):
    def __init__(self, classdef):
        self.classdef = classdef
        
    def __hash__(self):
        return hash("SomeInstance") ^ hash(self.classdef)
    
    def __eq__(self, other):
        if type(other) is not SomeInstance:
            return False
        return other.classdef == self.classdef
    
    def unionof(self, other):
        if type(other) is not SomeInstance:
            return super(SomeInstance, self).unionof(other)
        if self.classdef == other.classdef:
            return self
        return SomeInstance(unionof(self.classdef, other.classdef))
    
    def __repr__(self):
        return "<Instance of %s>" % str(self.classdef)
    
    def striter(self):
        yield "<Instance of "
        yield self.classdef
        yield ">"
    
    typedef = types.InstanceType
class SomeInt(SomeObject):
    typedef = types.IntType
class SomeLambda(SomeObject):
    typedef = types.LambdaType
class SomeList(SomeObject):
    typedef = types.ListType
class SomeLong(SomeObject):
    typedef = types.LongType
class SomeMethod(SomeObject):
    typedef = types.MethodType
class SomeModule(SomeObject):
    typedef = types.ModuleType
class SomeNone(SomeObject):
    typedef = types.NoneType
class SomeNotImplemented(SomeObject):
    typedef = types.NotImplementedType
class SomeObject(SomeObject):
    typedef = types.ObjectType
class SomeSlice(SomeObject):
    typedef = types.SliceType
class SomeString(SomeObject):
    typedef = types.StringType
class SomeTraceback(SomeObject):
    typedef = types.TracebackType
class SomeTuple(SomeObject):
    typedef = types.TupleType
class SomeType(SomeObject):
    typedef = types.TypeType
class SomeUnboundMethod(SomeObject):
    typedef = types.UnboundMethodType
class SomeUnicode(SomeObject):
    typedef = types.UnicodeType
class SomeXRange(SomeObject):
    typedef = types.XRangeType
class SomeImpossibleValue(SomeObject):
    def unionof(self, other):
        return other
    
    def __repr__(self):
        return "<UNKNOWN>"
s_ImpossibleValue = SomeImpossibleValue()
s_None = SomeNone()
s_Ellipsis = SomeEllipsis()
def guess_type(x):
    # this is mostly copy of immutablevalue
    if hasattr(x, 'im_self') and x.im_self is None:
        x = x.im_func
        assert not hasattr(x, 'im_self')
    tp = type(x)
    if tp is bool:
        result = SomeBoolean()
    elif tp is int:
        result = SomeInt()
    elif issubclass(tp, str):
        result = SomeString()
    elif tp is unicode:
        result = SomeUnicode()
    elif tp is tuple:
        result = SomeTuple()
        #result = SomeTuple(items = [self.immutablevalue(e, need_const) for e in x])
    elif tp is float:
        result = SomeFloat()
    elif tp is list:
        #else:
        #    listdef = ListDef(self, s_ImpossibleValue)
        #    for e in x:
        #        listdef.generalize(self.annotation_from_example(e))
        result = SomeList()
    elif tp is dict:
##        dictdef = DictDef(self, 
##        s_ImpossibleValue,
##        s_ImpossibleValue,
##        is_r_dict = tp is r_dict)
##        if tp is r_dict:
##            s_eqfn = self.immutablevalue(x.key_eq)
##            s_hashfn = self.immutablevalue(x.key_hash)
##            dictdef.dictkey.update_rdict_annotations(s_eqfn,
##                s_hashfn)
##        for ek, ev in x.iteritems():
##            dictdef.generalize_key(self.annotation_from_example(ek))
##            dictdef.generalize_value(self.annotation_from_example(ev))
        result = SomeDict()
    elif tp is types.ModuleType:
        result = SomeModule()
    elif callable(x):
        #if hasattr(x, '__self__') and x.__self__ is not None:
        #    # for cases like 'l.append' where 'l' is a global constant list
        #    s_self = self.immutablevalue(x.__self__, need_const)
        #    result = s_self.find_method(x.__name__)
        #    if result is None:
        #        result = SomeObject()
        #elif hasattr(x, 'im_self') and hasattr(x, 'im_func'):
        #    # on top of PyPy, for cases like 'l.append' where 'l' is a
        #    # global constant list, the find_method() returns non-None
        #    s_self = self.immutablevalue(x.im_self, need_const)
        #    result = s_self.find_method(x.im_func.__name__)
        #else:
        #    result = None
        #if result is None:
        #    if (self.annotator.policy.allow_someobjects
        #        and getattr(x, '__module__', None) == '__builtin__'
        #        # XXX note that the print support functions are __builtin__
        #        and tp not in (types.FunctionType, types.MethodType)):
        ##        result = SomeObject()
        #        result.knowntype = tp # at least for types this needs to be correct
        #    else:
        #        result = SomePBC([self.getdesc(x)])
        if tp is types.BuiltinFunctionType or tp is types.BuiltinMethodType:
            result = SomeBuiltinFunction()
        elif hasattr(x, 'im_func'):
            result = SomeMethod()
        elif hasattr(x, 'func_code'):
            result = SomeFunction()
        elif hasattr(x, '__class__'):
            if x.__class__ is type:
                result = SomeClass(x)
            else:
                result = SomeInstance(SomeClass(x.__class__))
        elif tp is types.ClassType:
            result = SomeClass(x)
    elif x is None:
        return s_None
    elif hasattr(x, '__class__'):
        result = SomeInstance(SomeClass(x.__class__))
    else:
        result = SomeObject()
    # XXX here we might want to consider stuff like
    # buffer, slice, etc. etc. Let's leave it for now
    return result
def unionof(first, other):
    return first.unionof(other)