Skip to content

Commit f829ab3

Browse files
committed
fix: mypy error in unit testing code
Add tests/ as another code for mypy to check.
1 parent 4a024a3 commit f829ab3

File tree

6 files changed

+52
-36
lines changed

6 files changed

+52
-36
lines changed

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ dependencies = [
2121
"click",
2222
"tomli",
2323
"GitPython",
24+
"typing_extensions >=4.12, <5",
2425
'importlib-metadata; python_version>="3.10"',
2526
]
2627

@@ -65,7 +66,7 @@ ignore-init-module-imports = true
6566
known-first-party = ["grading_lib"]
6667

6768
[tool.mypy]
68-
files = ["grading_lib"]
69+
files = ["grading_lib", "tests"]
6970
show_error_codes = true
7071
pretty = true
7172
strict = true

tests/test_cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from grading_lib.cli.internal import collect_autograding_tests_command
99

1010

11-
def test_collect_autograding_tests_command():
11+
def test_collect_autograding_tests_command() -> None:
1212
runner = CliRunner()
1313
with runner.isolated_filesystem():
1414
problem_path = Path("lorem-ipsum")
@@ -46,7 +46,7 @@ def test_collect_autograding_tests_command():
4646
assert data["tests"][0]["name"] == "lorem-ipsum - cast some spells"
4747

4848

49-
def test_summary_command():
49+
def test_summary_command() -> None:
5050
runner = CliRunner()
5151

5252
# When there is no problem,

tests/test_common.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,35 @@
1616
)
1717

1818

19-
def test_is_debug_mode():
19+
def test_is_debug_mode() -> None:
2020
vals_for_true = ["1", "True", "T", "On", "ON", "t", "true"]
2121
for val in vals_for_true:
2222
os.environ["test_DEBUG"] = val
2323
assert is_debug_mode("test_DEBUG")
2424

2525
vals_for_false = ["", None, "False", "false", "lorem", "0"]
26-
for val in vals_for_false:
27-
if val is None:
26+
for v in vals_for_false:
27+
if v is None:
2828
del os.environ["test_DEBUG"]
2929
else:
30-
os.environ["test_DEBUG"] = val
30+
os.environ["test_DEBUG"] = v
3131
assert is_debug_mode("test_DEBUG") is False
3232

3333

34-
def test_get_seed_from_env():
34+
def test_get_seed_from_env() -> None:
3535
"""
3636
If value is set, should properly parse and use it.
3737
3838
If not, the value should be the scaled time (to avoid same seed
3939
for multiple tests). Does not currently check if the return value is
4040
the scaled time.
4141
"""
42-
val = 123
43-
os.environ["test_SEED"] = str(val)
44-
assert get_seed_from_env("test_SEED") == val
42+
val1 = 123
43+
os.environ["test_SEED"] = str(val1)
44+
assert get_seed_from_env("test_SEED") == val1
4545

46-
val = "123not-a-number"
47-
os.environ["test_SEED"] = str(val)
46+
val2 = "123not-a-number"
47+
os.environ["test_SEED"] = str(val2)
4848
res = get_seed_from_env("test_SEED")
4949
assert isinstance(res, int)
5050
assert res != 0
@@ -58,7 +58,7 @@ def a_temp_file():
5858
f.close()
5959

6060

61-
def test_has_file_changed(a_temp_file):
61+
def test_has_file_changed(a_temp_file) -> None:
6262
path = Path(a_temp_file.name)
6363

6464
# Take snapshot of the mtime.
@@ -75,7 +75,7 @@ def test_has_file_changed(a_temp_file):
7575
assert not has_file_changed(last_known_mtime, path)
7676

7777

78-
def test_populate_folder_with_filenames():
78+
def test_populate_folder_with_filenames() -> None:
7979
with tempfile.TemporaryDirectory() as tmpdir:
8080
tmpdir_path = Path(tmpdir)
8181

@@ -98,13 +98,13 @@ def test_populate_folder_with_filenames():
9898
assert result_names == expected_files
9999

100100

101-
def test_run_executable():
101+
def test_run_executable() -> None:
102102
cmd_result = run_executable(["git", "version"])
103103
assert cmd_result.success
104104
assert "git version" in cmd_result.output
105105

106106

107-
def test_BaseTestCase():
107+
def test_BaseTestCase() -> None:
108108
"""Tests for the BaseTestCase."""
109109

110110
# When with_temporary_dir is not specified,
@@ -126,16 +126,16 @@ class ChildClsWithTempDir(BaseTestCase):
126126
instance.tearDown()
127127

128128

129-
def test_BaseTestCase_assertArchiveFileIsGzip():
129+
def test_BaseTestCase_assertArchiveFileIsGzip() -> None:
130130
class ChildClsWithTempDir(BaseTestCase):
131131
with_temporary_dir = True
132132

133133
instance = ChildClsWithTempDir()
134134
instance.setUp()
135135

