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
|
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "d42a9120-8d70-4b10-8d6c-b59692fd03ad",
"metadata": {},
"outputs": [],
"source": [
"%load_ext autoreload\n",
"%autoreload 2"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "11cb390f-b0f4-438d-b524-db7242d22ee9",
"metadata": {},
"outputs": [],
"source": [
"#| default_exp: test"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dd03783f-252c-40f5-b92f-0f3f9278ce09",
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"import ast,copy,contextlib,inspect,textwrap,traceback\n",
"from typing import Any,Generator\n",
"from dataclasses import dataclass"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2cf5d527",
"metadata": {},
"outputs": [],
"source": [
"from nb.core import pubattr"
]
},
{
"cell_type": "markdown",
"id": "5b8abf6a-c94a-43e5-a1d1-81b1a1d64656",
"metadata": {},
"source": [
"# Testing\n",
"> Utilities for testing within notebooks"
]
},
{
"cell_type": "markdown",
"id": "12e34b1f",
"metadata": {},
"source": [
"## Re-constructing source code and program state during runtime"
]
},
{
"cell_type": "markdown",
"id": "93a3d5fe",
"metadata": {},
"source": [
"When writing test libraries, it can be incredibly useful to understand what the call site source code looks like.\n",
"\n",
"For instance, let's say you call `equal(x in [1, 2, 3], should_exist)`. When we're in `equal`, the arguments have already been evaluated and we simply have two booleans as arguments, each `True` or `False`. If this test fails, it likely isn't *that* helpful to see `False != True`. \n",
"\n",
"Instead, it is likely much *more* helpful to see something like `x in [1, 2, 3] != should_exist` where `x = 4` and `should_exist=True`."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a6c67919",
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"def get_call_args(frames_up: int = 1) -> tuple[list[str], dict[str,Any]]:\n",
" \"Get the source code expressions of the caller's arguments.\"\n",
" fm_info = inspect.getouterframes(inspect.currentframe())[frames_up+1]\n",
" src, _ = inspect.getsourcelines(fm_info.frame)\n",
" srcl = src[fm_info.positions.lineno - 1:fm_info.positions.end_lineno]\n",
" if len(srcl) == 0:\n",
" raise RuntimeError(\n",
" \"`get_call_args` is designed to be called from within a function, \" +\n",
" \"not at the top-level. If you want to call it at the top level, pass `frames_up=0`.\"\n",
" )\n",
" elif len(srcl) == 1:\n",
" fn_src = srcl[0][fm_info.positions.col_offset:fm_info.positions.end_col_offset]\n",
" else:\n",
" fn_src = \"\".join(\n",
" [srcl[0][fm_info.positions.col_offset:]] +\n",
" srcl[1:-1] +\n",
" [srcl[-1][:fm_info.positions.end_col_offset]]\n",
" )\n",
" call_node = ast.parse(fn_src).body[0].value\n",
" args_src = [ast.unparse(arg) for arg in call_node.args]\n",
" kwargs_src = {kw.arg: ast.unparse(kw.value) for kw in call_node.keywords}\n",
" return args_src, kwargs_src"
]
},
{
"cell_type": "markdown",
"id": "487c29d9",
"metadata": {},
"source": [
"Because `get_call_args()` is expected to be used from within a function, it generally will not work properly by default if you call it from the top-level."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bbe2859c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"`get_call_args` is designed to be called from within a function, not at the top-level. If you want to call it at the top level, pass `frames_up=0`.\n"
]
}
],
"source": [
"try: get_call_args(); assert False, \"should have raised\"\n",
"except RuntimeError as e: print(e)"
]
},
{
"cell_type": "markdown",
"id": "ce788841",
"metadata": {},
"source": [
"However, you _can_ have it work if you pass `frames_up=0` (i.e. the current frame we're calling `get_call_args()` from rather than the caller frame)."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1f3b1abb",
"metadata": {},
"outputs": [],
"source": [
"args, kwargs = get_call_args(frames_up=0)\n",
"assert args == [], f\"Got {args}\"\n",
"assert kwargs == {\"frames_up\": \"0\"}, f\"Got {kwargs}\""
]
},
{
"cell_type": "markdown",
"id": "5a48fa09",
"metadata": {},
"source": [
"When you're in a function, it works as expected."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b7730389",
"metadata": {},
"outputs": [],
"source": [
"def f(a, b=1):\n",
" return get_call_args()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "19890aa0",
"metadata": {},
"outputs": [],
"source": [
"args, kwargs = f(1+1,b=max(10, 20))\n",
"assert args == [\"1 + 1\"], f\"Got {args}\"\n",
"assert kwargs == {\"b\": \"max(10, 20)\"}, f\"Got {kwargs}\""
]
},
{
"cell_type": "markdown",
"id": "b9aba848",
"metadata": {},
"source": [
"It also works with multi-line function calls.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b8b7195c",
"metadata": {},
"outputs": [],
"source": [
"args, kwargs = f(\n",
" 1,\n",
" b=10,\n",
" \n",
" \n",
")\n",
"assert args == [\"1\"], f\"Got {args}\"\n",
"assert kwargs == {\"b\": \"10\"}, f\"Got {kwargs}\""
]
},
{
"cell_type": "markdown",
"id": "f93a8882",
"metadata": {},
"source": [
"If you use it with a function within a function, you'll get the arguments for the inner function.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ce0666f5",
"metadata": {},
"outputs": [],
"source": [
"def nested_f(a, b=1):\n",
" def inner_f(c, d, e=1):\n",
" return get_call_args()\n",
" return inner_f(5 * 5, 10 / 10)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3555f6cf",
"metadata": {},
"outputs": [],
"source": [
"args, kwargs = nested_f(1)\n",
"assert args == [\"5 * 5\", \"10 / 10\"], f\"Got {args}\"\n",
"assert kwargs == {}, f\"Got {kwargs}\""
]
},
{
"cell_type": "markdown",
"id": "97d47759",
"metadata": {},
"source": [
"However, you can still get the outer function arguments with the `frames_up` keyword argument."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2a42fc2b",
"metadata": {},
"outputs": [],
"source": [
"def nested_f(a, b=1):\n",
" def inner_f(c, d, e=1):\n",
" return get_call_args(frames_up=2)\n",
" return inner_f(5 * 5, 10 / 10)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b3687819",
"metadata": {},
"outputs": [],
"source": [
"args, kwargs = nested_f(1)\n",
"assert args == [\"1\"], f\"Got {args}\"\n",
"assert kwargs == {}, f\"Got {kwargs}\""
]
},
{
"cell_type": "markdown",
"id": "5d991fd4",
"metadata": {},
"source": [
"## Assertions"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "623a480c",
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"@dataclass\n",
"class ExceptionInfo:\n",
" type_: type[Exception] | None = None\n",
" value: Exception | None = None\n",
" traceback: str | None = None"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0ad5fcef-f04d-469d-8ad1-94d3a98694e4",
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"@contextlib.contextmanager\n",
"def raises(\n",
" exc: type[Exception] = Exception, # The exception type to check is raised.\n",
" contains: str | None = None, # Optionally, check whether the raised exception contains this message.\n",
") -> Generator[ExceptionInfo, None, None]:\n",
" \"Asserts that the body of the context manager raises `exc`.\"\n",
" raised = True\n",
" try:\n",
" info = ExceptionInfo()\n",
" yield info\n",
" raised = False\n",
" except exc as e:\n",
" info.type_ = type(e)\n",
" info.value = e\n",
" info.traceback = traceback.format_tb(e.__traceback__)\n",
" if contains and contains not in str(e):\n",
" raise AssertionError(f\"Raised {e} but message does not contain {contains}\") from e\n",
" if not raised:\n",
" raise AssertionError(f\"Did not raise {e}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7c478098-7a09-403c-a62b-3c34cf0f7bab",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ExceptionInfo(type_=<class 'Exception'>, value=Exception('abc'), traceback=[' File \"/tmp/ipykernel_572/3259713399.py\", line 10, in raises\\n yield info\\n', ' File \"/tmp/ipykernel_572/3922684010.py\", line 2, in <module>\\n raise Exception(\"abc\")\\n'])\n"
]
}
],
"source": [
"with raises() as e:\n",
" raise Exception(\"abc\")\n",
"print(e)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2b503871-3f39-4013-a7ae-f5143af3138e",
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"def equal(a, b):\n",
" \"Assert that a == b.\"\n",
" args_src, _ = get_call_args()\n",
" if a != b:\n",
" msg = (\n",
" f\"FAIL: {args_src[0]} != {args_src[1]}\\n\" +\n",
" ' ' * (len(AssertionError.__name__)+2) +\n",
" f\" {a!r} != {b!r}\"\n",
" )\n",
" raise AssertionError(msg)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cc7872bb",
"metadata": {},
"outputs": [],
"source": [
"with raises(exc=AssertionError) as e:\n",
" equal(1, 2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7a1393a7-6a89-4c5d-a8a7-51e0fd46d190",
"metadata": {},
"outputs": [],
"source": [
"import nb; nb.export_nb('test.ipynb', src_dir='../nb/')"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}
|