Skip to content

Commit 97b5b96

Browse files
committed
#19 Add type annotations to test_jupyter_notebook.py module.
1 parent 4f27adf commit 97b5b96

1 file changed

Lines changed: 46 additions & 43 deletions

File tree

tests/test_jupyter_notebook.py

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,36 @@
11
import os
22
import shutil
3+
from typing import List
34

45
import pytest
56
import six
67
from voluptuous import Any, Schema
78

89
from numdoclint import jupyter_notebook, py_module
910

10-
TMP_TEST_MODULE_DIR = './tests/tmp/'
11-
TMP_TEST_NOTEBOOK_PATH_1 = os.path.join(TMP_TEST_MODULE_DIR, 'tmp_1.ipynb')
12-
TMP_TEST_NOTEBOOK_PATH_2 = os.path.join(TMP_TEST_MODULE_DIR, 'tmp_2.ipynb')
11+
TMP_TEST_MODULE_DIR: str = './tests/tmp/'
12+
TMP_TEST_NOTEBOOK_PATH_1: str = os.path.join(
13+
TMP_TEST_MODULE_DIR, 'tmp_1.ipynb')
14+
TMP_TEST_NOTEBOOK_PATH_2: str = os.path.join(
15+
TMP_TEST_MODULE_DIR, 'tmp_2.ipynb')
1316

14-
STR_SCHEMA = Any(*six.string_types)
17+
STR_SCHEMA: Any = Any(*six.string_types)
1518

1619

17-
def setup():
20+
def setup() -> None:
1821
"""Function to be executed at the start of the test.
1922
"""
2023
shutil.rmtree(TMP_TEST_MODULE_DIR, ignore_errors=True)
2124
os.makedirs(TMP_TEST_MODULE_DIR)
2225

2326

24-
def teardown():
27+
def teardown() -> None:
2528
"""Function to be executed at the end of the test.
2629
"""
2730
shutil.rmtree(TMP_TEST_MODULE_DIR, ignore_errors=True)
2831

2932

30-
def _delete_test_notebook():
33+
def _delete_test_notebook() -> None:
3134
"""Delete notebook for testing if exists.
3235
"""
3336
if os.path.exists(TMP_TEST_NOTEBOOK_PATH_1):
@@ -36,10 +39,10 @@ def _delete_test_notebook():
3639
os.remove(TMP_TEST_NOTEBOOK_PATH_2)
3740

3841

39-
def test__check_notebook_exists():
42+
def test__check_notebook_exists() -> None:
4043
_delete_test_notebook()
4144

42-
with pytest.raises(IOError):
45+
with pytest.raises(IOError): # type: ignore
4346
jupyter_notebook._check_notebook_exists(
4447
notebook_path=TMP_TEST_NOTEBOOK_PATH_1)
4548

@@ -51,28 +54,28 @@ def test__check_notebook_exists():
5154
_delete_test_notebook()
5255

5356

54-
def test__check_notebook_extension():
55-
with pytest.raises(IOError):
57+
def test__check_notebook_extension() -> None:
58+
with pytest.raises(IOError): # type: ignore
5659
jupyter_notebook._check_notebook_extension(
5760
notebook_path='sample/path.py')
5861
jupyter_notebook._check_notebook_extension(
5962
notebook_path='sample/path.ipynb')
6063

6164

