Skip to content

Commit 124095e

Browse files
committed
Further type annotations
1 parent 5330c1d commit 124095e

File tree

7 files changed

+50
-39
lines changed

7 files changed

+50
-39
lines changed

nbgrader/auth/base.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def get_student_courses(self, student_id: str) -> Optional[list]: # pragma: no
1010
1111
Arguments
1212
---------
13-
student_id: string
13+
student_id:
1414
The unique id of the student.
1515
1616
Returns
@@ -28,9 +28,9 @@ def add_student_to_course(self, student_id: str, course_id: str) -> None: # pra
2828
2929
Arguments
3030
---------
31-
student_id: string
31+
student_id:
3232
The unique id of the student.
33-
course_id: string
33+
course_id:
3434
The unique id of the course.
3535
3636
"""
@@ -41,9 +41,9 @@ def remove_student_from_course(self, student_id: str, course_id: str) -> None:
4141
4242
Arguments
4343
---------
44-
student_id: string
44+
student_id:
4545
The unique id of the student.
46-
course_id: string
46+
course_id:
4747
The unique id of the course.
4848
4949
"""
@@ -88,7 +88,7 @@ def get_student_courses(self, student_id: str) -> Optional[list]:
8888
8989
Arguments
9090
---------
91-
student_id: string
91+
student_id:
9292
The unique id of the student.
9393
9494
Returns
@@ -106,9 +106,9 @@ def has_access(self, student_id: str, course_id: str) -> bool:
106106
107107
Arguments
108108
---------
109-
student_id: string
109+
student_id:
110110
The unique id of the student.
111-
course_id: string
111+
course_id:
112112
The unique id of the course.
113113
114114
Returns
@@ -126,9 +126,9 @@ def add_student_to_course(self, student_id: str, course_id: str) -> None:
126126
127127
Arguments
128128
---------
129-
student_id: string
129+
student_id:
130130
The unique id of the student.
131-
course_id: string
131+
course_id:
132132
The unique id of the course.
133133
134134
"""
@@ -139,9 +139,9 @@ def remove_student_from_course(self, student_id: str, course_id: str) -> None:
139139
140140
Arguments
141141
---------
142-
student_id: string
142+
student_id:
143143
The unique id of the student.
144-
course_id: string
144+
course_id:
145145
The unique id of the course.
146146
147147
"""

nbgrader/plugins/export.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
from traitlets import Unicode, List
22

33
from .base import BasePlugin
4-
from ..api import MissingEntry
4+
from ..api import MissingEntry, Gradebook
55

66

77
class ExportPlugin(BasePlugin):
88
"""Base class for export plugins."""
99

1010
to = Unicode("", help="destination to export to").tag(config=True)
1111

12-
student = List([],
13-
help="list of students to export").tag(config=True)
12+
student = List(
13+
[], help="list of students to export").tag(config=True)
1414

15-
assignment = List([],
16-
help="list of assignments to export").tag(config=True)
15+
assignment = List(
16+
[], help="list of assignments to export").tag(config=True)
1717

18-
def export(self, gradebook):
18+
def export(self, gradebook: Gradebook) -> None:
1919
"""Export grades to another format.
2020
2121
This method MUST be implemented by subclasses. Users should be able to
@@ -25,7 +25,7 @@ def export(self, gradebook):
2525
2626
Arguments
2727
---------
28-
gradebook: :class:`nbgrader.api.Gradebook`
28+
gradebook:
2929
An instance of the gradebook
3030
3131
"""
@@ -35,7 +35,7 @@ def export(self, gradebook):
3535
class CsvExportPlugin(ExportPlugin):
3636
"""CSV exporter plugin."""
3737

38-
def export(self, gradebook):
38+
def export(self, gradebook: Gradebook) -> None:
3939
if self.to == "":
4040
dest = "grades.csv"
4141
else:

nbgrader/plugins/latesubmission.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ def late_submission_penalty(self, student_id: str, score: float, total_seconds_l
2626
2727
Parameters
2828
----------
29-
student_id : string
29+
student_id:
3030
The unique id of the student
31-
score : float
31+
score:
3232
The score the student obtained for the submitted notebook
33-
total_seconds_late : float
33+
total_seconds_late:
3434
The total number of seconds the submitted notebook was late
3535
3636
Returns

nbgrader/plugins/zipcollect.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from textwrap import dedent
66
from traitlets import Bool, List, Unicode
7+
from typing import Optional
78

89
from .base import BasePlugin
910
from ..utils import unzip
@@ -31,7 +32,7 @@ class ExtractorPlugin(BasePlugin):
3132
)
3233
).tag(config=True)
3334

34-
def extract(self, archive_path, extracted_path):
35+
def extract(self, archive_path: str, extracted_path: str) -> None:
3536
"""Extract archive (zip) files and submission files in the
3637
`archive_directory`. Files are extracted to the `extracted_directory`.
3738
Non-archive (zip) files found in the `archive_directory` are copied to
@@ -42,9 +43,9 @@ def extract(self, archive_path, extracted_path):
4243
4344
Arguments
4445
---------
45-
archive_path: str
46+
archive_path:
4647
Absolute path to the `archive_directory`.
47-
extracted_path: str
48+
extracted_path:
4849
Absolute path to the `extracted_directory`.
4950
"""
5051
if not os.listdir(archive_path):
@@ -100,7 +101,7 @@ class FileNameCollectorPlugin(BasePlugin):
100101
named_regexp = Unicode(
101102
default_value='',
102103
help=dedent(
103-
"""
104+
r"""
104105
This regular expression is applied to each submission filename and
105106
MUST be supplied by the instructor. This regular expression MUST
106107
provide the `(?P<student_id>...)` and `(?P<file_id>...)` named
@@ -131,7 +132,7 @@ class FileNameCollectorPlugin(BasePlugin):
131132
)
132133
).tag(config=True)
133134

