99
1010from .preprocessors import Execute , ClearOutput , CheckCellMetadata
1111from . import utils
12+ from nbformat .notebooknode import NotebookNode
13+ import typing
1214
1315
1416class Validator (LoggingConfigurable ):
@@ -94,7 +96,7 @@ class Validator(LoggingConfigurable):
9496
9597 stream = sys .stdout
9698
97- def _indent (self , val ) :
99+ def _indent (self , val : str ) -> str :
98100 lines = val .split ("\n " )
99101 new_lines = []
100102 for line in lines :
@@ -104,7 +106,7 @@ def _indent(self, val):
104106 new_lines .append (new_line )
105107 return "\n " .join (new_lines )
106108
107- def _extract_error (self , cell ) :
109+ def _extract_error (self , cell : NotebookNode ) -> str :
108110 errors = []
109111
110112 # possibilities:
@@ -129,7 +131,7 @@ def _extract_error(self, cell):
129131
130132 return "\n " .join (errors )
131133
132- def _print_type_changed (self , old_type , new_type , source ) :
134+ def _print_type_changed (self , old_type : str , new_type : str , source : str ) -> None :
133135 self .stream .write ("\n " + "=" * self .width + "\n " )
134136 self .stream .write (
135137 "The following {} cell has changed to a {} cell:\n \n " .format (
@@ -141,7 +143,7 @@ def _print_changed(self, source):
141143 self .stream .write ("The following cell has changed:\n \n " )
142144 self .stream .write (self ._indent (source ) + "\n \n " )
143145
144- def _print_error (self , source , error ) :
146+ def _print_error (self , source : str , error : str ) -> None :
145147 self .stream .write ("\n " + "=" * self .width + "\n " )
146148 self .stream .write ("The following cell failed:\n \n " )
147149 self .stream .write (self ._indent (source ) + "\n \n " )
@@ -153,7 +155,7 @@ def _print_pass(self, source):
153155 self .stream .write ("The following cell passed:\n \n " )
154156 self .stream .write (self ._indent (source ) + "\n \n " )
155157
156- def _print_num_type_changed (self , num_changed ) :
158+ def _print_num_type_changed (self , num_changed : int ) -> None :
157159 if num_changed == 0 :
158160 return
159161
@@ -165,7 +167,7 @@ def _print_num_type_changed(self, num_changed):
165167 )
166168 )
167169
168- def _print_num_changed (self , num_changed ) :
170+ def _print_num_changed (self , num_changed : int ) -> None :
169171 if num_changed == 0 :
170172 return
171173
@@ -177,7 +179,7 @@ def _print_num_changed(self, num_changed):
177179 )
178180 )
179181
180- def _print_num_failed (self , num_failed ) :
182+ def _print_num_failed (self , num_failed : int ) -> None :
181183 if num_failed == 0 :
182184 self .stream .write ("Success! Your notebook passes all the tests.\n " )
183185
@@ -201,7 +203,7 @@ def _print_num_passed(self, num_passed):
201203 )
202204 )
203205
204- def _get_type_changed_cells (self , nb ) :
206+ def _get_type_changed_cells (self , nb : NotebookNode ) -> typing . List [ NotebookNode ] :
205207 changed = []
206208
207209 for cell in nb .cells :
@@ -217,7 +219,7 @@ def _get_type_changed_cells(self, nb):
217219
218220 return changed
219221
220- def _get_changed_cells (self , nb ) :
222+ def _get_changed_cells (self , nb : NotebookNode ) -> typing . List :
221223 changed = []
222224 for cell in nb .cells :
223225 if not (utils .is_grade (cell ) or utils .is_locked (cell )):
@@ -237,7 +239,7 @@ def _get_changed_cells(self, nb):
237239
238240 return changed
239241
240- def _get_failed_cells (self , nb ) :
242+ def _get_failed_cells (self , nb : NotebookNode ) -> typing . List [ NotebookNode ] :
241243 failed = []
242244 for cell in nb .cells :
243245 if not (self .validate_all or utils .is_grade (cell ) or utils .is_locked (cell )):
@@ -260,7 +262,7 @@ def _get_failed_cells(self, nb):
260262
261263 return failed
262264
263- def _get_passed_cells (self , nb ) :
265+ def _get_passed_cells (self , nb : NotebookNode ) -> typing . List [ NotebookNode ] :
264266 passed = []
265267 for cell in nb .cells :
266268 if not (utils .is_grade (cell ) or utils .is_locked (cell )):
@@ -278,7 +280,7 @@ def _get_passed_cells(self, nb):
278280
279281 return passed
280282
281- def _preprocess (self , nb ) :
283+ def _preprocess (self , nb : NotebookNode ) -> NotebookNode :
282284 resources = {}
283285 with utils .setenv (NBGRADER_VALIDATING = '1' ):
284286 for preprocessor in self .preprocessors :
@@ -290,7 +292,7 @@ def _preprocess(self, nb):
290292 nb , resources = pp .preprocess (nb , resources )
291293 return nb
292294
293- def validate (self , filename ) :
295+ def validate (self , filename : str ) -> typing . Dict [ str , typing . List [ typing . Dict [ str , str ]]] :
294296 self .log .info ("Validating '{}'" .format (os .path .abspath (filename )))
295297 basename = os .path .basename (filename )
296298 dirname = os .path .dirname (filename )
@@ -336,7 +338,7 @@ def validate(self, filename):
336338
337339 return results
338340
339- def validate_and_print (self , filename ) :
341+ def validate_and_print (self , filename : str ) -> None :
340342 results = self .validate (filename )
341343 type_changed = results .get ('type_changed' , [])
342344 changed = results .get ('changed' , [])
0 commit comments