-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
containers.py
692 lines (546 loc) · 19.1 KB
/
containers.py
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
from collections.abc import Iterable
from .abstract import (ConstSized, Container, Hashable, MutableSequence,
Sequence, Type, TypeRef)
from .common import Buffer, IterableType, SimpleIterableType, SimpleIteratorType
from .misc import Undefined, unliteral, Optional, NoneType
from ..typeconv import Conversion
from ..errors import TypingError
from .. import utils
class Pair(Type):
"""
A heterogeneous pair.
"""
def __init__(self, first_type, second_type):
self.first_type = first_type
self.second_type = second_type
name = "pair<%s, %s>" % (first_type, second_type)
super(Pair, self).__init__(name=name)
@property
def key(self):
return self.first_type, self.second_type
def unify(self, typingctx, other):
if isinstance(other, Pair):
first = typingctx.unify_pairs(self.first_type, other.first_type)
second = typingctx.unify_pairs(self.second_type, other.second_type)
if first is not None and second is not None:
return Pair(first, second)
class BaseContainerIterator(SimpleIteratorType):
"""
Convenience base class for some container iterators.
Derived classes must implement the *container_class* attribute.
"""
def __init__(self, container):
assert isinstance(container, self.container_class), container
self.container = container
yield_type = container.dtype
name = 'iter(%s)' % container
super(BaseContainerIterator, self).__init__(name, yield_type)
def unify(self, typingctx, other):
cls = type(self)
if isinstance(other, cls):
container = typingctx.unify_pairs(self.container, other.container)
if container is not None:
return cls(container)
@property
def key(self):
return self.container
class BaseContainerPayload(Type):
"""
Convenience base class for some container payloads.
Derived classes must implement the *container_class* attribute.
"""
def __init__(self, container):
assert isinstance(container, self.container_class)
self.container = container
name = 'payload(%s)' % container
super(BaseContainerPayload, self).__init__(name)
@property
def key(self):
return self.container
class Bytes(Buffer):
"""
Type class for Python 3.x bytes objects.
"""
mutable = False
# Actually true but doesn't matter since bytes is immutable
slice_is_copy = False
class ByteArray(Buffer):
"""
Type class for bytearray objects.
"""
slice_is_copy = True
class PyArray(Buffer):
"""
Type class for array.array objects.
"""
slice_is_copy = True
class MemoryView(Buffer):
"""
Type class for memoryview objects.
"""
def is_homogeneous(*tys):
"""Are the types homogeneous?
"""
if tys:
first, tys = tys[0], tys[1:]
return not any(t != first for t in tys)
else:
# *tys* is empty.
return False
class BaseTuple(ConstSized, Hashable):
"""
The base class for all tuple types (with a known size).
"""
@classmethod
def from_types(cls, tys, pyclass=None):
"""
Instantiate the right tuple type for the given element types.
"""
if pyclass is not None and pyclass is not tuple:
# A subclass => is it a namedtuple?
assert issubclass(pyclass, tuple)
if hasattr(pyclass, "_asdict"):
tys = tuple(map(unliteral, tys))
homogeneous = is_homogeneous(*tys)
if homogeneous:
return NamedUniTuple(tys[0], len(tys), pyclass)
else:
return NamedTuple(tys, pyclass)
else:
dtype = utils.unified_function_type(tys)
if dtype is not None:
return UniTuple(dtype, len(tys))
# non-named tuple
homogeneous = is_homogeneous(*tys)
if homogeneous:
return cls._make_homogeneous_tuple(tys[0], len(tys))
else:
return cls._make_heterogeneous_tuple(tys)
@classmethod
def _make_homogeneous_tuple(cls, dtype, count):
return UniTuple(dtype, count)
@classmethod
def _make_heterogeneous_tuple(cls, tys):
return Tuple(tys)
class BaseAnonymousTuple(BaseTuple):
"""
Mixin for non-named tuples.
"""
def can_convert_to(self, typingctx, other):
"""
Convert this tuple to another one. Note named tuples are rejected.
"""
if not isinstance(other, BaseAnonymousTuple):
return
if len(self) != len(other):
return
if len(self) == 0:
return Conversion.safe
if isinstance(other, BaseTuple):
kinds = [typingctx.can_convert(ta, tb)
for ta, tb in zip(self, other)]
if any(kind is None for kind in kinds):
return
return max(kinds)
def __unliteral__(self):
return BaseTuple.from_types([unliteral(t) for t in self])
class _HomogeneousTuple(Sequence, BaseTuple):
@property
def iterator_type(self):
return UniTupleIter(self)
def __getitem__(self, i):
"""
Return element at position i
"""
return self.dtype
def __iter__(self):
return iter([self.dtype] * self.count)
def __len__(self):
return self.count
@property
def types(self):
return (self.dtype,) * self.count
class UniTuple(BaseAnonymousTuple, _HomogeneousTuple, Sequence):
"""
Type class for homogeneous tuples.
"""
def __init__(self, dtype, count):
self.dtype = dtype
self.count = count
name = "%s(%s x %d)" % (
self.__class__.__name__, dtype, count,
)
super(UniTuple, self).__init__(name)
@property
def mangling_args(self):
return self.__class__.__name__, (self.dtype, self.count)
@property
def key(self):
return self.dtype, self.count
def unify(self, typingctx, other):
"""
Unify UniTuples with their dtype
"""
if isinstance(other, UniTuple) and len(self) == len(other):
dtype = typingctx.unify_pairs(self.dtype, other.dtype)
if dtype is not None:
return UniTuple(dtype=dtype, count=self.count)
class UniTupleIter(BaseContainerIterator):
"""
Type class for homogeneous tuple iterators.
"""
container_class = _HomogeneousTuple
class _HeterogeneousTuple(BaseTuple):
def __getitem__(self, i):
"""
Return element at position i
"""
return self.types[i]
def __len__(self):
# Beware: this makes Tuple(()) false-ish
return len(self.types)
def __iter__(self):
return iter(self.types)
@staticmethod
def is_types_iterable(types):
# issue 4463 - check if argument 'types' is iterable
if not isinstance(types, Iterable):
raise TypingError("Argument 'types' is not iterable")
class UnionType(Type):
def __init__(self, types):
self.types = tuple(sorted(set(types), key=lambda x:x.name))
name = 'Union[{}]'.format(','.join(map(str, self.types)))
super(UnionType, self).__init__(name=name)
def get_type_tag(self, typ):
return self.types.index(typ)
class Tuple(BaseAnonymousTuple, _HeterogeneousTuple):
def __new__(cls, types):
t = utils.unified_function_type(types, require_precise=True)
if t is not None:
return UniTuple(dtype=t, count=len(types))
_HeterogeneousTuple.is_types_iterable(types)
if types and all(t == types[0] for t in types[1:]):
return UniTuple(dtype=types[0], count=len(types))
else:
return object.__new__(Tuple)
def __init__(self, types):
self.types = tuple(types)
self.count = len(self.types)
self.dtype = UnionType(types)
name = "%s(%s)" % (
self.__class__.__name__,
', '.join(str(i) for i in self.types),
)
super(Tuple, self).__init__(name)
@property
def mangling_args(self):
return self.__class__.__name__, tuple(t for t in self.types)
@property
def key(self):
return self.types
def unify(self, typingctx, other):
"""
Unify elements of Tuples/UniTuples
"""
# Other is UniTuple or Tuple
if isinstance(other, BaseTuple) and len(self) == len(other):
unified = [typingctx.unify_pairs(ta, tb)
for ta, tb in zip(self, other)]
if all(t is not None for t in unified):
return Tuple(unified)
class _StarArgTupleMixin:
@classmethod
def _make_homogeneous_tuple(cls, dtype, count):
return StarArgUniTuple(dtype, count)
@classmethod
def _make_heterogeneous_tuple(cls, tys):
return StarArgTuple(tys)
class StarArgTuple(_StarArgTupleMixin, Tuple):
"""To distinguish from Tuple() used as argument to a `*args`.
"""
def __new__(cls, types):
_HeterogeneousTuple.is_types_iterable(types)
if types and all(t == types[0] for t in types[1:]):
return StarArgUniTuple(dtype=types[0], count=len(types))
else:
return object.__new__(StarArgTuple)
class StarArgUniTuple(_StarArgTupleMixin, UniTuple):
"""To distinguish from UniTuple() used as argument to a `*args`.
"""
class BaseNamedTuple(BaseTuple):
pass
class NamedUniTuple(_HomogeneousTuple, BaseNamedTuple):
def __init__(self, dtype, count, cls):
self.dtype = dtype
self.count = count
self.fields = tuple(cls._fields)
self.instance_class = cls
name = "%s(%s x %d)" % (cls.__name__, dtype, count)
super(NamedUniTuple, self).__init__(name)
@property
def iterator_type(self):
return UniTupleIter(self)
@property
def key(self):
return self.instance_class, self.dtype, self.count
class NamedTuple(_HeterogeneousTuple, BaseNamedTuple):
def __init__(self, types, cls):
_HeterogeneousTuple.is_types_iterable(types)
self.types = tuple(types)
self.count = len(self.types)
self.fields = tuple(cls._fields)
self.instance_class = cls
name = "%s(%s)" % (cls.__name__, ', '.join(str(i) for i in self.types))
super(NamedTuple, self).__init__(name)
@property
def key(self):
return self.instance_class, self.types
class List(MutableSequence):
"""
Type class for (arbitrary-sized) homogeneous lists.
"""
mutable = True
def __init__(self, dtype, reflected=False):
dtype = unliteral(dtype)
self.dtype = dtype
self.reflected = reflected
cls_name = "reflected list" if reflected else "list"
name = "%s(%s)" % (cls_name, self.dtype)
super(List, self).__init__(name=name)
def copy(self, dtype=None, reflected=None):
if dtype is None:
dtype = self.dtype
if reflected is None:
reflected = self.reflected
return List(dtype, reflected)
def unify(self, typingctx, other):
if isinstance(other, List):
dtype = typingctx.unify_pairs(self.dtype, other.dtype)
reflected = self.reflected or other.reflected
if dtype is not None:
return List(dtype, reflected)
@property
def key(self):
return self.dtype, self.reflected
@property
def iterator_type(self):
return ListIter(self)
def is_precise(self):
return self.dtype.is_precise()
def __getitem__(self, args):
"""
Overrides the default __getitem__ from Type.
"""
return self.dtype
class ListIter(BaseContainerIterator):
"""
Type class for list iterators.
"""
container_class = List
class ListPayload(BaseContainerPayload):
"""
Internal type class for the dynamically-allocated payload of a list.
"""
container_class = List
class Set(Container):
"""
Type class for homogeneous sets.
"""
mutable = True
def __init__(self, dtype, reflected=False):
assert isinstance(dtype, (Hashable, Undefined))
self.dtype = dtype
self.reflected = reflected
cls_name = "reflected set" if reflected else "set"
name = "%s(%s)" % (cls_name, self.dtype)
super(Set, self).__init__(name=name)
@property
def key(self):
return self.dtype, self.reflected
@property
def iterator_type(self):
return SetIter(self)
def is_precise(self):
return self.dtype.is_precise()
def copy(self, dtype=None, reflected=None):
if dtype is None:
dtype = self.dtype
if reflected is None:
reflected = self.reflected
return Set(dtype, reflected)
def unify(self, typingctx, other):
if isinstance(other, Set):
dtype = typingctx.unify_pairs(self.dtype, other.dtype)
reflected = self.reflected or other.reflected
if dtype is not None:
return Set(dtype, reflected)
class SetIter(BaseContainerIterator):
"""
Type class for set iterators.
"""
container_class = Set
class SetPayload(BaseContainerPayload):
"""
Internal type class for the dynamically-allocated payload of a set.
"""
container_class = Set
class SetEntry(Type):
"""
Internal type class for the entries of a Set's hash table.
"""
def __init__(self, set_type):
self.set_type = set_type
name = 'entry(%s)' % set_type
super(SetEntry, self).__init__(name)
@property
def key(self):
return self.set_type
class ListType(IterableType):
"""List type
"""
mutable = True
def __init__(self, itemty):
assert not isinstance(itemty, TypeRef)
itemty = unliteral(itemty)
if isinstance(itemty, Optional):
fmt = 'List.item_type cannot be of type {}'
raise TypingError(fmt.format(itemty))
# FIXME: _sentry_forbidden_types(itemty)
self.item_type = itemty
self.dtype = itemty
name = '{}[{}]'.format(
self.__class__.__name__,
itemty,
)
super(ListType, self).__init__(name)
def is_precise(self):
return not isinstance(self.item_type, Undefined)
@property
def iterator_type(self):
return ListTypeIterableType(self).iterator_type
@classmethod
def refine(cls, itemty):
"""Refine to a precise list type
"""
res = cls(itemty)
assert res.is_precise()
return res
def unify(self, typingctx, other):
"""
Unify this with the *other* list.
"""
# If other is list
if isinstance(other, ListType):
if not other.is_precise():
return self
class ListTypeIterableType(SimpleIterableType):
"""List iterable type
"""
def __init__(self, parent):
assert isinstance(parent, ListType)
self.parent = parent
self.yield_type = self.parent.item_type
name = "list[{}]".format(self.parent.name)
iterator_type = ListTypeIteratorType(self)
super(ListTypeIterableType, self).__init__(name, iterator_type)
class ListTypeIteratorType(SimpleIteratorType):
def __init__(self, iterable):
self.parent = iterable.parent
self.iterable = iterable
yield_type = iterable.yield_type
name = "iter[{}->{}]".format(iterable.parent, yield_type)
super(ListTypeIteratorType, self).__init__(name, yield_type)
def _sentry_forbidden_types(key, value):
# Forbids List and Set for now
if isinstance(key, (Set, List)):
raise TypingError('{} as key is forbidden'.format(key))
if isinstance(value, (Set, List)):
raise TypingError('{} as value is forbidden'.format(value))
class DictType(IterableType):
"""Dictionary type
"""
def __init__(self, keyty, valty):
assert not isinstance(keyty, TypeRef)
assert not isinstance(valty, TypeRef)
keyty = unliteral(keyty)
valty = unliteral(valty)
if isinstance(keyty, (Optional, NoneType)):
fmt = 'Dict.key_type cannot be of type {}'
raise TypingError(fmt.format(keyty))
if isinstance(valty, (Optional, NoneType)):
fmt = 'Dict.value_type cannot be of type {}'
raise TypingError(fmt.format(valty))
_sentry_forbidden_types(keyty, valty)
self.key_type = keyty
self.value_type = valty
self.keyvalue_type = Tuple([keyty, valty])
name = '{}[{},{}]'.format(
self.__class__.__name__,
keyty,
valty,
)
super(DictType, self).__init__(name)
def is_precise(self):
return not any((
isinstance(self.key_type, Undefined),
isinstance(self.value_type, Undefined),
))
@property
def iterator_type(self):
return DictKeysIterableType(self).iterator_type
@classmethod
def refine(cls, keyty, valty):
"""Refine to a precise dictionary type
"""
res = cls(keyty, valty)
assert res.is_precise()
return res
def unify(self, typingctx, other):
"""
Unify this with the *other* dictionary.
"""
# If other is dict
if isinstance(other, DictType):
if not other.is_precise():
return self
class DictItemsIterableType(SimpleIterableType):
"""Dictionary iterable type for .items()
"""
def __init__(self, parent):
assert isinstance(parent, DictType)
self.parent = parent
self.yield_type = self.parent.keyvalue_type
name = "items[{}]".format(self.parent.name)
self.name = name
iterator_type = DictIteratorType(self)
super(DictItemsIterableType, self).__init__(name, iterator_type)
class DictKeysIterableType(SimpleIterableType):
"""Dictionary iterable type for .keys()
"""
def __init__(self, parent):
assert isinstance(parent, DictType)
self.parent = parent
self.yield_type = self.parent.key_type
name = "keys[{}]".format(self.parent.name)
self.name = name
iterator_type = DictIteratorType(self)
super(DictKeysIterableType, self).__init__(name, iterator_type)
class DictValuesIterableType(SimpleIterableType):
"""Dictionary iterable type for .values()
"""
def __init__(self, parent):
assert isinstance(parent, DictType)
self.parent = parent
self.yield_type = self.parent.value_type
name = "values[{}]".format(self.parent.name)
self.name = name
iterator_type = DictIteratorType(self)
super(DictValuesIterableType, self).__init__(name, iterator_type)
class DictIteratorType(SimpleIteratorType):
def __init__(self, iterable):
self.parent = iterable.parent
self.iterable = iterable
yield_type = iterable.yield_type
name = "iter[{}->{}],{}".format(iterable.parent, yield_type,
iterable.name)
super(DictIteratorType, self).__init__(name, yield_type)