-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_dispatch.py
More file actions
315 lines (273 loc) · 12.6 KB
/
test_dispatch.py
File metadata and controls
315 lines (273 loc) · 12.6 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
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
# -*- coding: utf-8; -*-
from ..syntax import macros, test, test_raises, fail, the # noqa: F401
from ..test.fixtures import session, testset, returns_normally
import typing
from ..fun import curry
from ..dispatch import generic, generic_for, typed
@generic
def zorblify(x: int, y: int):
return 2 * x + y
@generic
def zorblify(x: str, y: int): # noqa: F811, registered as a method of the same generic function.
# Because dispatching occurs on both arguments, this method is not reached by the tests.
fail["this method should not be reached by the tests"] # pragma: no cover
@generic
def zorblify(x: str, y: float): # noqa: F811
return f"{x[::-1]} {y}"
# TODO: def zorblify(x: int, *args: typing.Sequence[str]):
# @generic can also be used to simplify argument handling code in functions
# where the role of an argument in a particular position changes depending on
# the number of arguments (like the range() builtin).
#
# The pattern is that the generic function canonizes the arguments,
# and then calls the actual implementation, which always gets them
# in the same format.
@generic
def example(stop: int):
return _example_impl(0, 1, stop)
@generic
def example(start: int, stop: int): # noqa: F811
return _example_impl(start, 1, stop)
@generic
def example(start: int, step: int, stop: int): # noqa: F811
return _example_impl(start, step, stop)
def _example_impl(start, step, stop): # no @generic!
return start, step, stop
# shorter, same effect
@generic
def example2(start: int, stop: int):
return example2(start, 1, stop) # just call the method that has the implementation
@generic
def example2(start: int, step: int, stop: int): # noqa: F811
return start, step, stop
# varargs are supported via `typing.Tuple`
@generic
def gargle(*args: typing.Tuple[int, ...]): # any number of ints
return "int"
@generic
def gargle(*args: typing.Tuple[float, ...]): # any number of floats # noqa: F811
return "float"
@generic
def gargle(*args: typing.Tuple[int, float, str]): # three args, matching the given types # noqa: F811
return "int, float, str"
# One-method pony, which automatically enforces argument types.
# The type specification may use features from the `typing` stdlib module.
@typed
def blubnify(x: int, y: float):
return x * y
@typed
def jack(x: typing.Union[int, str]): # look, it's the union-jack!
return x
def runtests():
with testset("@generic"):
test[zorblify(17, 8) == 42]
test[zorblify(17, y=8) == 42] # can also use named arguments
test[zorblify(y=8, x=17) == 42]
test[zorblify("tac", 1.0) == "cat 1.0"]
test[zorblify(y=1.0, x="tac") == "cat 1.0"]
test_raises[TypeError, zorblify(1.0, 2.0)] # there's no zorblify(float, float)
test[example(10) == (0, 1, 10)]
test[example(2, 10) == (2, 1, 10)]
test[example(2, 3, 10) == (2, 3, 10)]
test[example2(1, 5) == (1, 1, 5)]
test[example2(1, 1, 5) == (1, 1, 5)]
test[example2(1, 2, 5) == (1, 2, 5)]
test[gargle(1, 2, 3, 4, 5) == "int"]
test[gargle(2.71828, 3.14159) == "float"]
test[gargle(42, 6.022e23, "hello") == "int, float, str"]
test[gargle(1, 2, 3) == "int"] # as many as in the [int, float, str] case
with testset("@generic_for"):
@generic
def f1(x: typing.Any):
return False
@generic_for(f1)
def f2(x: int):
return x
test[f1("hello") is False]
test[f1(42) == 42]
def f3(x: typing.Any): # not @generic!
return False
with test_raises(TypeError, "should not be able to @generic_for a non-generic function"):
@generic_for(f3)
def f4(x: int):
return x
with testset("@generic integration with OOP"):
class TestTarget:
myname = "Test target"
def __init__(self, a):
self.a = a
# The OOP method type modifier (`@staticmethod` or `@classmethod`),
# if any, goes on the outside.
@staticmethod
@generic
def staticmeth(x: str):
return " ".join(2 * [x])
@staticmethod
@generic
def staticmeth(x: int): # noqa: F811
return 2 * x
# `cls` does not need a type annotation.
@classmethod
@generic
def clsmeth(cls, x: str):
return f"{cls.myname} says: {' '.join(2 * [x])}"
# be careful, generic can't check that all variants are a @classmethod!
@classmethod
@generic
def clsmeth(cls, x: int): # noqa: F811
return f"{cls.myname} computes: {2 * x}"
# `self` does not need a type annotation.
@generic
def instmeth(self, x: str):
return " ".join(self.a * [x])
@generic
def instmeth(self, x: int): # noqa: F811
return self.a * x
@typed
def checked(self, x: int):
pass # pragma: no cover
tt = TestTarget(3)
test[tt.instmeth("hi") == "hi hi hi"]
test[tt.instmeth(21) == 63]
test[tt.clsmeth("hi") == "Test target says: hi hi"] # call via instance
test[tt.clsmeth(21) == "Test target computes: 42"]
test[TestTarget.clsmeth("hi") == "Test target says: hi hi"] # call via class
test[TestTarget.clsmeth(21) == "Test target computes: 42"]
test[tt.staticmeth("hi") == "hi hi"] # call via instance
test[tt.staticmeth(21) == 42]
test[TestTarget.staticmeth("hi") == "hi hi"] # call via class
test[TestTarget.staticmeth(21) == 42]
test[returns_normally(tt.checked(42))]
test_raises[TypeError, tt.checked("hi")]
# In OOP, `@generic` dispatches across the MRO.
#
# The classes are tried in MRO order, matching all methods in a single class
# before moving on to the next one.
with testset("@generic integration with OOP, with inheritance"):
class BabyTestTarget(TestTarget): # child class, get it?
@staticmethod
@generic
def staticmeth(x: float):
return f"float {2 * x}"
@classmethod
@generic
def clsmeth(cls, x: float):
return f"{cls.myname} floats: {2 * x}"
@generic
def instmeth(self, x: float):
return f"floating with {self.a * x}"
tt2 = BabyTestTarget(3)
# the new generic-function methods become available, installed on the OOP method
test[tt2.instmeth(3.14) == "floating with 9.42"]
# old generic-function methods registered by the ancestor remain available
test[tt2.instmeth("hi") == "hi hi hi"]
test[tt2.instmeth(21) == 63]
test[tt2.clsmeth(3.14) == "Test target floats: 6.28"]
test[tt2.clsmeth("hi") == "Test target says: hi hi"]
test[tt2.clsmeth(21) == "Test target computes: 42"]
test[BabyTestTarget.clsmeth(3.14) == "Test target floats: 6.28"]
test[BabyTestTarget.clsmeth("hi") == "Test target says: hi hi"]
test[BabyTestTarget.clsmeth(21) == "Test target computes: 42"]
# `@generic` on *static methods* **does not** support MRO lookup.
# Basically, one of the roles of `cls` or `self` is to define the MRO;
# a static method doesn't have that.
#
# See discussions on interaction between `@staticmethod` and `super` in Python:
# https://bugs.python.org/issue31118
# https://stackoverflow.com/questions/26788214/super-and-staticmethod-interaction/26807879
test[tt2.staticmeth(3.14) == "float 6.28"] # this is available on `tt2`
test_raises[TypeError, tt2.staticmeth("hi")] # but this is not (no MRO)
test_raises[TypeError, tt2.staticmeth(21)]
test[BabyTestTarget.staticmeth(3.14) == "float 6.28"] # available on `BabyTestTarget`
test_raises[TypeError, BabyTestTarget.staticmeth("hi")] # not available (no MRO)
test_raises[TypeError, BabyTestTarget.staticmeth(21)]
test_raises[TypeError, tt2.clsmeth(None)] # not defined for NoneType
with testset("@typed"):
test[blubnify(2, 21.0) == 42]
test_raises[TypeError, blubnify(2, 3)] # blubnify only accepts (int, float)
test[not hasattr(blubnify, "register")] # and no more methods can be registered on it
test[jack(42) == 42]
test[jack("foo") == "foo"]
test_raises[TypeError, jack(3.14)] # jack only accepts int or str
with testset("error cases"):
with test_raises(TypeError, "@typed should only accept a single method"):
@typed
def errorcase1(x: int):
pass # pragma: no cover
@typed
def errorcase1(x: str): # noqa: F811
pass # pragma: no cover
with test_raises(TypeError, "@generic should complain about missing type annotations"):
@generic
def errorcase2(x):
pass # pragma: no cover
with testset("@typed integration with curry"):
f = curry(blubnify, 2)
test[callable(the[f])]
test[f(21.0) == 42]
# But be careful:
f = curry(blubnify, 2.0) # wrong argument type; error not triggered yet
test[callable(the[f])]
test_raises[TypeError, f(21.0) == 42] # error will occur now, when the call is triggered
with testset("holy traits in Python with @generic"):
# Note we won't get the performance benefits of Julia, because this is a
# purely run-time implementation.
#
# For what this is about, see:
# https://ahsmart.com/pub/holy-traits-design-patterns-and-best-practice-book/
# https://www.juliabloggers.com/the-emergent-features-of-julialang-part-ii-traits/
# The traits, orthogonal to the type hierarchy of the actual data.
# Here we have just one.
class FlippabilityTrait:
pass
class IsFlippable(FlippabilityTrait):
pass
class IsNotFlippable(FlippabilityTrait):
pass
# Mapping of concrete types to traits. This is the extensible part.
@generic
def flippable(x: typing.Any): # default
raise NotImplementedError(f"`flippable` trait not registered for any type specification matching {type(x)}")
# Since these are in the same lexical scope as the original definition of the
# generic function `flippable`, we could do this using `@generic`, but
# later extensions (which are the whole point of traits) will need to specify
# on which function the new methods are to be registered, using `@generic_for`.
# So let's do that to show how it's done.
@generic_for(flippable)
def flippable(x: str): # noqa: F811
return IsFlippable()
@generic_for(flippable)
def flippable(x: int): # noqa: F811
return IsNotFlippable()
# Trait-based dispatcher for the operation `flip`, implemented as a
# generic function. The dispatcher maps the concrete type of `x` to
# the desired trait (relevant to that particular operation), while
# passing the value `x` itself along.
#
# The "flip" operation is just a silly example of something that is
# applicable to "flippable" objects but not to "nonflippable" ones.
@generic
def flip(x: typing.Any):
return flip(flippable(x), x)
# Implementation of `flip`. Same comment about `@generic_for` as above.
#
# Here we provide one implementation for "flippable" objects and another one
# for "nonflippable" objects. Note this dispatches regardless of the actual
# data type of `x`, and particularly, does not care which class hierarchy
# `type(x)` belongs to, as long as it has been registered to the relevant trait.
#
# We could also add methods for specific types if needed. Note this is not
# Julia, so the first matching definition wins, instead of the most specific
# one.
@generic_for(flip)
def flip(traitvalue: IsFlippable, x: typing.Any): # noqa: F811
return x[::-1]
@generic_for(flip)
def flip(traitvalue: IsNotFlippable, x: typing.Any): # noqa: F811
raise TypeError(f"{repr(x)} is IsNotFlippable")
test[flip("abc") == "cba"]
test_raises[TypeError, flip(42), "int should not be flippable"]
test_raises[NotImplementedError, flip(2.0), "float should not be registered for the flippable trait"]
if __name__ == '__main__': # pragma: no cover
with session(__file__):
runtests()