Skip to content

Commit 27c7894

Browse files
authored
Merge pull request #245 from tisnik/lcore-303-fixed-issues-found-by-pyright
LCORE-303: fixed issues found by pyright
2 parents 8833716 + 3a94da0 commit 27c7894

File tree

9 files changed

+71
-38
lines changed

9 files changed

+71
-38
lines changed

.github/workflows/pylint.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ jobs:
2020
- name: Install dependencies
2121
run: uv sync
2222
- name: Python linter
23-
run: uv run pylint src
23+
run: uv run pylint src tests

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ black:
6060
uv run black --check .
6161

6262
pylint:
63-
uv run pylint src
63+
uv run pylint src tests
6464

6565
pyright:
6666
uv run pyright src

tests/unit/app/endpoints/test_models.py

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

55
import pytest
66

7-
from fastapi import HTTPException, status
7+
from fastapi import HTTPException, Request, status
88

99
from app.endpoints.models import models_endpoint_handler
1010
from configuration import AppConfig
@@ -19,7 +19,13 @@ def test_models_endpoint_handler_configuration_not_loaded(mocker):
1919
)
2020
mocker.patch("app.endpoints.models.configuration", None)
2121

22-
request = None
22+
request = Request(
23+
scope={
24+
"type": "http",
25+
"headers": [(b"authorization", b"Bearer invalid-token")],
26+
}
27+
)
28+
2329
with pytest.raises(HTTPException) as e:
2430
models_endpoint_handler(request)
2531
assert e.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR
@@ -58,7 +64,12 @@ def test_models_endpoint_handler_improper_llama_stack_configuration(mocker):
5864
return_value=None,
5965
)
6066

61-
request = None
67+
request = Request(
68+
scope={
69+
"type": "http",
70+
"headers": [(b"authorization", b"Bearer invalid-token")],
71+
}
72+
)
6273
with pytest.raises(HTTPException) as e:
6374
models_endpoint_handler(request)
6475
assert e.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR
@@ -91,8 +102,14 @@ def test_models_endpoint_handler_configuration_loaded():
91102
cfg = AppConfig()
92103
cfg.init_from_dict(config_dict)
93104

105+
request = Request(
106+
scope={
107+
"type": "http",
108+
"headers": [(b"authorization", b"Bearer invalid-token")],
109+
}
110+
)
111+
94112
with pytest.raises(HTTPException) as e:
95-
request = None
96113
models_endpoint_handler(request)
97114
assert e.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR
98115
assert e.detail["response"] == "Unable to connect to Llama Stack"
@@ -132,6 +149,11 @@ def test_models_endpoint_handler_unable_to_retrieve_models_list(mocker):
132149
mock_config = mocker.Mock()
133150
mocker.patch("app.endpoints.models.configuration", mock_config)
134151

135-
request = None
152+
request = Request(
153+
scope={
154+
"type": "http",
155+
"headers": [(b"authorization", b"Bearer invalid-token")],
156+
}
157+
)
136158
response = models_endpoint_handler(request)
137159
assert response is not None

tests/unit/auth/test_auth.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,23 @@
88

99
def test_get_auth_dependency_noop():
1010
"""Test getting Noop authentication dependency."""
11+
assert configuration.authentication_configuration is not None
1112
configuration.authentication_configuration.module = AUTH_MOD_NOOP
1213
auth_dependency = get_auth_dependency()
1314
assert isinstance(auth_dependency, noop.NoopAuthDependency)
1415

1516

1617
def test_get_auth_dependency_noop_with_token():
1718
"""Test getting Noop with token authentication dependency."""
19+
assert configuration.authentication_configuration is not None
1820
configuration.authentication_configuration.module = AUTH_MOD_NOOP_WITH_TOKEN
1921
auth_dependency = get_auth_dependency()
2022
assert isinstance(auth_dependency, noop_with_token.NoopWithTokenAuthDependency)
2123

2224

2325
def test_get_auth_dependency_k8s():
2426
"""Test getting K8s authentication dependency."""
27+
assert configuration.authentication_configuration is not None
2528
configuration.authentication_configuration.module = AUTH_MOD_K8S
2629
auth_dependency = get_auth_dependency()
2730
assert isinstance(auth_dependency, k8s.K8SAuthDependency)

tests/unit/auth/test_utils.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
"""Unit tests for functions defined in auth/utils.py"""
22

33
from fastapi import HTTPException
4+
from starlette.datastructures import Headers
5+
46
from auth.utils import extract_user_token
57

68

