Skip to content

Commit 6717701

Browse files
committed
add type hints to tests
1 parent 10c6ba7 commit 6717701

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

extras/lib/nb_helper.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,42 @@
11
import nbconvert
22
import nbformat
3+
from nbformat import NotebookNode
34
from .diffable import is_system_command
45

56

6-
def notebook_to_script(notebook):
7+
def notebook_to_script(notebook: NotebookNode):
78
exporter = nbconvert.exporters.PythonExporter
89
script, _ = nbconvert.exporters.export(exporter, notebook)
910
return script
1011

1112

12-
def read_notebook(notebook_path):
13+
def read_notebook(notebook_path: str) -> NotebookNode:
1314
return nbformat.read(notebook_path, as_version=4)
1415

1516

16-
def is_markdown(cell):
17+
def is_markdown(cell: NotebookNode):
1718
return cell.cell_type == "markdown"
1819

1920

20-
def is_h1(cell):
21+
def is_h1(cell: NotebookNode):
2122
return is_markdown(cell) and cell.source.startswith("# ")
2223

2324

24-
def is_magic(source):
25+
def is_magic(source: str):
2526
"""https://ipython.readthedocs.io/en/stable/interactive/tutorial.html#magic-functions"""
2627
return source.startswith("%%")
2728

2829

29-
def is_code_cell(cell):
30+
def is_code_cell(cell: NotebookNode):
3031
return cell.cell_type == "code"
3132

3233

33-
def is_python(cell):
34+
def is_python(cell: NotebookNode):
3435
source = cell.source
3536
return cell.cell_type == "code" and not (
3637
is_magic(source) or is_system_command(source)
3738
)
3839

3940

40-
def get_tags(cell):
41+
def get_tags(cell: NotebookNode):
4142
return cell.metadata.get("tags", [])

extras/test_lectures.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from glob import glob
22
import re
33

4+
from nbformat import NotebookNode
45
import pytest
56

67
from .lib.nb_helper import get_tags, is_code_cell, read_notebook
@@ -10,11 +11,11 @@
1011
lecture_notebooks.sort()
1112

1213

13-
def slide_type(cell):
14+
def slide_type(cell: NotebookNode):
1415
return cell.metadata.get("slideshow", {}).get("slide_type")
1516

1617

17-
def is_slide(cell):
18+
def is_slide(cell: NotebookNode):
1819
SLIDE_TYPES = ["slide", "subslide"]
1920
return slide_type(cell) in SLIDE_TYPES
2021

@@ -23,7 +24,7 @@ def is_exercise(source: str):
2324
return re.match("#+.+(demo|exercise)", source, re.IGNORECASE) is not None
2425

2526

26-
def num_slides(cells):
27+
def num_slides(cells: list[NotebookNode]):
2728
"""Return a weighted number of slides"""
2829

2930
slides = [cell for cell in cells if is_slide(cell)]
@@ -51,12 +52,13 @@ def test_num_slides(file):
5152
if file == "lecture_6.ipynb":
5253
pytest.xfail("The various pieces of the lecture can be scaled appropriately")
5354

54-
num_columbia = num_slides_without_tag(notebook.cells, "nyu-only")
55+
cells: list[NotebookNode] = notebook.cells
56+
num_columbia = num_slides_without_tag(cells, "nyu-only")
5557
print("Number of slides for Columbia: ", num_columbia)
5658
assert num_columbia >= 42, "Too few slides for Columbia"
5759
assert num_columbia <= 63, "Too many slides for Columbia"
5860

59-
num_nyu = num_slides_without_tag(notebook.cells, "columbia-only")
61+
num_nyu = num_slides_without_tag(cells, "columbia-only")
6062
print("Number of slides for NYU: ", num_nyu)
6163
assert num_nyu >= 39, "Too few slides for NYU"
6264
assert num_nyu <= 51, "Too many slides for NYU"

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# https://mypy.readthedocs.io/en/stable/config_file.html
2+
[tool.mypy]
3+
check_untyped_defs = true
4+
15
[tool.pytest.ini_options]
26
addopts = [
37
"--ignore=extras/autograder/",

0 commit comments

Comments
 (0)