Skip to content

Commit 5330c1d

Browse files
committed
Update typing for nbgraderformat
1 parent 8bb7244 commit 5330c1d

File tree

4 files changed

+37
-31
lines changed

4 files changed

+37
-31
lines changed

nbgrader/nbgraderformat/common.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def __init__(self) -> None:
3232
with open(os.path.join(root, "v{:d}.json".format(self.schema_version)), "r") as fh:
3333
self.schema = json.loads(fh.read())
3434

35-
def _remove_extra_keys(self, cell):
35+
def _remove_extra_keys(self, cell: NotebookNode) -> None:
3636
meta = cell.metadata['nbgrader']
3737
allowed = set(self.schema["properties"].keys())
3838
keys = set(meta.keys()) - allowed
@@ -41,12 +41,12 @@ def _remove_extra_keys(self, cell):
4141
for key in keys:
4242
del meta[key]
4343

44-
def upgrade_notebook_metadata(self, nb):
44+
def upgrade_notebook_metadata(self, nb: NotebookNode) -> NotebookNode:
4545
for cell in nb.cells:
4646
self.upgrade_cell_metadata(cell)
4747
return nb
4848

49-
def upgrade_cell_metadata(self, cell): # pragma: no cover
49+
def upgrade_cell_metadata(self, cell: NotebookNode) -> NotebookNode: # pragma: no cover
5050
raise NotImplementedError("this method must be implemented by subclasses")
5151

5252
def validate_cell(self, cell: NotebookNode) -> None:

nbgrader/nbgraderformat/v1.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from nbformat import read as _read, reads as _reads
22
from nbformat import write as _write, writes as _writes
3+
from nbformat.notebooknode import NotebookNode
34
from .common import BaseMetadataValidator, ValidationError
5+
import typing
46

57

68
class MetadataValidatorV1(BaseMetadataValidator):
@@ -10,7 +12,7 @@ class MetadataValidatorV1(BaseMetadataValidator):
1012
def __init__(self) -> None:
1113
super().__init__()
1214

13-
def _upgrade_v0_to_v1(self, cell):
15+
def _upgrade_v0_to_v1(self, cell: NotebookNode) -> NotebookNode:
1416
meta = cell.metadata['nbgrader']
1517

1618
if 'grade' not in meta:
@@ -43,7 +45,7 @@ def _upgrade_v0_to_v1(self, cell):
4345

4446
return cell
4547

46-
def upgrade_cell_metadata(self, cell):
48+
def upgrade_cell_metadata(self, cell: NotebookNode) -> NotebookNode:
4749
if 'nbgrader' not in cell.metadata:
4850
return cell
4951

@@ -59,7 +61,7 @@ def upgrade_cell_metadata(self, cell):
5961
self._remove_extra_keys(cell)
6062
return cell
6163

62-
def validate_cell(self, cell):
64+
def validate_cell(self, cell: NotebookNode) -> None:
6365
super(MetadataValidatorV1, self).validate_cell(cell)
6466

6567
if 'nbgrader' not in cell.metadata:
@@ -92,7 +94,7 @@ def validate_cell(self, cell):
9294
raise ValidationError(
9395
"Markdown solution cell is not marked as a grade cell: {}".format(cell.source))
9496

95-
def validate_nb(self, nb):
97+
def validate_nb(self, nb: NotebookNode) -> None:
9698
super(MetadataValidatorV1, self).validate_nb(nb)
9799

98100
ids = set([])
@@ -114,23 +116,23 @@ def validate_nb(self, nb):
114116
ids.add(grade_id)
115117

116118

117-
def read_v1(source, as_version, **kwargs):
119+
def read_v1(source: typing.io.TextIO, as_version: int, **kwargs: typing.Any) -> NotebookNode:
118120
nb = _read(source, as_version, **kwargs)
119121
MetadataValidatorV1().validate_nb(nb)
120122
return nb
121123

122124

123-
def write_v1(nb, fp, **kwargs):
125+
def write_v1(nb: NotebookNode, fp: typing.io.TextIO, **kwargs: typing.Any) -> None:
124126
MetadataValidatorV1().validate_nb(nb)
125-
return _write(nb, fp, **kwargs)
127+
_write(nb, fp, **kwargs)
126128

127129

128-
def reads_v1(source, as_version, **kwargs):
130+
def reads_v1(source: str, as_version: int, **kwargs: typing.Any) -> NotebookNode:
129131
nb = _reads(source, as_version, **kwargs)
130132
MetadataValidatorV1().validate_nb(nb)
131133
return nb
132134

133135

134-
def writes_v1(nb, **kwargs):
136+
def writes_v1(nb: NotebookNode, **kwargs: typing.Any) -> None:
135137
MetadataValidatorV1().validate_nb(nb)
136-
return _writes(nb, **kwargs)
138+
_writes(nb, **kwargs)

nbgrader/nbgraderformat/v2.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from nbformat import read as _read, reads as _reads
22
from nbformat import write as _write, writes as _writes
3+
from nbformat.notebooknode import NotebookNode
4+
import typing
35
from .v1 import MetadataValidatorV1
46
from .common import BaseMetadataValidator, ValidationError
57

@@ -12,7 +14,7 @@ def __init__(self) -> None:
1214
super().__init__()
1315
self.v1 = MetadataValidatorV1()
1416