62-
def test__read_notebook_data_dict():
63-
notebook_data_dict = jupyter_notebook._read_notebook_data_dict(
65+
def test__read_notebook_data_dict() -> None:
66+
notebook_data_dict: dict = jupyter_notebook._read_notebook_data_dict(
6467
notebook_path='./tests/jupyter/test_jupyter_notebook_py3.ipynb')
6568
assert isinstance(notebook_data_dict, dict)
66-
has_key = 'cells' in notebook_data_dict
69+
has_key: bool = 'cells' in notebook_data_dict
6770
assert has_key
6871

6972

70-
def test__get_code_cell_str_list():
71-
code_str_list = jupyter_notebook._get_code_cell_str_list(
73+
def test__get_code_cell_str_list() -> None:
74+
code_str_list: List[str] = jupyter_notebook._get_code_cell_str_list(
7275
notebook_data_dict={})
7376
assert code_str_list == []
7477

75-
notebook_data_dict = {
78+
notebook_data_dict: dict = {
7679
'cells': [{
7780
'cell_type': 'code',
7881
'source': [
@@ -97,14 +100,14 @@ def test__get_code_cell_str_list():
97100
code_str_list = jupyter_notebook._get_code_cell_str_list(
98101
notebook_data_dict=notebook_data_dict)
99102
assert len(code_str_list) == 2
100-
expected_code_str = 'import pandas as pd\nimport numpy as np'
103+
expected_code_str: str = 'import pandas as pd\nimport numpy as np'
101104
assert code_str_list[0] == expected_code_str
102105
expected_code_str = 'def sample_func():\n pass'
103106
assert code_str_list[1] == expected_code_str
104107

105108

106-
def test__rename_dict_key():
107-
info_list = [{
109+
def test__rename_dict_key() -> None:
110+
info_list: List[dict]= [{
108111
py_module.INFO_KEY_MODULE_PATH: 'sample/path.ipynb',
109112
py_module.INFO_KEY_FUNC_NAME: 'sample_func_1',
110113
py_module.INFO_KEY_INFO_ID: 1,
@@ -118,7 +121,7 @@ def test__rename_dict_key():
118121
info_list = jupyter_notebook._rename_dict_key(
119122
info_list=info_list)
120123
assert len(info_list) == 2
121-
schema = Schema(
124+
schema: Schema = Schema(
122125
schema={
123126
jupyter_notebook.INFO_KEY_NOTEBOOK_PATH: 'sample/path.ipynb',
124127
jupyter_notebook.INFO_KEY_FUNC_NAME: Any(
@@ -132,13 +135,13 @@ def test__rename_dict_key():
132135
schema(info_dict)
133136

134137

135-
def test__add_code_cell_index():
136-
info_list = [{}, {}]
138+
def test__add_code_cell_index() -> None:
139+
info_list: List[dict]= [{}, {}]
137140
info_list = jupyter_notebook._add_code_cell_index(
138141
info_list=info_list,
139142
code_cell_idx=5)
140143
assert len(info_list) == 2
141-
schema = Schema(
144+
schema: Schema= Schema(
142145
schema={
143146
jupyter_notebook.INFO_KEY_CODE_CELL_INDEX: 5,
144147
},
@@ -147,14 +150,14 @@ def test__add_code_cell_index():
147150
schema(info_dict)
148151

149152

150-
def test__check_unit_code_cell_str():
151-
expected_notebook_path = 'sample/path.ipynb'
152-
expected_code_cell_idx = 5
153-
code_cell_str = """
153+
def test__check_unit_code_cell_str() -> None:
154+
expected_notebook_path: str = 'sample/path.ipynb'
155+
expected_code_cell_idx: int = 5
156+
code_cell_str: str = """
154157
import pandas as pd
155158
import numpy as np
156159
"""
157-
info_list = jupyter_notebook._check_unit_code_cell_str(
160+
info_list: List[dict] = jupyter_notebook._check_unit_code_cell_str(
158161
notebook_path=expected_notebook_path,
159162
code_cell_idx=expected_code_cell_idx,
160163
code_cell_str=code_cell_str,
@@ -163,7 +166,7 @@ def test__check_unit_code_cell_str():
163166
enable_default_or_optional_doc_check=False)
164167
assert info_list == []
165168

166-
schema = Schema(
169+
schema: Schema= Schema(
167170
schema={
168171
jupyter_notebook.INFO_KEY_NOTEBOOK_PATH: STR_SCHEMA,
169172
jupyter_notebook.INFO_KEY_CODE_CELL_INDEX: int,
@@ -250,12 +253,12 @@ def sample_func(price=100):
250253
assert info_list
251254

252255

253-
def test__print_info_list():
254-
printed_str = jupyter_notebook._print_info_list(
256+
def test__print_info_list() -> None:
257+
printed_str: str = jupyter_notebook._print_info_list(
255258
info_list=[], verbose=jupyter_notebook.VERBOSE_ENABLED)
256259
assert printed_str == ''
257260

258-
info_list = [{
261+
info_list: List[dict] = [{
259262
jupyter_notebook.INFO_KEY_NOTEBOOK_PATH: 'sample/path.ipynb',
260263
jupyter_notebook.INFO_KEY_CODE_CELL_INDEX: 5,
261264
jupyter_notebook.INFO_KEY_FUNC_NAME: 'sample_func_1',
@@ -286,7 +289,7 @@ def test__print_info_list():
286289
assert 'Sample infomation 2.' in printed_str
287290

288291

289-
schema = Schema(
292+
schema: Schema = Schema(
290293
schema={
291294
jupyter_notebook.INFO_KEY_NOTEBOOK_PATH: STR_SCHEMA,
292295
jupyter_notebook.INFO_KEY_CODE_CELL_INDEX: int,
@@ -296,9 +299,9 @@ def test__print_info_list():
296299
}, required=True)
297300

298301

299-
def test_check_jupyter_notebook():
300-
notebook_path = './tests/jupyter/test_jupyter_notebook_py3.ipynb'
301-
info_list = jupyter_notebook.check_jupyter_notebook(
302+
def test_check_jupyter_notebook() -> None:
303+
notebook_path: str = './tests/jupyter/test_jupyter_notebook_py3.ipynb'
304+
info_list: List[dict] = jupyter_notebook.check_jupyter_notebook(
302305
notebook_path=notebook_path,
303306
verbose=0,
304307
ignore_func_name_prefix_list=[],
@@ -309,7 +312,7 @@ def test_check_jupyter_notebook():
309312
schema(info_dict)
310313
assert len(info_list) >= 10
311314

312-
ignore_info_id_list = [
315+
ignore_info_id_list: List[int]= [
313316
info_dict[jupyter_notebook.INFO_KEY_INFO_ID]
314317
for info_dict in info_list]
315318

@@ -340,8 +343,8 @@ def test_check_jupyter_notebook():
340343
assert info_list == []
341344

342345

343-
def test_check_jupyter_notebook_recursively():
344-
info_list = jupyter_notebook.check_jupyter_notebook_recursively(
346+
def test_check_jupyter_notebook_recursively() -> None:
347+
info_list: List[dict] = jupyter_notebook.check_jupyter_notebook_recursively(
345348
dir_path='./numdoclint/',
346349
verbose=jupyter_notebook.VERBOSE_DISABLED,
347350
ignore_func_name_prefix_list=[],
@@ -358,13 +361,13 @@ def test_check_jupyter_notebook_recursively():
358361
assert info_list
359362
for info_dict in info_list:
360363
schema(info_dict)
361-
notebook_path_list = [
364+
notebook_path_list: List[str] = [
362365
info_dict[jupyter_notebook.INFO_KEY_NOTEBOOK_PATH]
363366
for info_dict in info_list]
364-
unique_path_list = list(set(notebook_path_list))
367+
unique_path_list: List[str] = list(set(notebook_path_list))
365368
assert len(unique_path_list) > 1
366369

367-
ignore_info_id_list = [
370+
ignore_info_id_list: List[int] = [
368371
info_dict[jupyter_notebook.INFO_KEY_INFO_ID]
369372
for info_dict in info_list]
370373

0 commit comments

Comments
 (0)