Skip to content

Commit 4f27adf

Browse files
committed
#19 Add type annotations to jupyter_notebook.py module.
1 parent 0b43e85 commit 4f27adf

1 file changed

Lines changed: 71 additions & 65 deletions

File tree

numdoclint/jupyter_notebook.py

Lines changed: 71 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
"""A module that checks docstring in Jupyter notebook.
22
"""
33

4-
from __future__ import print_function
5-
64
import json
75
import os
6+
from typing import List
87

98
from numdoclint import helper, py_module
109

11-
INFO_KEY_NOTEBOOK_PATH = 'notebook_path'
12-
INFO_KEY_CODE_CELL_INDEX = 'code_cell_index'
13-
INFO_KEY_FUNC_NAME = py_module.INFO_KEY_FUNC_NAME
14-
INFO_KEY_INFO_ID = py_module.INFO_KEY_INFO_ID
15-
INFO_KEY_INFO = py_module.INFO_KEY_INFO
10+
INFO_KEY_NOTEBOOK_PATH: str = 'notebook_path'
11+
INFO_KEY_CODE_CELL_INDEX: str = 'code_cell_index'
12+
INFO_KEY_FUNC_NAME: str = py_module.INFO_KEY_FUNC_NAME
13+
INFO_KEY_INFO_ID: str = py_module.INFO_KEY_INFO_ID
14+
INFO_KEY_INFO: str = py_module.INFO_KEY_INFO
1615

17-
VERBOSE_ENABLED = py_module.VERBOSE_ENABLED
18-
VERBOSE_DISABLED = py_module.VERBOSE_DISABLED
16+
VERBOSE_ENABLED: int = py_module.VERBOSE_ENABLED
17+
VERBOSE_DISABLED: int = py_module.VERBOSE_DISABLED
1918

2019