134-
def _match(self, filename):
135+
def _match(self, filename: str) -> Optional[dict]:
135136
"""Match the named group regular expression to the beginning of the
136137
filename and return the match groupdict or None if no match.
137138
"""
@@ -157,20 +158,20 @@ def _match(self, filename):
157158
)
158159
return gd
159160

160-
def collect(self, submitted_file):
161+
def collect(self, submitted_file: str) -> Optional[dict]:
161162
"""This is the main function called by the
162163
:class:`~nbgrader.apps.zipcollectapp.ZipCollectApp` for each submitted
163164
file. Note this function must also return a dictionary or None for
164165
sub-classed plugins.
165166
166167
Arguments
167168
---------
168-
submitted_file: str
169+
submitted_file:
169170
Each submitted file in the ``extracted_directory`` (absolute path).
170171
171172
Returns
172173
-------
173-
groupdict: dict
174+
groupdict:
174175
Collected data from the filename or None if the file should be
175176
skipped. Collected data is a dict of the form::
176177

nbgrader/preprocessors/getgrades.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
from traitlets import List
22

3+
from nbconvert.exporters.exporter import ResourcesDict
4+
from nbformat.notebooknode import NotebookNode
5+
from typing import Optional, Any, Tuple
6+
37
from .. import utils
48
from ..api import Gradebook
59
from . import NbGraderPreprocessor
@@ -10,7 +14,10 @@ class GetGrades(NbGraderPreprocessor):
1014

1115
display_data_priority = List(['text/html', 'application/pdf', 'text/latex', 'image/svg+xml', 'image/png', 'image/jpeg', 'text/plain'], config=True)
1216

13-
def preprocess(self, nb, resources):
17+
def preprocess(self,
18+
nb: NotebookNode,
19+
resources: ResourcesDict,
20+
) -> Tuple[NotebookNode, ResourcesDict]:
1421
# pull information from the resources
1522
self.notebook_id = resources['nbgrader']['notebook']
1623
self.assignment_id = resources['nbgrader']['assignment']
@@ -37,7 +44,7 @@ def preprocess(self, nb, resources):
3744

3845
return nb, resources
3946

40-
def _get_comment(self, cell, resources):
47+
def _get_comment(self, cell: NotebookNode, resources: ResourcesDict) -> None:
4148
"""Graders can optionally add comments to the student's solutions, so
4249
add the comment information into the database if it doesn't
4350
already exist. It should NOT overwrite existing comments that
@@ -55,7 +62,7 @@ def _get_comment(self, cell, resources):
5562
# save it in the notebook
5663
cell.metadata.nbgrader['comment'] = comment.comment
5764

58-
def _get_score(self, cell, resources):
65+
def _get_score(self, cell: NotebookNode, resources: ResourcesDict) -> None:
5966
grade = self.gradebook.find_grade(
6067
cell.metadata['nbgrader']['grade_id'],
6168
self.notebook_id,
@@ -65,7 +72,10 @@ def _get_score(self, cell, resources):
6572
cell.metadata.nbgrader['score'] = grade.score
6673
cell.metadata.nbgrader['points'] = grade.max_score
6774

68-
def preprocess_cell(self, cell, resources, cell_index):
75+
def preprocess_cell(self,
76+
cell: NotebookNode,
77+
resources: ResourcesDict, cell_index: int,
78+
) -> Tuple[NotebookNode, ResourcesDict]:
6979
# if it's a solution cell, then add a comment
7080
if utils.is_solution(cell):
7181
self._get_comment(cell, resources)

nbgrader/preprocessors/overwritecells.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from . import NbGraderPreprocessor
66
from nbconvert.exporters.exporter import ResourcesDict
77
from nbformat.notebooknode import NotebookNode
8-
from typing import Tuple
8+
from typing import Tuple, Any
99

1010

1111
class OverwriteCells(NbGraderPreprocessor):
@@ -25,7 +25,7 @@ def preprocess(self, nb: NotebookNode, resources: ResourcesDict) -> Tuple[Notebo
2525

2626
return nb, resources
2727

28-
def update_cell_type(self, cell, cell_type):
28+
def update_cell_type(self, cell: NotebookNode, cell_type: str) -> None:
2929
if cell.cell_type == cell_type:
3030
return
3131
elif cell_type == 'code':
@@ -41,7 +41,7 @@ def update_cell_type(self, cell, cell_type):
4141
del cell['execution_count']
4242
validate(cell, 'markdown_cell')
4343

44-
def report_change(self, name, attr, old, new):
44+
def report_change(self, name: str, attr: str, old: Any, new: Any) -> None:
4545
self.log.warning(
4646
"Attribute '%s' for cell %s has changed! (should be: %s, got: %s)", attr, name, old, new)
4747

nbgrader/preprocessors/savecells.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def _create_solution_cell(self, cell: NotebookNode) -> None:
131131

132132
self.new_solution_cells[grade_id] = solution_cell
133133

134-
def _create_task_cell(self, cell):
134+
def _create_task_cell(self, cell: NotebookNode) -> None:
135135
grade_id = cell.metadata.nbgrader['grade_id']
136136
try:
137137
task_cell = self.gradebook.find_task_cell(grade_id, self.notebook_id, self.assignment_id).to_dict()

0 commit comments

Comments
 (0)