Skip to content

Commit d977a2b

Browse files
authored
Merge pull request lightspeed-core#632 from tisnik/lcore-740-more-type-hints
LCORE-740: More type hints
2 parents 72ea8cd + 383f0b2 commit d977a2b

2 files changed

Lines changed: 27 additions & 23 deletions

File tree

tests/unit/utils/test_checks.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
from types import ModuleType
66
from unittest.mock import patch
77

8+
from typing import Any
9+
810
import pytest
911

1012
from utils import checks
1113

1214

1315
@pytest.fixture(name="input_file")
14-
def input_file_fixture(tmp_path):
16+
def input_file_fixture(tmp_path: Path) -> str:
1517
"""Create file manually using the tmp_path fixture."""
1618
filename = os.path.join(tmp_path, "mydoc.csv")
1719
with open(filename, "wt", encoding="utf-8") as fout:
@@ -20,17 +22,17 @@ def input_file_fixture(tmp_path):
2022

2123

2224
@pytest.fixture(name="input_directory")
23-
def input_directory_fixture(tmp_path):
25+
def input_directory_fixture(tmp_path: Path) -> str:
2426
"""Create directory manually using the tmp_path fixture."""
2527
dirname = os.path.join(tmp_path, "mydir")
2628
os.mkdir(dirname)
2729
return dirname
2830

2931

30-
def test_get_attribute_from_file_no_record():
32+
def test_get_attribute_from_file_no_record() -> None:
3133
"""Test the get_attribute_from_file function when record is not in dictionary."""
3234
# no data
33-
d = {}
35+
d: dict[str, Any] = {}
3436

3537
# non-existing key
3638
key = ""
@@ -43,7 +45,7 @@ def test_get_attribute_from_file_no_record():
4345
assert value is None
4446

4547

46-
def test_get_attribute_from_file_proper_record(input_file):
48+
def test_get_attribute_from_file_proper_record(input_file: str) -> None:
4749
"""Test the get_attribute_from_file function when record is present in dictionary."""
4850
# existing key
4951
key = "my_file"
@@ -57,7 +59,7 @@ def test_get_attribute_from_file_proper_record(input_file):
5759
assert value == "some content!"
5860

5961

60-
def test_get_attribute_from_file_improper_filename():
62+
def test_get_attribute_from_file_improper_filename() -> None:
6163
"""Test the get_attribute_from_file when the file does not exist."""
6264
# existing key
6365
key = "my_file"
@@ -70,26 +72,26 @@ def test_get_attribute_from_file_improper_filename():
7072
checks.get_attribute_from_file(d, "my_file")
7173

7274

73-
def test_file_check_existing_file(input_file):
75+
def test_file_check_existing_file(input_file: str) -> None:
7476
"""Test the function file_check for existing file."""
7577
# just call the function, it should not raise an exception
7678
checks.file_check(input_file, "description")
7779

7880

79-
def test_file_check_non_existing_file():
81+
def test_file_check_non_existing_file() -> None:
8082
"""Test the function file_check for non existing file."""
8183
with pytest.raises(checks.InvalidConfigurationError):
8284
checks.file_check(Path("does-not-exists"), "description")
8385

8486

85-
def test_file_check_not_readable_file(input_file):
87+
def test_file_check_not_readable_file(input_file: str) -> None:
8688
"""Test the function file_check for not readable file."""
8789
with patch("os.access", return_value=False):
8890
with pytest.raises(checks.InvalidConfigurationError):
8991
checks.file_check(input_file, "description")
9092

9193

92-
def test_directory_check_non_existing_directory():
94+
def test_directory_check_non_existing_directory() -> None:
9395
"""Test the function directory_check skips non-existing directory."""
9496
# just call the function, it should not raise an exception
9597
checks.directory_check(
@@ -101,15 +103,15 @@ def test_directory_check_non_existing_directory():
101103
)
102104

103105

104-
def test_directory_check_existing_writable_directory(input_directory):
106+
def test_directory_check_existing_writable_directory(input_directory: str) -> None:
105107
"""Test the function directory_check checks directory."""
106108
# just call the function, it should not raise an exception
107109
checks.directory_check(
108110
input_directory, must_exists=True, must_be_writable=True, desc="foobar"
109111
)
110112

111113

112-
def test_directory_check_non_a_directory(input_file):
114+
def test_directory_check_non_a_directory(input_file: str) -> None:
113115
"""Test the function directory_check checks directory."""
114116
# pass a filename not a directory name
115117
with pytest.raises(checks.InvalidConfigurationError):
@@ -118,7 +120,7 @@ def test_directory_check_non_a_directory(input_file):
118120
)
119121

120122

