Skip to content

Commit 91338d5

Browse files
TuTomaszAddressXceptionkeithrfung
authored
Removed existing T imports from utils and made T instances private (#183)
Made U instances private for consistency Co-authored-by: Matt Wilhelm <[email protected]> Co-authored-by: Keith Fung <[email protected]>
1 parent 0281fc7 commit 91338d5

File tree

4 files changed

+40
-35
lines changed

4 files changed

+40
-35
lines changed

src/electionguard/data_store.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,26 @@
1212
)
1313

1414

15-
T = TypeVar("T")
16-
U = TypeVar("U")
15+
_T = TypeVar("_T")
16+
_U = TypeVar("_U")
1717

1818

19-
class DataStore(Generic[T, U], Iterable):
19+
class DataStore(Generic[_T, _U], Iterable):
2020
"""
2121
A lightweight convenience wrapper around a dictionary for data storage.
2222
This implementation defines the common interface used to access stored
2323
state elements.
2424
"""
2525

26-
_store: Dict[T, U]
26+
_store: Dict[_T, _U]
2727

2828
def __init__(self) -> None:
2929
self._store = {}
3030

3131
def __iter__(self) -> Iterator:
3232
return iter(self._store.items())
3333

34-
def all(self) -> List[Optional[U]]:
34+
def all(self) -> List[Optional[_U]]:
3535
"""
3636
Get all `CiphertextAcceptedBallot` from the store
3737
"""
@@ -43,22 +43,22 @@ def clear(self) -> None:
4343
"""
4444
self._store.clear()
4545

46-
def get(self, key: T) -> Optional[U]:
46+
def get(self, key: _T) -> Optional[_U]:
4747
"""
4848
Get value in store
4949
:param key: key
5050
:return: value if found
5151
"""
5252
return self._store.get(key)
5353

54-
def items(self) -> Iterable[Tuple[T, U]]:
54+
def items(self) -> Iterable[Tuple[_T, _U]]:
5555
"""
5656
Gets all items in store as list
5757
:return: List of (key, value)
5858
"""
5959
return self._store.items()
6060

61-
def keys(self) -> Iterable[T]:
61+
def keys(self) -> Iterable[_T]:
6262
"""
6363
Gets all keys in store as list
6464
:return: List of keys
@@ -72,7 +72,7 @@ def __len__(self) -> int:
7272
"""
7373
return len(self._store)
7474

75-
def pop(self, key: T) -> Optional[U]:
75+
def pop(self, key: _T) -> Optional[_U]:
7676
"""
7777
Pop an object from the store if it exists.
7878
:param key: key
@@ -81,31 +81,31 @@ def pop(self, key: T) -> Optional[U]:
8181
return self._store.pop(key)
8282
return None
8383

84-
def set(self, key: T, value: U) -> None:
84+
def set(self, key: _T, value: _U) -> None:
8585
"""
8686
Create or update a new value in store
8787
:param key: key
8888
:param value: value
8989
"""
9090
self._store[key] = value
9191

92-
def values(self) -> Iterable[U]:
92+
def values(self) -> Iterable[_U]:
9393
"""
9494
Gets all values in store as list
9595
:return: List of values
9696
"""
9797
return self._store.values()
9898

9999

100-
class ReadOnlyDataStore(Generic[T, U], Mapping):
100+
class ReadOnlyDataStore(Generic[_T, _U], Mapping):
101101
"""
102102
A readonly view to a Data store
103103
"""
104104

105-
def __init__(self, data: DataStore[T, U]):
106-
self._data: DataStore[T, U] = data
105+
def __init__(self, data: DataStore[_T, _U]):
106+
self._data: DataStore[_T, _U] = data
107107

108-
def __getitem__(self, key: T) -> Optional[U]:
108+
def __getitem__(self, key: _T) -> Optional[_U]:
109109
return self._data.get(key)
110110

111111
def __len__(self) -> int:

src/electionguard/scheduler.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
from multiprocessing.pool import Pool
66
from psutil import cpu_count
77
import traceback
8-
from typing import Any, Callable, Iterable, List
8+
from typing import Any, Callable, Iterable, List, TypeVar
99

1010
from .logs import log_warning
1111
from .singleton import Singleton
12-
from .utils import T
12+
13+
_T = TypeVar("_T")
1314

1415

