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
|
From d23ee2902e9e9aeffec86cbdb7a392d71be70861 Mon Sep 17 00:00:00 2001
From: slorg1 <slorg1@gmail.com>
Date: Tue, 16 Apr 2019 14:13:52 -0400
Subject: [PATCH] + upgrade to use collections.abc as needed for python 3.6+
--- a/src/contracts/library/map.py
+++ b/src/contracts/library/map.py
@@ -1,7 +1,11 @@
from ..interface import Contract, ContractNotRespected
from ..syntax import (W, contract_expression, O, S, add_contract, add_keyword,
Keyword)
-import collections
+
+try:
+ import collections.abc as collectionsAbc # python 3.6+
+except ImportError:
+ import collections as collectionsAbc
class Map(Contract):
@@ -13,7 +17,7 @@ def __init__(self, length=None, key_c=None, value_c=None, where=None):
self.value_c = value_c
def check_contract(self, context, value, silent):
- if not isinstance(value, collections.Mapping):
+ if not isinstance(value, collectionsAbc.Mapping):
error = 'Expected a Mapping, got %r.' % value.__class__.__name__
raise ContractNotRespected(contract=self, error=error,
value=value, context=context)
--- a/src/contracts/library/miscellaneous_aliases.py
+++ b/src/contracts/library/miscellaneous_aliases.py
@@ -1,12 +1,16 @@
-import collections
-
+try:
+ import collections.abc as collectionsAbc # python 3.6+
+except ImportError:
+ import collections as collectionsAbc
def ist(C):
+
def f(x):
f.__name__ = 'isinstance_of_%s' % C.__name__
if not isinstance(x, C):
raise ValueError('Value is not an instance of %s.' % C.__name__)
+
return f
@@ -14,33 +18,32 @@ def m_new_contract(name, f):
from contracts.library.extensions import CheckCallable
from contracts.library.extensions import Extension
Extension.registrar[name] = CheckCallable(f)
-
-m_new_contract('Container', ist(collections.Container))
-# todo: Iterable(x)
-m_new_contract('Iterable', ist(collections.Iterable))
-
-m_new_contract('Hashable', ist(collections.Hashable))
+m_new_contract('Container', ist(collectionsAbc.Container))
+# todo: Iterable(x)
+m_new_contract('Iterable', ist(collectionsAbc.Iterable))
+m_new_contract('Hashable', ist(collectionsAbc.Hashable))
-m_new_contract('Iterator', ist(collections.Iterator))
-m_new_contract('Sized', ist(collections.Sized))
-m_new_contract('Callable', ist(collections.Callable))
-m_new_contract('Sequence', ist(collections.Sequence))
-m_new_contract('Set', ist(collections.Set))
-m_new_contract('MutableSequence', ist(collections.MutableSequence))
-m_new_contract('MutableSet', ist(collections.MutableSet))
-m_new_contract('Mapping', ist(collections.Mapping))
-m_new_contract('MutableMapping', ist(collections.MutableMapping))
-#new_contract('MappingView', ist(collections.MappingView))
-#new_contract('ItemsView', ist(collections.ItemsView))
-#new_contract('ValuesView', ist(collections.ValuesView))
+m_new_contract('Iterator', ist(collectionsAbc.Iterator))
+m_new_contract('Sized', ist(collectionsAbc.Sized))
+m_new_contract('Callable', ist(collectionsAbc.Callable))
+m_new_contract('Sequence', ist(collectionsAbc.Sequence))
+m_new_contract('Set', ist(collectionsAbc.Set))
+m_new_contract('MutableSequence', ist(collectionsAbc.MutableSequence))
+m_new_contract('MutableSet', ist(collectionsAbc.MutableSet))
+m_new_contract('Mapping', ist(collectionsAbc.Mapping))
+m_new_contract('MutableMapping', ist(collectionsAbc.MutableMapping))
+# new_contract('MappingView', ist(collections.MappingView))
+# new_contract('ItemsView', ist(collections.ItemsView))
+# new_contract('ValuesView', ist(collections.ValuesView))
# Not a lambda to have better messages
-def is_None(x):
+def is_None(x):
return x is None
+
m_new_contract('None', is_None)
m_new_contract('NoneType', is_None)
--- a/src/contracts/library/seq.py
+++ b/src/contracts/library/seq.py
@@ -1,7 +1,12 @@
from ..interface import Contract, ContractNotRespected
from ..syntax import (add_contract, W, contract_expression, O, S, add_keyword,
Keyword)
-import collections
+
+try:
+ import collections.abc as collectionsAbc # python 3.6+
+except ImportError:
+ import collections as collectionsAbc
+
from past.builtins import xrange
try:
@@ -38,7 +43,7 @@ def check_contract(self, context, value, silent):
return
- if not isinstance(value, collections.Sequence):
+ if not isinstance(value, collectionsAbc.Sequence):
error = 'Expected a Sequence, got %r.' % value.__class__.__name__
raise ContractNotRespected(self, error, value, context)
--- a/src/contracts/library/sets.py
+++ b/src/contracts/library/sets.py
@@ -1,7 +1,10 @@
from ..interface import Contract, ContractNotRespected, describe_type
from ..syntax import (Keyword, O, S, W, add_contract, add_keyword,
contract_expression)
-import collections
+try:
+ import collections.abc as collectionsAbc # python 3.6+
+except ImportError:
+ import collections as collectionsAbc
class ASet(Contract):
@@ -13,7 +16,7 @@ def __init__(self, length_contract=None,
self.elements_contract = elements_contract
def check_contract(self, context, value, silent):
- if not isinstance(value, collections.Set):
+ if not isinstance(value, collectionsAbc.Set):
error = 'Expected a set, got %r.' % describe_type(value)
raise ContractNotRespected(self, error, value, context)
|