Skip to content

Commit 3a397db

Browse files
Use typed dict
1 parent ffaebdf commit 3a397db

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

src/daq_config_server/client.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from collections import defaultdict
33
from logging import Logger, getLogger
44
from pathlib import Path
5-
from typing import TypeVar
5+
from typing import Any, TypeVar
66

77
import requests
88
from cachetools import TTLCache, cachedmethod
@@ -14,13 +14,13 @@
1414

1515
from .constants import ENDPOINTS
1616

17-
T = TypeVar("T", str, bytes, dict) # type: ignore - to allow any type of dict
17+
T = TypeVar("T", str, bytes, dict[Any, Any])
1818

1919

2020
return_type_to_mime_type: dict[type, ValidAcceptHeaders] = defaultdict(
2121
lambda: ValidAcceptHeaders.PLAIN_TEXT,
2222
{
23-
dict: ValidAcceptHeaders.JSON,
23+
dict[Any, Any]: ValidAcceptHeaders.JSON,
2424
str: ValidAcceptHeaders.PLAIN_TEXT,
2525
bytes: ValidAcceptHeaders.RAW_BYTES,
2626
},
@@ -135,7 +135,7 @@ def _get(
135135

136136
def get_file_contents(
137137
self,
138-
file_path: Path,
138+
file_path: Path | str,
139139
desired_return_type: type[T] = str,
140140
reset_cached_result: bool = False,
141141
) -> T:
@@ -155,7 +155,7 @@ def get_file_contents(
155155
Returns:
156156
The file contents, in the format specified.
157157
"""
158-
158+
file_path = Path(file_path)
159159
accept_header = return_type_to_mime_type[desired_return_type]
160160

161161
return TypeAdapter(desired_return_type).validate_python( # type: ignore - to allow any dict
@@ -166,3 +166,8 @@ def get_file_contents(
166166
reset_cached_result=reset_cached_result,
167167
)
168168
)
169+
170+
171+
# thing = ConfigServer("url")
172+
# h = thing.get_file_contents("path", dict)
173+
# print(h)

tests/system_tests/test_client.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import os
3+
from typing import Any
34
from unittest.mock import MagicMock
45

56
import pytest
@@ -67,7 +68,7 @@ def test_read_good_json_as_dict(server: ConfigServer):
6768
assert (
6869
server.get_file_contents(
6970
file_path,
70-
dict,
71+
dict[Any, Any],
7172
)
7273
== expected_response
7374
)
@@ -86,6 +87,6 @@ def test_bad_json_gives_http_error_with_details(server: ConfigServer):
8687
with pytest.raises(requests.exceptions.HTTPError):
8788
server.get_file_contents(
8889
file_path,
89-
dict,
90+
dict[Any, Any],
9091
)
9192
server._log.error.assert_called_once_with(expected_detail)

0 commit comments

Comments
 (0)