Skip to content

Commit 4351875

Browse files
committed
make the functional call checker support any function name
1 parent bcf252c commit 4351875

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

extras/scripts/hw_0_helper.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
from .nb_helper import *
55

66

7-
class InputChecker(ast.NodeVisitor):
8-
def __init__(self):
9-
self.num_inputs = 0
7+
# https://sadh.life/post/ast/
8+
class FuncCallChecker(ast.NodeVisitor):
9+
def __init__(self, func: str):
10+
self.func = func
11+
self.num_calls = 0
1012

1113
def visit_Call(self, node):
12-
if isinstance(node.func, ast.Name) and node.func.id == "input":
13-
self.num_inputs += 1
14+
if isinstance(node.func, ast.Name) and node.func.id == self.func:
15+
self.num_calls += 1
1416

1517

1618
@pytest.fixture()

hw_0.ipynb

+15-9
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@
207207
},
208208
{
209209
"cell_type": "code",
210-
"execution_count": 3,
210+
"execution_count": 2,
211211
"metadata": {},
212212
"outputs": [
213213
{
@@ -218,20 +218,26 @@
218218
"============================================= FAILURES =============================================\n",
219219
"\u001b[31m\u001b[1m_________________________________________ test_num_inputs __________________________________________\u001b[0m\n",
220220
"\n",
221-
"tree = <ast.Module object at 0x109a5a860>\n",
221+
"tree = <ast.Module object at 0x10e7da1a0>\n",
222222
"\n",
223223
" \u001b[94mdef\u001b[39;49;00m \u001b[92mtest_num_inputs\u001b[39;49;00m(tree):\n",
224-
" checker = InputChecker()\n",
224+
" checker = FuncCallChecker(\u001b[33m\"\u001b[39;49;00m\u001b[33minput\u001b[39;49;00m\u001b[33m\"\u001b[39;49;00m)\n",
225225
" checker.visit(tree)\n",
226226
" \n",
227-
"> \u001b[94massert\u001b[39;49;00m checker.num_inputs >= \u001b[94m8\u001b[39;49;00m, \u001b[33m\"\u001b[39;49;00m\u001b[33myou don\u001b[39;49;00m\u001b[33m'\u001b[39;49;00m\u001b[33mt have enough calls to input()\u001b[39;49;00m\u001b[33m\"\u001b[39;49;00m\n",
227+
"> \u001b[94massert\u001b[39;49;00m checker.num_calls >= \u001b[94m8\u001b[39;49;00m, \u001b[33m\"\u001b[39;49;00m\u001b[33myou don\u001b[39;49;00m\u001b[33m'\u001b[39;49;00m\u001b[33mt have enough calls to input()\u001b[39;49;00m\u001b[33m\"\u001b[39;49;00m\n",
228228
"\u001b[1m\u001b[31mE AssertionError: you don't have enough calls to input()\u001b[0m\n",
229229
"\u001b[1m\u001b[31mE assert 2 >= 8\u001b[0m\n",
230-
"\u001b[1m\u001b[31mE + where 2 = <extras.scripts.hw_0_helper.InputChecker object at 0x10acc9ba0>.num_inputs\u001b[0m\n",
230+
"\u001b[1m\u001b[31mE + where 2 = <extras.scripts.hw_0_helper.FuncCallChecker object at 0x10e7d8df0>.num_calls\u001b[0m\n",
231+
"\n",
232+
"\u001b[1m\u001b[31m/var/folders/kg/1ys0dccx4237f5wsd_w10dt80000gn/T/ipykernel_68325/4145485001.py\u001b[0m:8: AssertionError\n",
233+
"\u001b[33m========================================= warnings summary =========================================\u001b[0m\n",
234+
"tmpc439hpt9.py::test_num_inputs\n",
235+
" /usr/local/Caskroom/miniconda/base/envs/python-public-policy/lib/python3.10/site-packages/IPython/core/inputsplitter.py:21: DeprecationWarning: IPython.core.inputsplitter is deprecated since IPython 7 in favor of `IPython.core.inputtransformer2`\n",
236+
" warn('IPython.core.inputsplitter is deprecated since IPython 7 in favor of `IPython.core.inputtransformer2`',\n",
231237
"\n",
232-
"\u001b[1m\u001b[31m/var/folders/kg/1ys0dccx4237f5wsd_w10dt80000gn/T/ipykernel_67029/2901258849.py\u001b[0m:8: AssertionError\n",
238+
"-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html\n",
233239
"===================================== short test summary info ======================================\n",
234-
"FAILED tmpyoyk9z3m.py::test_num_inputs - AssertionError: you don't have enough calls to input()\n"
240+
"FAILED tmpc439hpt9.py::test_num_inputs - AssertionError: you don't have enough calls to input()\n"
235241
]
236242
}
237243
],
@@ -242,10 +248,10 @@
242248
"\n",
243249
"\n",
244250
"def test_num_inputs(tree):\n",
245-
" checker = InputChecker()\n",
251+
" checker = FuncCallChecker(\"input\")\n",
246252
" checker.visit(tree)\n",
247253
"\n",
248-
" assert checker.num_inputs >= 8, \"you don't have enough calls to input()\""
254+
" assert checker.num_calls >= 8, \"you don't have enough calls to input()\""
249255
]
250256
},
251257
{

0 commit comments

Comments
 (0)