2120
def check_jupyter_notebook(
22-
notebook_path, verbose=1, ignore_func_name_prefix_list=['test_'],
23-
ignore_info_id_list=[],
24-
enable_default_or_optional_doc_check=False):
21+
notebook_path: str, verbose: int = 1,
22+
ignore_func_name_prefix_list: List[str] = ['test_'],
23+
ignore_info_id_list: List[int] = [],
24+
enable_default_or_optional_doc_check: bool = False) -> List[dict]:
2525
"""
2626
Check docstring of single Jupyter notebook.
2727
@@ -64,21 +64,21 @@ def check_jupyter_notebook(
6464
- If the target notebook can not be found.
6565
- If the target notebook extension is not `ipynb`.
6666
"""
67-
is_checkpoint_in = '.ipynb_checkpoints' in notebook_path
67+
is_checkpoint_in: bool = '.ipynb_checkpoints' in notebook_path
6868
if is_checkpoint_in:
6969
return []
7070
_check_notebook_exists(notebook_path=notebook_path)
7171
_check_notebook_extension(notebook_path=notebook_path)
72-
notebook_data_dict = _read_notebook_data_dict(
72+
notebook_data_dict: dict = _read_notebook_data_dict(
7373
notebook_path=notebook_path)
74-
code_cell_str_list = _get_code_cell_str_list(
74+
code_cell_str_list: List[str] = _get_code_cell_str_list(
7575
notebook_data_dict=notebook_data_dict)
7676
if not code_cell_str_list:
7777
return []
78-
info_list = []
79-
enable_def_or_opt_check = enable_default_or_optional_doc_check
78+
info_list: List[dict]= []
79+
enable_def_or_opt_check: bool = enable_default_or_optional_doc_check
8080
for i, code_cell_str in enumerate(code_cell_str_list):
81-
info_list_unit = _check_unit_code_cell_str(
81+
info_list_unit: List[dict] = _check_unit_code_cell_str(
8282
notebook_path=notebook_path,
8383
code_cell_idx=i,
8484
code_cell_str=code_cell_str,
@@ -91,9 +91,10 @@ def check_jupyter_notebook(
9191

9292

9393
def check_jupyter_notebook_recursively(
94-
dir_path, verbose=1, ignore_func_name_prefix_list=['test_'],
95-
ignore_info_id_list=[],
96-
enable_default_or_optional_doc_check=False):
94+
dir_path: str, verbose: int = 1,
95+
ignore_func_name_prefix_list: List[str] = ['test_'],
96+
ignore_info_id_list: List[int] = [],
97+
enable_default_or_optional_doc_check: bool = False) -> List[dict]:
9798
"""
9899
Check docstring of Jupyter notebook recursively.
99100
@@ -130,8 +131,8 @@ def check_jupyter_notebook_recursively(
130131
- info_id : int -> Identification number of which information.
131132
- info : str -> Information of check result.
132133
"""
133-
enable_def_or_opt_check = enable_default_or_optional_doc_check
134-
info_list = _check_jupyter_notebook_recursively(
134+
enable_def_or_opt_check: bool = enable_default_or_optional_doc_check
135+
info_list: List[dict] = _check_jupyter_notebook_recursively(
135136
dir_path=dir_path,
136137
info_list=[],
137138
verbose=verbose,
@@ -142,9 +143,10 @@ def check_jupyter_notebook_recursively(
142143

143144

144145
def _check_jupyter_notebook_recursively(
145-
dir_path, info_list, verbose,
146-
ignore_func_name_prefix_list, ignore_info_id_list,
147-
enable_default_or_optional_doc_check):
146+
dir_path: str, info_list: List[dict], verbose: int,
147+
ignore_func_name_prefix_list: List[str],
148+
ignore_info_id_list: List[int],
149+
enable_default_or_optional_doc_check: bool) -> List[dict]:
148150
"""
149151
Check docstring of Jupyter notebook recursively.
150152
@@ -176,12 +178,12 @@ def _check_jupyter_notebook_recursively(
176178
info_list : list of dicts
177179
A list containing information on check results.
178180
"""
179-
file_or_folder_name_list = os.listdir(dir_path)
181+
file_or_folder_name_list: List[str] = os.listdir(dir_path)
180182
if not file_or_folder_name_list:
181183
return info_list
182-
enable_def_or_opt_check = enable_default_or_optional_doc_check
184+
enable_def_or_opt_check: bool = enable_default_or_optional_doc_check
183185
for file_or_folder_name in file_or_folder_name_list:
184-
path = os.path.join(dir_path, file_or_folder_name)
186+
path: str = os.path.join(dir_path, file_or_folder_name)
185187
path = path.replace('\\', '/')
186188
if os.path.isdir(path):
187189
info_list = _check_jupyter_notebook_recursively(
@@ -194,7 +196,7 @@ def _check_jupyter_notebook_recursively(
194196
continue
195197
if not path.endswith('.ipynb'):
196198
continue
197-
unit_info_list = check_jupyter_notebook(
199+
unit_info_list: List[dict]= check_jupyter_notebook(
198200
notebook_path=path,
199201
verbose=verbose,
200202
ignore_func_name_prefix_list=ignore_func_name_prefix_list,
@@ -204,7 +206,7 @@ def _check_jupyter_notebook_recursively(
204206
return info_list
205207

206208

207-
def _print_info_list(info_list, verbose):
209+
def _print_info_list(info_list: List[dict], verbose: int) -> str:
208210
"""
209211
Print check result.
210212
@@ -230,7 +232,7 @@ def _print_info_list(info_list, verbose):
230232
return ''
231233
if verbose != VERBOSE_ENABLED:
232234
return ''
233-
printed_str = ''
235+
printed_str: str = ''
234236
for info_dict in info_list:
235237
if printed_str != '':
236238
printed_str += '\n'
@@ -245,9 +247,10 @@ def _print_info_list(info_list, verbose):
245247

246248

247249
def _check_unit_code_cell_str(
248-
notebook_path, code_cell_idx, code_cell_str,
249-
ignore_func_name_prefix_list, ignore_info_id_list,
250-
enable_default_or_optional_doc_check):
250+
notebook_path: str, code_cell_idx: int, code_cell_str: str,
251+
ignore_func_name_prefix_list: List[str],
252+
ignore_info_id_list: List[int],
253+
enable_default_or_optional_doc_check: bool) -> List[dict]:
251254
"""
252255
Check the single code cell.
253256
@@ -280,33 +283,34 @@ def _check_unit_code_cell_str(
280283
- info_id : int -> Identification number of which information.
281284
- info : str -> Information of check result.
282285
"""
283-
func_name_list = helper.get_func_name_list(
286+
func_name_list: List[str] = helper.get_func_name_list(
284287
code_str=code_cell_str)
285288
if not func_name_list:
286289
return []
287-
info_list = []
288-
enable_def_or_opt_check = enable_default_or_optional_doc_check
290+
info_list: List[dict] = []
291+
enable_def_or_opt_check: bool= enable_default_or_optional_doc_check
289292
for func_name in func_name_list:
290-
is_func_name_to_ignore = py_module.is_func_name_to_ignore(
293+
is_func_name_to_ignore: bool = py_module.is_func_name_to_ignore(
291294
func_name=func_name,
292295
ignore_func_name_prefix_list=ignore_func_name_prefix_list)
293296
if is_func_name_to_ignore:
294297
continue
295-
single_func_info_list = py_module.get_single_func_info_list(
296-
path=notebook_path,
297-
code_str=code_cell_str,
298-
func_name=func_name,
299-
enable_default_or_optional_doc_check=enable_def_or_opt_check,
300-
skip_decorator_name_list=[],
301-
ignore_info_id_list=ignore_info_id_list)
298+
single_func_info_list: List[dict] = \
299+
py_module.get_single_func_info_list(
300+
path=notebook_path,
301+
code_str=code_cell_str,
302+
func_name=func_name,
303+
enable_default_or_optional_doc_check=enable_def_or_opt_check,
304+
skip_decorator_name_list=[],
305+
ignore_info_id_list=ignore_info_id_list)
302306
info_list.extend(single_func_info_list)
303307
info_list = _rename_dict_key(info_list=info_list)
304308
info_list = _add_code_cell_index(
305309
info_list=info_list, code_cell_idx=code_cell_idx)
306310
return info_list
307311

308312

309-
def _add_code_cell_index(info_list, code_cell_idx):
313+
def _add_code_cell_index(info_list: List[dict], code_cell_idx: int):
310314
"""
311315
Add cell index value to the dictionaries in the list.
312316
@@ -329,7 +333,7 @@ def _add_code_cell_index(info_list, code_cell_idx):
329333
return info_list
330334

331335

332-
def _rename_dict_key(info_list):
336+
def _rename_dict_key(info_list: List[dict]) -> List[dict]:
333337
"""
334338
Rename dictionary key names in the list.
335339
@@ -354,13 +358,13 @@ def _rename_dict_key(info_list):
354358
- info : str
355359
"""
356360
for info_dict in info_list:
357-
path_str = info_dict[py_module.INFO_KEY_MODULE_PATH]
361+
path_str: str = info_dict[py_module.INFO_KEY_MODULE_PATH]
358362
info_dict[INFO_KEY_NOTEBOOK_PATH] = path_str
359363
del info_dict[py_module.INFO_KEY_MODULE_PATH]
360364
return info_list
361365

362366

363-
def _get_code_cell_str_list(notebook_data_dict):
367+
def _get_code_cell_str_list(notebook_data_dict: dict) -> List[str]:
364368
"""
365369
Get a list of code cell strings.
366370
@@ -374,22 +378,22 @@ def _get_code_cell_str_list(notebook_data_dict):
374378
code_str_list : list of str
375379
A list of code cell strings.
376380
"""
377-
code_str_list = []
378-
has_key = 'cells' in notebook_data_dict
381+
code_str_list: List[str] = []
382+
has_key: bool = 'cells' in notebook_data_dict
379383
if not has_key:
380384
return []
381-
cells_list = notebook_data_dict['cells']
385+
cells_list: List[dict] = notebook_data_dict['cells']
382386
for cell_dict in cells_list:
383-
cell_type = cell_dict['cell_type']
387+
cell_type: str = cell_dict['cell_type']
384388
if cell_type != 'code':
385389
continue
386-
source_list = cell_dict['source']
390+
source_list: List[str] = cell_dict['source']
387391
source = ''.join(source_list)
388392
code_str_list.append(source)
389393
return code_str_list
390394

391395

392-
def _read_notebook_data_dict(notebook_path):
396+
def _read_notebook_data_dict(notebook_path: str) -> dict:
393397
"""
394398
Read a dictionary of notebook data.
395399
@@ -404,12 +408,12 @@ def _read_notebook_data_dict(notebook_path):
404408
A dictionary of notebook data.
405409
"""
406410
with open(notebook_path, 'r') as f:
407-
notebook_data_str = f.read()
408-
notebook_data_dict = json.loads(notebook_data_str)
411+
notebook_data_str: str = f.read()
412+
notebook_data_dict: dict = json.loads(notebook_data_str)
409413
return notebook_data_dict
410414

411415

412-
def _check_notebook_extension(notebook_path):
416+
def _check_notebook_extension(notebook_path: str) -> None:
413417
"""
414418
Check the path extension of the notebook.
415419
@@ -423,15 +427,17 @@ def _check_notebook_extension(notebook_path):
423427
IOError
424428
If the extension is invalid.
425429
"""
426-
extension_str = notebook_path.split('.')[-1]
430+
extension_str: str = notebook_path.split('.')[-1]
427431
if extension_str.endswith('ipynb'):
428432
return
429-
err_msg = 'The extension is invalid. Please Specify a path of '\
430-
'`.ipynb` extension.'
433+
err_msg: str = (
434+
'The extension is invalid. Please Specify a path of '
435+
'`.ipynb` extension.'
436+
)
431437
raise IOError(err_msg)
432438

433439

434-
def _check_notebook_exists(notebook_path):
440+
def _check_notebook_exists(notebook_path: str) -> None:
435441
"""
436442
Check that the target Jupyter notebook exists.
437443
@@ -447,6 +453,6 @@ def _check_notebook_exists(notebook_path):
447453
"""
448454
if os.path.exists(notebook_path):
449455
return
450-
err_msg = 'The target notebook could not be found.'
451-
err_msg += '\nNotebook path: %s' % notebook_path
456+
err_msg: str = 'The target notebook could not be found.'
457+
err_msg += f'\nNotebook path: {notebook_path}'
452458
raise IOError(err_msg)

0 commit comments

Comments
 (0)