79
def test_extract_user_token():
810
"""Test extracting user token from headers."""
9-
headers = {"Authorization": "Bearer abcdef123"}
11+
headers = Headers({"Authorization": "Bearer abcdef123"})
1012
token = extract_user_token(headers)
1113
assert token == "abcdef123"
1214

1315

1416
def test_extract_user_token_no_header():
1517
"""Test extracting user token when no Authorization header is present."""
16-
headers = {}
18+
headers = Headers({})
1719
try:
1820
extract_user_token(headers)
1921
except HTTPException as exc:
@@ -23,7 +25,7 @@ def test_extract_user_token_no_header():
2325

2426
def test_extract_user_token_invalid_format():
2527
"""Test extracting user token with invalid Authorization header format."""
26-
headers = {"Authorization": "InvalidFormat"}
28+
headers = Headers({"Authorization": "InvalidFormat"})
2729
try:
2830
extract_user_token(headers)
2931
except HTTPException as exc:

tests/unit/models/test_config.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,9 @@ def test_user_data_collection_data_collector_wrong_configuration() -> None:
187187
def test_tls_configuration() -> None:
188188
"""Test the TLS configuration."""
189189
cfg = TLSConfiguration(
190-
tls_certificate_path="tests/configuration/server.crt",
191-
tls_key_path="tests/configuration/server.key",
192-
tls_key_password="tests/configuration/password",
190+
tls_certificate_path=Path("tests/configuration/server.crt"),
191+
tls_key_path=Path("tests/configuration/server.key"),
192+
tls_key_password=Path("tests/configuration/password"),
193193
)
194194
assert cfg is not None
195195
assert cfg.tls_certificate_path == Path("tests/configuration/server.crt")
@@ -201,59 +201,59 @@ def test_tls_configuration_wrong_certificate_path() -> None:
201201
"""Test the TLS configuration loading when some path is broken."""
202202
with pytest.raises(ValueError, match="Path does not point to a file"):
203203
TLSConfiguration(
204-
tls_certificate_path="this-is-wrong",
205-
tls_key_path="tests/configuration/server.key",
206-
tls_key_password="tests/configuration/password",
204+
tls_certificate_path=Path("this-is-wrong"),
205+
tls_key_path=Path("tests/configuration/server.key"),
206+
tls_key_password=Path("tests/configuration/password"),
207207
)
208208

209209

210210
def test_tls_configuration_wrong_key_path() -> None:
211211
"""Test the TLS configuration loading when some path is broken."""
212212
with pytest.raises(ValueError, match="Path does not point to a file"):
213213
TLSConfiguration(
214-
tls_certificate_path="tests/configurationserver.crt",
215-
tls_key_path="this-is-wrong",
216-
tls_key_password="tests/configuration/password",
214+
tls_certificate_path=Path("tests/configurationserver.crt"),
215+
tls_key_path=Path("this-is-wrong"),
216+
tls_key_password=Path("tests/configuration/password"),
217217
)
218218

219219

220220
def test_tls_configuration_wrong_password_path() -> None:
221221
"""Test the TLS configuration loading when some path is broken."""
222222
with pytest.raises(ValueError, match="Path does not point to a file"):
223223
TLSConfiguration(
224-
tls_certificate_path="tests/configurationserver.crt",
225-
tls_key_path="tests/configuration/server.key",
226-
tls_key_password="this-is-wrong",
224+
tls_certificate_path=Path("tests/configurationserver.crt"),
225+
tls_key_path=Path("tests/configuration/server.key"),
226+
tls_key_password=Path("this-is-wrong"),
227227
)
228228

229229

230230
def test_tls_configuration_certificate_path_to_directory() -> None:
231231
"""Test the TLS configuration loading when some path points to a directory."""
232232
with pytest.raises(ValueError, match="Path does not point to a file"):
233233
TLSConfiguration(
234-
tls_certificate_path="tests/",
235-
tls_key_path="tests/configuration/server.key",
236-
tls_key_password="tests/configuration/password",
234+
tls_certificate_path=Path("tests/"),
235+
tls_key_path=Path("tests/configuration/server.key"),
236+
tls_key_password=Path("tests/configuration/password"),
237237
)
238238

239239

240240
def test_tls_configuration_key_path_to_directory() -> None:
241241
"""Test the TLS configuration loading when some path points to a directory."""
242242
with pytest.raises(ValueError, match="Path does not point to a file"):
243243
TLSConfiguration(
244-
tls_certificate_path="tests/configurationserver.crt",
245-
tls_key_path="tests/",
246-
tls_key_password="tests/configuration/password",
244+
tls_certificate_path=Path("tests/configurationserver.crt"),
245+
tls_key_path=Path("tests/"),
246+
tls_key_password=Path("tests/configuration/password"),
247247
)
248248

