|
2 | 2 |
|
3 | 3 | import collections |
4 | 4 | import typing |
| 5 | + |
| 6 | +from ..collections import frozendict |
5 | 7 | from ..typecheck import isoftype |
6 | 8 |
|
7 | 9 | def test(): |
@@ -110,6 +112,58 @@ def test(): |
110 | 112 | assert isoftype(d, typing.Deque[int]) |
111 | 113 | assert not isoftype(d, typing.Deque[float]) |
112 | 114 |
|
| 115 | + # typing.Mapping, typing.MutableMapping |
| 116 | + assert isoftype(frozendict({1: "foo", 2: "bar"}), typing.Mapping[int, str]) |
| 117 | + assert not isoftype(frozendict({1: "foo", 2: "bar"}), typing.MutableMapping[int, str]) |
| 118 | + assert not isoftype(frozendict({1: "foo", 2: "bar"}), typing.Mapping[str, str]) |
| 119 | + assert isoftype({1: "foo", 2: "bar"}, typing.MutableMapping[int, str]) |
| 120 | + assert not isoftype(42, typing.Mapping[int, str]) |
| 121 | + # empty mapping has no key/value types |
| 122 | + assert not isoftype({}, typing.MutableMapping[int, str]) |
| 123 | + assert not isoftype({}, typing.Mapping[int, str]) |
| 124 | + assert not isoftype(frozendict(), typing.Mapping[int, str]) |
| 125 | + |
| 126 | + # typing.Sequence, typing.MutableSequence |
| 127 | + assert not isoftype((), typing.Sequence[int]) # empty sequence has no element type |
| 128 | + assert isoftype((1, 2, 3), typing.Sequence[int]) |
| 129 | + assert not isoftype((1, 2, 3), typing.Sequence[float]) |
| 130 | + assert not isoftype((1, 2, 3), typing.MutableSequence[int]) |
| 131 | + assert isoftype([1, 2, 3], typing.Sequence[int]) |
| 132 | + assert isoftype([1, 2, 3], typing.MutableSequence[int]) |
| 133 | + assert not isoftype([], typing.MutableSequence[int]) # empty mutable sequence has no element type |
| 134 | + assert not isoftype([1, 2, 3], typing.MutableSequence[float]) |
| 135 | + assert not isoftype(42, typing.Sequence[int]) |
| 136 | + assert not isoftype(42, typing.MutableSequence[int]) |
| 137 | + |
| 138 | + # typing.AbstractSet, typing.MutableSet |
| 139 | + assert isoftype({1, 2, 3}, typing.AbstractSet[int]) |
| 140 | + assert isoftype({1, 2, 3}, typing.MutableSet[int]) |
| 141 | + assert not isoftype({1, 2, 3}, typing.AbstractSet[float]) |
| 142 | + assert isoftype(frozenset({1, 2, 3}), typing.AbstractSet[int]) |
| 143 | + assert not isoftype(frozenset({1, 2, 3}), typing.MutableSet[int]) |
| 144 | + assert not isoftype(42, typing.AbstractSet[int]) |
| 145 | + assert not isoftype(42, typing.MutableSet[int]) |
| 146 | + |
| 147 | + # one-trick ponies |
| 148 | + assert isoftype(3.14, typing.SupportsInt) |
| 149 | + # assert isoftype(3.14, typing.SupportsComplex) # ehm, WTF? |
| 150 | + assert isoftype(3.14, typing.SupportsAbs) |
| 151 | + assert isoftype(3.14, typing.SupportsRound) |
| 152 | + assert isoftype(42, typing.SupportsFloat) |
| 153 | + assert isoftype((1, 2, 3), typing.Sized) |
| 154 | + assert isoftype((1, 2, 3), typing.Hashable) |
| 155 | + assert isoftype([1, 2, 3], typing.Sized) |
| 156 | + assert not isoftype([1, 2, 3], typing.Hashable) |
| 157 | + |
| 158 | + # For these it's impossible, in general, to non-destructively check the |
| 159 | + # element type, so this run-time type checker ignores making that check. |
| 160 | + # It only checks that the value is an instance of the appropriate ABC. |
| 161 | + assert isoftype(iter([1, 2, 3]), typing.Iterator) |
| 162 | + assert isoftype([1, 2, 3], typing.Iterable) |
| 163 | + assert isoftype([1, 2, 3], typing.Reversible) |
| 164 | + assert isoftype([1, 2, 3], typing.Container) |
| 165 | + assert isoftype([1, 2, 3], typing.Collection) # Sized Iterable Container |
| 166 | + |
113 | 167 | print("All tests PASSED") |
114 | 168 |
|
115 | 169 | if __name__ == '__main__': |
|
0 commit comments