136136
try:
137-
with tempfile.TemporaryFile() as tmp_file:
137+
with tempfile.NamedTemporaryFile() as tmp_file:
138138
with pytest.raises(AssertionError):
139-
instance.assertArchiveFileIsGzip(tmp_file)
139+
instance.assertArchiveFileIsGzip(tmp_file.name)
140140
finally:
141141
instance.tearDown()

tests/test_makefile.py

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
@pytest.fixture
7-
def makefile_1():
7+
def makefile_1() -> str:
88
"""
99
First line is empty and two equal signs on a var-def line.
1010
"""
@@ -18,7 +18,7 @@ def makefile_1():
1818

1919

2020
@pytest.fixture
21-
def makefile_2():
21+
def makefile_2() -> str:
2222
return """
2323
# https://www.gnu.org/software/make/manual/html_node/Phony-Targets.html
2424
@@ -40,7 +40,7 @@ def makefile_2():
4040

4141

4242
@pytest.fixture
43-
def makefile_3():
43+
def makefile_3() -> str:
4444
"""Continuous Makefile."""
4545
return """
4646
a: e f
@@ -52,7 +52,7 @@ def makefile_3():
5252

5353

5454
@pytest.fixture
55-
def makefile_var_defs():
55+
def makefile_var_defs() -> str:
5656
"""
5757
Various variable definitions.
5858
"""
@@ -74,7 +74,7 @@ def makefile_var_defs():
7474
"""
7575

7676

77-
def test_makefile_from_text(makefile_1):
77+
def test_makefile_from_text(makefile_1: str) -> None:
7878
"""
7979
A verification test for `:` showing up twice.
8080
"""
@@ -85,7 +85,7 @@ def test_makefile_from_text(makefile_1):
8585
pytest.fail("Exception raises while parsing a makefile.")
8686

8787

88-
def test_makefile_from_text_2(makefile_2):
88+
def test_makefile_from_text_2(makefile_2) -> None:
8989
try:
9090
mk = Makefile.from_text(makefile_2)
9191
except Exception:
@@ -95,27 +95,39 @@ def test_makefile_from_text_2(makefile_2):
9595
assert not mk.rules[1].is_empty()
9696

9797

98-
def test_makefile_from_text_3(makefile_3):
98+
def test_makefile_from_text_3(makefile_3) -> None:
9999
try:
100100
mk = Makefile.from_text(makefile_3)
101101
except Exception:
102102
pytest.fail("Exception raises while parsing a makefile.")
103103

104104
assert mk.has_rule("a")
105-
assert mk.get_rule("a").prerequisites == ["e", "f"]
105+
rule = mk.get_rule("a")
106+
assert rule is not None
107+
if rule:
108+
assert rule.prerequisites == ["e", "f"]
106109

107110
assert mk.has_rule("b")
108-
assert mk.get_rule("b").prerequisites == ["g"]
111+
rule = mk.get_rule("b")
112+
assert rule is not None
113+
if rule:
114+
assert rule.prerequisites == ["g"]
109115

110116
assert mk.has_rule("c")
111-
assert mk.get_rule("c").prerequisites == ["h"]
112-
assert not mk.get_rule("c").is_empty()
117+
rule = mk.get_rule("c")
118+
assert rule is not None
119+
if rule:
120+
assert rule.prerequisites == ["h"]
121+
assert not rule.is_empty()
113122

114123
assert mk.has_rule("d")
115-
assert mk.get_rule("d").prerequisites == ["i", "j"]
124+
rule = mk.get_rule("d")
125+
assert rule is not None
126+
if rule:
127+
assert rule.prerequisites == ["i", "j"]
116128

117129

118-
def test_makefile_var_defs_parsing(makefile_var_defs):
130+
def test_makefile_var_defs_parsing(makefile_var_defs) -> None:
119131
try:
120132
_ = Makefile.from_text(makefile_var_defs)
121133
except Exception:

tests/test_repository.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ def an_empty_folder():
1717
temp_dir.cleanup()
1818

1919

20-
def test_Repository_init_with_an_empty_folder(an_empty_folder):
20+
def test_Repository_init_with_an_empty_folder(an_empty_folder) -> None:
2121
repo = Repository(an_empty_folder.name)
2222
assert isinstance(repo.working_tree_dir, Path)
2323

2424

25-
def test_Repository_create_and_add_random_file(an_empty_folder):
25+
def test_Repository_create_and_add_random_file(an_empty_folder) -> None:
2626
repo = Repository(an_empty_folder.name)
2727
assert isinstance(repo.working_tree_dir, Path)
2828

tox.ini

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ env_list =
77
typing
88

99
[testenv]
10-
description = run unit tests
10+
description = run unit testing tests
1111
package = wheel
1212
wheel_build_env = .pkg
1313
deps = pytest
@@ -20,5 +20,8 @@ skip_install = true
2020
commands = ruff check
2121

2222
[testenv:typing]
23+
description = run mypy against the codebase and unit testing code
24+
skip_install = true
2325
deps = mypy
26+
pytest
2427
commands = mypy

0 commit comments

Comments
 (0)