121-
def test_directory_check_existing_non_writable_directory(input_directory):
123+
def test_directory_check_existing_non_writable_directory(input_directory: str) -> None:
122124
"""Test the function directory_check checks directory."""
123125
with patch("os.access", return_value=False):
124126
with pytest.raises(checks.InvalidConfigurationError):
@@ -127,7 +129,7 @@ def test_directory_check_existing_non_writable_directory(input_directory):
127129
)
128130

129131

130-
def test_import_python_module_success():
132+
def test_import_python_module_success() -> None:
131133
"""Test importing a Python module."""
132134
module_path = "tests/profiles/test/profile.py"
133135
module_name = "profile"
@@ -136,7 +138,7 @@ def test_import_python_module_success():
136138
assert isinstance(result, ModuleType)
137139

138140

139-
def test_import_python_module_error():
141+
def test_import_python_module_error() -> None:
140142
"""Test importing a Python module that is a .txt file."""
141143
module_path = "tests/profiles/test_two/test.txt"
142144
module_name = "profile"
@@ -145,7 +147,7 @@ def test_import_python_module_error():
145147
assert result is None
146148

147149

148-
def test_is_valid_profile():
150+
def test_is_valid_profile() -> None:
149151
"""Test if an imported profile is valid."""
150152
module_path = "tests/profiles/test/profile.py"
151153
module_name = "profile"
@@ -157,7 +159,7 @@ def test_is_valid_profile():
157159
assert result is True
158160

159161

160-
def test_invalid_profile():
162+
def test_invalid_profile() -> None:
161163
"""Test if an imported profile is valid (expect invalid)"""
162164
module_path = "tests/profiles/test_three/profile.py"
163165
module_name = "profile"

tests/unit/utils/test_types.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,30 @@
88
class TestGraniteToolParser:
99
"""Unit tests for functions defined in utils/types.py."""
1010

11-
def test_get_tool_parser_when_model_is_is_not_granite(self):
11+
def test_get_tool_parser_when_model_is_is_not_granite(self) -> None:
1212
"""Test that the tool_parser is None when model_id is not a granite model."""
1313
assert (
1414
GraniteToolParser.get_parser("ollama3.3") is None
1515
), "tool_parser should be None"
1616

17-
def test_get_tool_parser_when_model_id_does_not_start_with_granite(self):
17+
def test_get_tool_parser_when_model_id_does_not_start_with_granite(self) -> None:
1818
"""Test that the tool_parser is None when model_id does not start with granite."""
1919
assert (
2020
GraniteToolParser.get_parser("a-fine-trained-granite-model") is None
2121
), "tool_parser should be None"
2222

23-
def test_get_tool_parser_when_model_id_starts_with_granite(self):
23+
def test_get_tool_parser_when_model_id_starts_with_granite(self) -> None:
2424
"""Test that the tool_parser is not None when model_id starts with granite."""
2525
tool_parser = GraniteToolParser.get_parser("granite-3.3-8b-instruct")
2626
assert tool_parser is not None, "tool_parser should not be None"
2727

28-
def test_get_tool_calls_from_completion_message_when_none(self):
28+
def test_get_tool_calls_from_completion_message_when_none(self) -> None:
2929
"""Test that get_tool_calls returns an empty array when CompletionMessage is None."""
3030
tool_parser = GraniteToolParser.get_parser("granite-3.3-8b-instruct")
3131
assert tool_parser is not None, "tool parser was not returned"
3232
assert tool_parser.get_tool_calls(None) == [], "get_tool_calls should return []"
3333

34-
def test_get_tool_calls_from_completion_message_when_not_none(self):
34+
def test_get_tool_calls_from_completion_message_when_not_none(self) -> None:
3535
"""Test that get_tool_calls returns an empty array when CompletionMessage has no tool_calls.""" # pylint: disable=line-too-long
3636
tool_parser = GraniteToolParser.get_parser("granite-3.3-8b-instruct")
3737
assert tool_parser is not None, "tool parser was not returned"
@@ -41,7 +41,9 @@ def test_get_tool_calls_from_completion_message_when_not_none(self):
4141
completion_message
4242
), "get_tool_calls should return []"
4343

44-
def test_get_tool_calls_from_completion_message_when_message_has_tool_calls(self):
44+
def test_get_tool_calls_from_completion_message_when_message_has_tool_calls(
45+
self,
46+
) -> None:
4547
"""Test that get_tool_calls returns the tool_calls when CompletionMessage has tool_calls."""
4648
tool_parser = GraniteToolParser.get_parser("granite-3.3-8b-instruct")
4749
assert tool_parser is not None, "tool parser was not returned"

0 commit comments

Comments
 (0)