15-
def _upgrade_v1_to_v2(self, cell):
17+
def _upgrade_v1_to_v2(self, cell: NotebookNode) -> NotebookNode:
1618
meta = cell.metadata['nbgrader']
1719

1820
# only add cell type if the checksum has also already been set
@@ -24,7 +26,7 @@ def _upgrade_v1_to_v2(self, cell):
2426

2527
return cell
2628

27-
def upgrade_cell_metadata(self, cell):
29+
def upgrade_cell_metadata(self, cell: NotebookNode) -> NotebookNode:
2830
if 'nbgrader' not in cell.metadata:
2931
return cell
3032

@@ -42,7 +44,7 @@ def upgrade_cell_metadata(self, cell):
4244
self._remove_extra_keys(cell)
4345
return cell
4446

45-
def validate_cell(self, cell):
47+
def validate_cell(self, cell: NotebookNode) -> None:
4648
super(MetadataValidatorV2, self).validate_cell(cell)
4749

4850
if 'nbgrader' not in cell.metadata:
@@ -81,7 +83,7 @@ def validate_cell(self, cell):
8183
raise ValidationError(
8284
"Markdown solution cell is not marked as a grade cell: {}".format(cell.source))
8385

84-
def validate_nb(self, nb):
86+
def validate_nb(self, nb: NotebookNode) -> None:
8587
super(MetadataValidatorV2, self).validate_nb(nb)
8688

8789
ids = set([])
@@ -103,23 +105,23 @@ def validate_nb(self, nb):
103105
ids.add(grade_id)
104106

105107

106-
def read_v2(source, as_version, **kwargs):
108+
def read_v2(source: typing.io.TextIO, as_version: int, **kwargs: typing.Any) -> NotebookNode:
107109
nb = _read(source, as_version, **kwargs)
108110
MetadataValidatorV2().validate_nb(nb)
109111
return nb
110112

111113

112-
def write_v2(nb, fp, **kwargs):
114+
def write_v2(nb: NotebookNode, fp: typing.io.TextIO, **kwargs: typing.Any) -> None:
113115
MetadataValidatorV2().validate_nb(nb)
114-
return _write(nb, fp, **kwargs)
116+
_write(nb, fp, **kwargs)
115117

116118

117-
def reads_v2(source, as_version, **kwargs):
119+
def reads_v2(source: str, as_version: int, **kwargs: typing.Any) -> NotebookNode:
118120
nb = _reads(source, as_version, **kwargs)
119121
MetadataValidatorV2().validate_nb(nb)
120122
return nb
121123

122124

123-
def writes_v2(nb, **kwargs):
125+
def writes_v2(nb: NotebookNode, **kwargs: typing.Any) -> None:
124126
MetadataValidatorV2().validate_nb(nb)
125-
return _writes(nb, **kwargs)
127+
_writes(nb, **kwargs)

nbgrader/nbgraderformat/v3.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from nbformat import read as _read, reads as _reads
22
from nbformat import write as _write, writes as _writes
3+
from nbformat.notebooknode import NotebookNode
4+
import typing
35
from .v1 import MetadataValidatorV1
46
from .v2 import MetadataValidatorV2
57
from .common import BaseMetadataValidator, ValidationError
@@ -15,13 +17,13 @@ def __init__(self) -> None:
1517
self.v1 = MetadataValidatorV1()
1618
self.v2 = MetadataValidatorV2()
1719

18-
def _upgrade_v2_to_v3(self, cell):
20+
def _upgrade_v2_to_v3(self, cell: NotebookNode) -> NotebookNode:
1921
meta = cell.metadata['nbgrader']
2022
meta['schema_version'] = self.schema_version
2123

2224
return cell
2325

24-
def upgrade_cell_metadata(self, cell):
26+
def upgrade_cell_metadata(self, cell: NotebookNode) -> NotebookNode:
2527
if 'nbgrader' not in cell.metadata:
2628
return cell
2729

@@ -109,23 +111,23 @@ def validate_nb(self, nb: NotebookNode) -> None:
109111
ids.add(grade_id)
110112

111113

112-
def read_v3(source, as_version, **kwargs):
114+
def read_v3(source: typing.io.TextIO, as_version: int, **kwargs: typing.Any) -> NotebookNode:
113115
nb = _read(source, as_version, **kwargs)
114116
MetadataValidatorV3().validate_nb(nb)
115117
return nb
116118

117119

118-
def write_v3(nb, fp, **kwargs):
120+
def write_v3(nb: NotebookNode, fp: typing.io.TextIO, **kwargs: typing.Any) -> None:
119121
MetadataValidatorV3().validate_nb(nb)
120-
return _write(nb, fp, **kwargs)
122+
_write(nb, fp, **kwargs)
121123

122124

123-
def reads_v3(source, as_version, **kwargs):
125+
def reads_v3(source: str, as_version: int, **kwargs: typing.Any) -> NotebookNode:
124126
nb = _reads(source, as_version, **kwargs)
125127
MetadataValidatorV3().validate_nb(nb)
126128
return nb
127129

128130

129-
def writes_v3(nb, **kwargs):
131+
def writes_v3(nb: NotebookNode, **kwargs: typing.Any) -> None:
130132
MetadataValidatorV3().validate_nb(nb)
131-
return _writes(nb, **kwargs)
133+
_writes(nb, **kwargs)

0 commit comments

Comments
 (0)