Skip to content

Commit ff3b948

Browse files
authored
Merge pull request #1282 from aliniknejad/master
Further type annotations across the codebase
2 parents 15f610f + 124095e commit ff3b948

40 files changed

+395
-246
lines changed

nbgrader/apps/autogradeapp.py

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

77
from .baseapp import NbGrader, nbgrader_aliases, nbgrader_flags
88
from ..converters import BaseConverter, Autograde, NbGraderException
9+
from traitlets.traitlets import MetaHasTraits
10+
from traitlets.config.loader import Config
11+
from typing import List
912

1013
aliases = {
1114
'course': 'CourseDirectory.course_id'
@@ -95,12 +98,12 @@ class AutogradeApp(NbGrader):
9598
"""
9699

97100
@default("classes")
98-
def _classes_default(self):
101+
def _classes_default(self) -> List[MetaHasTraits]:
99102
classes = super(AutogradeApp, self)._classes_default()
100103
classes.extend([BaseConverter, Autograde])
101104
return classes
102105

103-
def _load_config(self, cfg, **kwargs):
106+
def _load_config(self, cfg: Config, **kwargs: dict) -> None:
104107
if 'AutogradeApp' in cfg:
105108
self.log.warning(
106109
"Use Autograde in config, not AutogradeApp. Outdated config:\n%s",
@@ -114,8 +117,8 @@ def _load_config(self, cfg, **kwargs):
114117

115118
super(AutogradeApp, self)._load_config(cfg, **kwargs)
116119

117-
def start(self):
118-
super(AutogradeApp, self).start()
120+
def start(self) -> None:
121+
super().start()
119122

120123
if len(self.extra_args) > 1:
121124
self.fail("Only one argument (the assignment id) may be specified")

nbgrader/apps/baseapp.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
from .. import plugins
2323
from .. import exchange
2424
from .. import converters
25+
from traitlets.traitlets import MetaHasTraits
26+
from typing import List as TypingList
27+
from io import StringIO
28+
from typing import Any
2529

2630

2731
nbgrader_aliases = {
@@ -62,15 +66,15 @@ class NbGrader(JupyterApp):
6266
_log_formatter_cls = LogFormatter
6367

6468
@default("log_level")
65-
def _log_level_default(self):
69+
def _log_level_default(self) -> int:
6670
return logging.INFO
6771

6872
@default("log_datefmt")
69-
def _log_datefmt_default(self):
73+
def _log_datefmt_default(self) -> str:
7074
return "%Y-%m-%d %H:%M:%S"
7175

7276
@default("log_format")
73-
def _log_format_default(self):
77+
def _log_format_default(self) -> str:
7478
return "%(color)s[%(name)s | %(levelname)s]%(end_color)s %(message)s"
7579

7680
logfile = Unicode(
@@ -83,7 +87,11 @@ def _log_format_default(self):
8387
)
8488
).tag(config=True)
8589

86-
def init_logging(self, handler_class, handler_args, color=True, subapps=False):
90+
def init_logging(self,
91+
handler_class: type,
92+
handler_args: TypingList[StringIO],
93+
color: bool = True,
94+
subapps: bool = False) -> None:
8795
handler = handler_class(*handler_args)
8896

8997
if color:
@@ -101,7 +109,7 @@ def init_logging(self, handler_class, handler_args, color=True, subapps=False):
101109
if subapps and self.subapp:
102110
self.subapp.init_logging(handler_class, handler_args, color=color, subapps=subapps)
103111

104-
def deinit_logging(self):
112+
def deinit_logging(self) -> None:
105113
if len(self.log.handlers) > 1:
106114
for handler in self.log.handlers[1:]:
107115
handler.close()
@@ -115,10 +123,10 @@ def deinit_logging(self):
115123
classes = List()
116124

117125
@default("classes")
118-
def _classes_default(self):
126+
def _classes_default(self) -> TypingList[MetaHasTraits]:
119127
return [NbGrader, CourseDirectory]
120128

121-
def all_configurable_classes(self):
129+
def all_configurable_classes(self) -> TypingList[MetaHasTraits]:
122130
"""Get a list of all configurable classes for nbgrader
123131
"""
124132
# Call explicitly the method on this class, to avoid infinite recursion
@@ -163,10 +171,10 @@ def all_configurable_classes(self):
163171
return classes
164172

165173
@default("config_file_name")
166-
def _config_file_name_default(self):
174+
def _config_file_name_default(self) -> str:
167175
return u'nbgrader_config'
168176

169-
def _load_config(self, cfg, **kwargs):
177+
def _load_config(self, cfg: Config, **kwargs: Any) -> None:
170178
if 'NbGraderConfig' in cfg:
171179
self.log.warning(
172180
"Use NbGrader in config, not NbGraderConfig. Outdated config:\n%s",
@@ -286,14 +294,14 @@ def fail(self, msg, *args):
286294
self.log.error(msg, *args)
287295
sys.exit(1)
288296

289-
def build_extra_config(self):
297+
def build_extra_config(self) -> Config:
290298
return Config()
291299

292300
def excepthook(self, etype, evalue, tb):
293301
format_excepthook(etype, evalue, tb)
294302

295303
@catch_config_error
296-
def initialize(self, argv=None):
304+
def initialize(self, argv: TypingList[str] = None) -> None:
297305
self.update_config(self.build_extra_config())
298306
self.init_syspath()
299307
self.coursedir = CourseDirectory(parent=self)
@@ -305,11 +313,11 @@ def initialize(self, argv=None):
305313
if self.logfile:
306314
self.init_logging(logging.FileHandler, [self.logfile], color=False)
307315

308-
def init_syspath(self):
316+
def init_syspath(self) -> None:
309317
"""Add the cwd to the sys.path ($PYTHONPATH)"""
310318
sys.path.insert(0, os.getcwd())
311319

312-
def reset(self):
320+
def reset(self) -> None:
313321
# stop logging
314322
self.deinit_logging()
315323

@@ -325,7 +333,7 @@ def print_subcommands(self):
325333
for key, (app, desc) in self.subcommands.items():
326334
print(" {}\n{}\n".format(key, desc))
327335

328-
def load_config_file(self, **kwargs):
336+
def load_config_file(self, **kwargs: Any) -> None:
329337
"""Load the config file.
330338
By default, errors in loading config are handled, and a warning
331339
printed on screen. For testing, the suppress_errors option is set
@@ -341,6 +349,6 @@ def load_config_file(self, **kwargs):
341349

342350
super(NbGrader, self).load_config_file(**kwargs)
343351

344-
def start(self):
352+
def start(self) -> None:
345353
super(NbGrader, self).start()
346354
self.authenticator = Authenticator(parent=self)

nbgrader/apps/generateassignmentapp.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
from .baseapp import NbGrader, nbgrader_aliases, nbgrader_flags
88
from ..converters import BaseConverter, GenerateAssignment, NbGraderException
9+
from traitlets.traitlets import MetaHasTraits
10+
from typing import List, Any
11+
from traitlets.config.loader import Config
912

1013
aliases = {
1114
'course': 'CourseDirectory.course_id'
@@ -107,12 +110,12 @@ class GenerateAssignmentApp(NbGrader):
107110
"""
108111

109112
@default("classes")
110-
def _classes_default(self):
113+
def _classes_default(self) -> List[MetaHasTraits]:
111114
classes = super(GenerateAssignmentApp, self)._classes_default()
112115
classes.extend([BaseConverter, GenerateAssignment])
113116
return classes
114117

115-
def _load_config(self, cfg, **kwargs):
118+
def _load_config(self, cfg: Config, **kwargs: Any) -> None:
116119
if 'AssignApp' in cfg:
117120
self.log.warning(
118121
"Use GenerateAssignment in config, not AssignApp. Outdated config:\n%s",
@@ -124,10 +127,10 @@ def _load_config(self, cfg, **kwargs):
124127
cfg.GenerateAssignment.merge(cfg.AssignApp)
125128
del cfg.AssignApp
126129

127-
super(GenerateAssignmentApp, self)._load_config(cfg, **kwargs)
130+
super()._load_config(cfg, **kwargs)
128131

129-
def start(self):
130-
super(GenerateAssignmentApp, self).start()
132+
def start(self) -> None:
133+
super().start()
131134

132135
if len(self.extra_args) > 1:
133136
self.fail("Only one argument (the assignment id) may be specified")

nbgrader/apps/nbgraderapp.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
ZipCollectApp,
3939
GenerateConfigApp
4040
)
41+
from traitlets.traitlets import MetaHasTraits
42+
from typing import List
4143

4244
aliases = {}
4345
aliases.update(nbgrader_aliases)
@@ -294,14 +296,14 @@ class NbGraderApp(NbGrader):
294296
)
295297

296298
@default("classes")
297-
def _classes_default(self):
299+
def _classes_default(self) -> List[MetaHasTraits]:
298300
return self.all_configurable_classes()
299301

300302
@catch_config_error
301-
def initialize(self, argv=None):
303+
def initialize(self, argv: List[str] = None) -> None:
302304
super(NbGraderApp, self).initialize(argv)
303305

304-
def start(self):
306+
def start(self) -> None:
305307
# check: is there a subapp given?
306308
if self.subapp is None:
307309
print("No command given (run with --help for options). List of subcommands:\n")

nbgrader/auth/base.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
from traitlets import Instance, Type
22
from traitlets.config import LoggingConfigurable
3+
from typing import Any, Optional
34

45

56
class BaseAuthPlugin(LoggingConfigurable):
67

7-
def get_student_courses(self, student_id): # pragma: no cover
8+
def get_student_courses(self, student_id: str) -> Optional[list]: # pragma: no cover
89
"""Gets the list of courses that the student is enrolled in.
910
1011
Arguments
1112
---------
12-
student_id: string
13+
student_id:
1314
The unique id of the student.
1415
1516
Returns
@@ -22,27 +23,27 @@ def get_student_courses(self, student_id): # pragma: no cover
2223
"""
2324
raise NotImplementedError
2425

25-
def add_student_to_course(self, student_id, course_id): # pragma: no cover
26+
def add_student_to_course(self, student_id: str, course_id: str) -> None: # pragma: no cover
2627
"""Grants a student access to a given course.
2728
2829
Arguments
2930
---------
30-
student_id: string
31+
student_id:
3132
The unique id of the student.
32-
course_id: string
33+
course_id:
3334
The unique id of the course.
3435
3536
"""
3637
raise NotImplementedError
3738

38-
def remove_student_from_course(self, student_id, course_id): # pragma: no cover
39+
def remove_student_from_course(self, student_id: str, course_id: str) -> None: # pragma: no cover
3940
"""Removes a student's access to a given course.
4041
4142
Arguments
4243
---------
43-
student_id: string
44+
student_id:
4445
The unique id of the student.
45-
course_id: string
46+
course_id:
4647
The unique id of the course.
4748
4849
"""
@@ -51,23 +52,22 @@ def remove_student_from_course(self, student_id, course_id): # pragma: no cover
5152

5253
class NoAuthPlugin(BaseAuthPlugin):
5354

54-
def get_student_courses(self, student_id):
55+
def get_student_courses(self, student_id: str) -> Optional[list]:
5556
# None means that the student has access to any course that might exist
5657
# on the system.
5758
return None
5859

59-
def add_student_to_course(self, student_id, course_id):
60+
def add_student_to_course(self, student_id: str, course_id: str) -> None:
6061
# Nothing to do, we don't keep track of which students are in which
6162
# courses in the default setting.
6263
pass
6364

64-
def remove_student_from_course(self, student_id, course_id):
65+
def remove_student_from_course(self, student_id: str, course_id: str) -> None:
6566
# Nothing to do, we don't keep track of which students are in which
6667
# courses in the default setting.
6768
pass
6869

6970

70-
7171
class Authenticator(LoggingConfigurable):
7272

7373
plugin_class = Type(
@@ -78,17 +78,17 @@ class Authenticator(LoggingConfigurable):
7878

7979
plugin = Instance(BaseAuthPlugin).tag(config=False)
8080

81-
def __init__(self, *args, **kwargs):
82-
super(Authenticator, self).__init__(*args, **kwargs)
81+
def __init__(self, *args: Any, **kwargs: Any) -> None:
82+
super().__init__(*args, **kwargs)
8383
self.log.debug("Using authenticator: %s", self.plugin_class.__name__)
8484
self.plugin = self.plugin_class(parent=self)
8585

86-
def get_student_courses(self, student_id):
86+
def get_student_courses(self, student_id: str) -> Optional[list]:
8787
"""Gets the list of courses that the student is enrolled in.
8888
8989
Arguments
9090
---------
91-
student_id: string
91+
student_id:
9292
The unique id of the student.
9393
9494
Returns
@@ -101,14 +101,14 @@ def get_student_courses(self, student_id):
101101
"""
102102
return self.plugin.get_student_courses(student_id)
103103

104-
def has_access(self, student_id, course_id):
104+
def has_access(self, student_id: str, course_id: str) -> bool:
105105
"""Checks whether a student has access to a particular course.
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
@@ -121,27 +121,27 @@ def has_access(self, student_id, course_id):
121121
return True
122122
return course_id in courses
123123

124-
def add_student_to_course(self, student_id, course_id):
124+
def add_student_to_course(self, student_id: str, course_id: str) -> None:
125125
"""Grants a student access to a given course.
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
"""
135-
return self.plugin.add_student_to_course(student_id, course_id)
135+
self.plugin.add_student_to_course(student_id, course_id)
136136

137-
def remove_student_from_course(self, student_id, course_id):
137+
def remove_student_from_course(self, student_id: str, course_id: str) -> None:
138138
"""Removes a student's access to a given course.
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
"""

0 commit comments

Comments
 (0)