-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_typecheck.py
More file actions
208 lines (176 loc) · 9.44 KB
/
test_typecheck.py
File metadata and controls
208 lines (176 loc) · 9.44 KB
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
# -*- coding: utf-8; -*-
from ..syntax import macros, test, test_raises, warn # noqa: F401
from ..test.fixtures import session, testset
import collections
import typing
from ..collections import frozendict
from ..typecheck import isoftype
def runtests():
with testset("concrete type"):
test[isoftype(17, int)]
test[isoftype("hello", str)]
test[not isoftype(17, str)]
with testset("callable"):
test[isoftype(lambda: ..., typing.Callable)]
test[not isoftype("blah", typing.Callable)]
with testset("typing.NewType"):
UserId = typing.NewType("UserId", int)
test[isoftype(UserId(42), UserId)]
# Note limitation: since NewType types discard their type information at
# run time, any instance of the underlying actual run-time type will match.
test[isoftype(42, UserId)]
# typing.Any (i.e. explicitly say "don't care")
with testset("typing.Any"):
test[isoftype(5, typing.Any)]
test[isoftype("something", typing.Any)]
test[isoftype(lambda: ..., typing.Any)]
# TypeVar, bare; a named type, but behaves like Any.
with testset("typing.TypeVar (bare; like a named Any)"):
X = typing.TypeVar("X")
test[isoftype(3.14, X)]
test[isoftype("anything", X)]
test[isoftype(lambda: ..., X)]
# TypeVar, with arguments; matches only the specs in the constraints.
with testset("typing.TypeVar (with arguments; constrained)"):
Number = typing.TypeVar("Number", int, float, complex)
test[isoftype(31337, Number)]
test[isoftype(3.14159, Number)]
test[isoftype(1 + 2j, Number)]
test[not isoftype("blargh", Number)]
Silly = typing.TypeVar("Silly", int, typing.Callable)
test[isoftype(123456, Silly)]
test[isoftype(lambda: ..., Silly)]
test[not isoftype(3.14, Silly)]
with testset("typing.Union"):
test[isoftype(23, typing.Union[int, str])]
test[isoftype("hello again", typing.Union[int, str])]
test[not isoftype(3.14, typing.Union[int, str])]
# The bare Union (no args) is empty, it has no types in it, so no value can match.
test[not isoftype(23, typing.Union)]
test[not isoftype("hello again", typing.Union)]
test[not isoftype(3.14, typing.Union)]
test[not isoftype(None, typing.Union)]
with testset("typing.Optional"):
test[isoftype(None, typing.Optional[int])]
test[isoftype(1337, typing.Optional[int])]
test[not isoftype(3.14, typing.Optional[int])]
with testset("typing.Tuple"):
test[isoftype((1, 2, 3), typing.Tuple)]
test[isoftype((1, 2, 3), typing.Tuple[int, ...])]
test[isoftype((1, 2.1, "footgun"), typing.Tuple[int, float, str])]
test[not isoftype((1.1, 2.2, 3.3), typing.Tuple[int, ...])]
test[not isoftype((1, 2.1, 9001), typing.Tuple[int, float, str])]
test[isoftype((), typing.Tuple)]
test[not isoftype((), typing.Tuple[int, ...])] # empty tuple has no element type
test[not isoftype((), typing.Tuple[float, ...])]
test[not isoftype([1, 2, 3], typing.Tuple)] # list is not tuple
with testset("typing.List"):
test[isoftype([1, 2, 3], typing.List[int])]
test[not isoftype([1, 2, 3], typing.List[float])]
test[not isoftype((1, 2, 3), typing.List[int])] # it's a tuple, silly
test[not isoftype(42, typing.List[int])] # try something that's not even a collection
with testset("typing.Set"):
test[isoftype({"cat", "fox", "python"}, typing.Set[str])]
test[not isoftype({1, 2, 3}, typing.Set[str])]
test[not isoftype(42, typing.Set[str])]
with testset("typing.FrozenSet"):
test[isoftype(frozenset({"cat", "fox", "python"}), typing.FrozenSet[str])]
test[not isoftype(frozenset({1, 2, 3}), typing.FrozenSet[str])]
test[not isoftype(42, typing.FrozenSet[str])]
with testset("typing.Dict"):
test[isoftype({17: "cat", 23: "fox", 42: "python"}, typing.Dict[int, str])]
test[not isoftype({"bar": "foo", "tavern": "a place"}, typing.Dict[int, str])]
test[not isoftype(42, typing.Dict[int, str])]
# no type arguments: any key/value types ok (consistent with Python 3.7+)
test[isoftype({"cat": "animal", "pi": 3.14159, 2.71828: "e"}, typing.Dict)]
# type alias (at run time, this is just an assignment)
with testset("type alias"):
U = typing.Union[int, str]
test[isoftype(42, U)]
test[isoftype("hello yet again", U)]
test[not isoftype(3.14, U)]
# typing.Text (in Python 3, alias of str)
with testset("typing.Text"):
test[isoftype("hi", typing.Text)]
test[not isoftype(42, typing.Text)]
with testset("typing.AnyStr"):
test[isoftype("hi", typing.AnyStr)]
test[isoftype(b"hi", typing.AnyStr)]
test[not isoftype(42, typing.AnyStr)]
with testset("typing.ByteString (bytes, bytearray, memoryview)"):
test[isoftype(b"hi", typing.ByteString)]
with testset("collections.deque"):
d = collections.deque()
test[not isoftype(d, typing.Deque[int])] # empty deque has no element type
d.append(42)
test[isoftype(d, typing.Deque[int])]
test[not isoftype(d, typing.Deque[float])]
with testset("typing.Mapping, typing.MutableMapping"):
test[isoftype(frozendict({1: "foo", 2: "bar"}), typing.Mapping[int, str])]
test[not isoftype(frozendict({1: "foo", 2: "bar"}), typing.MutableMapping[int, str])]
test[not isoftype(frozendict({1: "foo", 2: "bar"}), typing.Mapping[str, str])]
test[isoftype({1: "foo", 2: "bar"}, typing.MutableMapping[int, str])]
test[not isoftype(42, typing.Mapping[int, str])]
# empty mapping has no key/value types
test[not isoftype({}, typing.MutableMapping[int, str])]
test[not isoftype({}, typing.Mapping[int, str])]
test[not isoftype(frozendict(), typing.Mapping[int, str])]
with testset("typing.Sequence, typing.MutableSequence"):
test[not isoftype((), typing.Sequence[int])] # empty sequence has no element type
test[isoftype((1, 2, 3), typing.Sequence[int])]
test[not isoftype((1, 2, 3), typing.Sequence[float])]
test[not isoftype((1, 2, 3), typing.MutableSequence[int])]
test[isoftype([1, 2, 3], typing.Sequence[int])]
test[isoftype([1, 2, 3], typing.MutableSequence[int])]
test[not isoftype([], typing.MutableSequence[int])] # empty mutable sequence has no element type
test[not isoftype([1, 2, 3], typing.MutableSequence[float])]
test[not isoftype(42, typing.Sequence[int])]
test[not isoftype(42, typing.MutableSequence[int])]
with testset("typing.AbstractSet, typing.MutableSet"):
test[isoftype({1, 2, 3}, typing.AbstractSet[int])]
test[isoftype({1, 2, 3}, typing.MutableSet[int])]
test[not isoftype({1, 2, 3}, typing.AbstractSet[float])]
test[isoftype(frozenset({1, 2, 3}), typing.AbstractSet[int])]
test[not isoftype(frozenset({1, 2, 3}), typing.MutableSet[int])]
test[not isoftype(42, typing.AbstractSet[int])]
test[not isoftype(42, typing.MutableSet[int])]
with testset("one-trick pony ABCs"):
test[isoftype(3.14, typing.SupportsInt)]
# test[isoftype(3.14, typing.SupportsComplex)] # ehm, WTF?
test[isoftype(3.14, typing.SupportsAbs)]
test[isoftype(3.14, typing.SupportsRound)]
test[isoftype(42, typing.SupportsFloat)]
test[isoftype((1, 2, 3), typing.Sized)]
test[isoftype((1, 2, 3), typing.Hashable)]
test[isoftype([1, 2, 3], typing.Sized)]
test[not isoftype([1, 2, 3], typing.Hashable)]
# TODO: test SupportsComplex, SupportsBytes
# For these it's impossible, in general, to non-destructively check the
# element type, so this run-time type checker ignores making that check.
# It only checks that the value is an instance of the appropriate ABC.
test[isoftype(iter([1, 2, 3]), typing.Iterator)]
test[isoftype([1, 2, 3], typing.Iterable)]
test[isoftype([1, 2, 3], typing.Reversible)]
test[isoftype([1, 2, 3], typing.Container)]
if hasattr(typing, "Collection"): # Python 3.6+
test[isoftype([1, 2, 3], typing.Collection)] # Sized Iterable Container
with testset("typing.KeysView, typing.ValuesView, typing.ItemsView"):
d = {17: "cat", 23: "fox", 42: "python"}
test[isoftype(d.keys(), typing.KeysView[int])]
test[isoftype(d.values(), typing.ValuesView[str])]
test[isoftype(d.items(), typing.ItemsView[int, str])]
# no type arguments: any key/value types ok (consistent with Python 3.7+)
test[isoftype(d.keys(), typing.KeysView)]
test[isoftype(d.values(), typing.ValuesView)]
test[isoftype(d.items(), typing.ItemsView)]
test[not isoftype("hello", typing.ItemsView)]
test[not isoftype({}.items(), typing.ItemsView[int, str])] # empty dict has no key, value types
# TODO: test MappingView
# OTOH, do we need to? It seems it's just an ABC for the other three.
# https://docs.python.org/3/library/typing.html#typing.MappingView
# https://docs.python.org/3/library/collections.abc.html#collections.abc.MappingView
# https://docs.python.org/3/glossary.html#term-dictionary-view
# https://docs.python.org/3/library/stdtypes.html#dict-views
if __name__ == '__main__': # pragma: no cover
with session(__file__):
runtests()