249249

250250
def test_tls_configuration_password_path_to_directory() -> None:
251251
"""Test the TLS configuration loading when some path points to a directory."""
252252
with pytest.raises(ValueError, match="Path does not point to a file"):
253253
TLSConfiguration(
254-
tls_certificate_path="tests/configurationserver.crt",
255-
tls_key_path="tests/configuration/server.key",
256-
tls_key_password="tests/",
254+
tls_certificate_path=Path("tests/configurationserver.crt"),
255+
tls_key_path=Path("tests/configuration/server.key"),
256+
tls_key_password=Path("tests/"),
257257
)
258258

259259

@@ -283,13 +283,13 @@ def test_model_context_protocol_server_required_fields() -> None:
283283
"""Test that ModelContextProtocolServer requires name and url."""
284284

285285
with pytest.raises(ValidationError):
286-
ModelContextProtocolServer()
286+
ModelContextProtocolServer() # pyright: ignore
287287

288288
with pytest.raises(ValidationError):
289-
ModelContextProtocolServer(name="test-server")
289+
ModelContextProtocolServer(name="test-server") # pyright: ignore
290290

291291
with pytest.raises(ValidationError):
292-
ModelContextProtocolServer(url="http://localhost:8080")
292+
ModelContextProtocolServer(url="http://localhost:8080") # pyright: ignore
293293

294294

295295
def test_configuration_empty_mcp_servers() -> None:

tests/unit/models/test_responses.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,12 @@ def test_constructor(self) -> None:
6262
def test_constructor_fields_required(self) -> None:
6363
"""Test the AuthorizedResponse constructor."""
6464
with pytest.raises(ValidationError):
65-
AuthorizedResponse(username="testuser")
65+
AuthorizedResponse(username="testuser") # pyright: ignore
6666

6767
with pytest.raises(ValidationError):
68-
AuthorizedResponse(user_id="123e4567-e89b-12d3-a456-426614174000")
68+
AuthorizedResponse(
69+
user_id="123e4567-e89b-12d3-a456-426614174000"
70+
) # pyright: ignore
6971

7072

7173
class TestUnauthorizedResponse:
@@ -81,4 +83,4 @@ def test_constructor(self) -> None:
8183
def test_constructor_fields_required(self) -> None:
8284
"""Test the UnauthorizedResponse constructor."""
8385
with pytest.raises(Exception):
84-
UnauthorizedResponse()
86+
UnauthorizedResponse() # pyright: ignore

tests/unit/utils/test_checks.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Unit tests for functions defined in utils/checks module."""
22

33
import os
4+
from pathlib import Path
45
from unittest.mock import patch
56

67
import pytest
@@ -69,7 +70,7 @@ def test_file_check_existing_file(input_file):
6970
def test_file_check_non_existing_file():
7071
"""Test the function file_check for non existing file."""
7172
with pytest.raises(checks.InvalidConfigurationError):
72-
checks.file_check("does-not-exists", "description")
73+
checks.file_check(Path("does-not-exists"), "description")
7374

7475

7576
def test_file_check_not_readable_file(input_file):

tests/unit/utils/test_types.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ def test_get_tool_parser_when_model_id_starts_with_granite(self):
2828
def test_get_tool_calls_from_completion_message_when_none(self):
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")
31+
assert tool_parser is not None, "tool parser was not returned"
3132
assert tool_parser.get_tool_calls(None) == [], "get_tool_calls should return []"
3233

3334
def test_get_tool_calls_from_completion_message_when_not_none(self):
3435
"""Test that get_tool_calls returns an empty array when CompletionMessage has no tool_calls.""" # pylint: disable=line-too-long
3536
tool_parser = GraniteToolParser.get_parser("granite-3.3-8b-instruct")
37+
assert tool_parser is not None, "tool parser was not returned"
3638
completion_message = Mock()
3739
completion_message.tool_calls = []
3840
assert not tool_parser.get_tool_calls(
@@ -42,6 +44,7 @@ def test_get_tool_calls_from_completion_message_when_not_none(self):
4244
def test_get_tool_calls_from_completion_message_when_message_has_tool_calls(self):
4345
"""Test that get_tool_calls returns the tool_calls when CompletionMessage has tool_calls."""
4446
tool_parser = GraniteToolParser.get_parser("granite-3.3-8b-instruct")
47+
assert tool_parser is not None, "tool parser was not returned"
4548
completion_message = Mock()
4649
tool_calls = [Mock(tool_name="tool-1"), Mock(tool_name="tool-2")]
4750
completion_message.tool_calls = tool_calls

0 commit comments

Comments
 (0)