1516
class Scheduler(Singleton, AbstractContextManager):
@@ -58,7 +59,7 @@ def schedule(
5859
task: Callable,
5960
arguments: Iterable[Iterable[Any]],
6061
with_shared_resources: bool = False,
61-
) -> List[T]:
62+
) -> List[_T]:
6263
"""
6364
Schedule tasks with list of arguments
6465
:param task: the callable task to execute
@@ -74,7 +75,7 @@ def schedule(
7475

7576
def safe_starmap(
7677
self, pool: Pool, task: Callable, arguments: Iterable[Iterable[Any]]
77-
) -> List[T]:
78+
) -> List[_T]:
7879
"""Safe wrapper around starmap to ensure pool is open"""
7980
try:
8081
return pool.starmap(task, arguments)
@@ -89,7 +90,9 @@ def safe_starmap(
8990
)
9091
return []
9192

92-
def safe_map(self, pool: Pool, task: Callable, arguments: Iterable[Any]) -> List[T]:
93+
def safe_map(
94+
self, pool: Pool, task: Callable, arguments: Iterable[Any]
95+
) -> List[_T]:
9396
"""Safe wrapper around starmap to ensure pool is open"""
9497
try:
9598
return pool.map(task, arguments)

src/electionguard/serializable.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
)
2020

2121
S = TypeVar("S", bound="Serializable")
22-
T = TypeVar("T")
22+
_T = TypeVar("_T")
2323

2424
JSON_FILE_EXTENSION: str = ".json"
2525
WRITE: str = "w"
@@ -165,29 +165,29 @@ def write_json_file(
165165
json_file.write(write_json(object_to_write, strip_privates))
166166

167167

168-
def read_json(data: Any, class_out: Type[T]) -> T:
168+
def read_json(data: Any, class_out: Type[_T]) -> _T:
169169
"""
170170
Deserialize json file to object
171171
:param data: Json file data
172172
:param class_out: Object type
173173
:return: Deserialized object
174174
"""
175175
set_deserializers()
176-
return cast(T, loads(data, class_out))
176+
return cast(_T, loads(data, class_out))
177177

178178

179-
def read_json_object(data: Any, class_out: Type[T]) -> T:
179+
def read_json_object(data: Any, class_out: Type[_T]) -> _T:
180180
"""
181181
Deserialize json file to object
182182
:param data: Json file data
183183
:param class_out: Object type
184184
:return: Deserialized object
185185
"""
186186
set_deserializers()
187-
return cast(T, load(data, class_out))
187+
return cast(_T, load(data, class_out))
188188

189189

190-
def read_json_file(class_out: Type[T], file_name: str, file_path: str = "") -> T:
190+
def read_json_file(class_out: Type[_T], file_name: str, file_path: str = "") -> _T:
191191
"""
192192
Deserialize json file to object
193193
:param class_out: Object type
@@ -199,7 +199,7 @@ def read_json_file(class_out: Type[T], file_name: str, file_path: str = "") -> T
199199
json_file_path: str = path.join(file_path, file_name + JSON_FILE_EXTENSION)
200200
with open(json_file_path, READ) as json_file:
201201
data = json_file.read()
202-
target: T = read_json(data, class_out)
202+
target: _T = read_json(data, class_out)
203203
return target
204204

205205

src/electionguard/utils.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
from re import sub
44
from typing import Callable, Optional, TypeVar
55

6-
T = TypeVar("T")
7-
U = TypeVar("U")
6+
_T = TypeVar("_T")
7+
_U = TypeVar("_U")
88

99

10-
def get_optional(optional: Optional[T]) -> T:
10+
def get_optional(optional: Optional[_T]) -> _T:
1111
"""
1212
General-purpose unwrapping function to handle `Optional`.
1313
Raises an exception if it's actually `None`, otherwise
@@ -18,8 +18,8 @@ def get_optional(optional: Optional[T]) -> T:
1818

1919

2020
def match_optional(
21-
optional: Optional[T], none_func: Callable[[], U], some_func: Callable[[T], U]
22-
) -> U:
21+
optional: Optional[_T], none_func: Callable[[], _U], some_func: Callable[[_T], _U]
22+
) -> _U:
2323
"""
2424
General-purpose pattern-matching function to handle `Optional`.
2525
If it's actually `None`, the `none_func` lambda is called.
@@ -31,7 +31,7 @@ def match_optional(
3131
return some_func(optional)
3232

3333

34-
def get_or_else_optional(optional: Optional[T], alt_value: T) -> T:
34+
def get_or_else_optional(optional: Optional[_T], alt_value: _T) -> _T:
3535
"""
3636
General-purpose getter for `Optional`. If it's `None`, returns the `alt_value`.
3737
Otherwise, returns the contents of `optional`.
@@ -42,7 +42,7 @@ def get_or_else_optional(optional: Optional[T], alt_value: T) -> T:
4242
return optional
4343

4444

45-
def get_or_else_optional_func(optional: Optional[T], func: Callable[[], T]) -> T:
45+
def get_or_else_optional_func(optional: Optional[_T], func: Callable[[], _T]) -> _T:
4646
"""
4747
General-purpose getter for `Optional`. If it's `None`, calls the lambda `func`
4848
and returns its value. Otherwise, returns the contents of `optional`.
@@ -53,7 +53,9 @@ def get_or_else_optional_func(optional: Optional[T], func: Callable[[], T]) -> T
5353
return optional
5454

5555

56-
def flatmap_optional(optional: Optional[T], mapper: Callable[[T], U]) -> Optional[U]:
56+
def flatmap_optional(
57+
optional: Optional[_T], mapper: Callable[[_T], _U]
58+
) -> Optional[_U]:
5759
"""
5860
General-purpose flatmapping on `Optional`. If it's `None`, returns `None` as well,
5961
otherwise returns the lambda applied to the contents.

0 commit comments

Comments
 (0)