Skip to content

Commit d2bad60

Browse files
committed
Fix type definitions across all modules
1 parent e6435c6 commit d2bad60

11 files changed

Lines changed: 69 additions & 36 deletions

File tree

src/tests/test_dictionaries.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,15 @@
1818
import pytest
1919

2020
# ## Local First Party Imports ----
21-
from toolbox_python.collection_types import int_list, int_tuple, str_list, str_tuple
21+
from toolbox_python.collection_types import (
22+
dict_int_str,
23+
dict_str_int,
24+
dict_str_str,
25+
int_list,
26+
int_tuple,
27+
str_list,
28+
str_tuple,
29+
)
2230

2331
# Local Module Imports
2432
from toolbox_python.dictionaries import dict_reverse_keys_and_values
@@ -36,7 +44,7 @@ class TestDictionaries(TestCase):
3644
@classmethod
3745
def setUpClass(cls) -> None:
3846

39-
cls.dict_basic: dict[str, int] = {"a": 1, "b": 2, "c": 3}
47+
cls.dict_basic: dict_str_int = {"a": 1, "b": 2, "c": 3}
4048

4149
cls.dict_iterables: dict[
4250
str, Union[str_list, int_list, str_tuple, int_tuple]
@@ -71,8 +79,8 @@ def setUpClass(cls) -> None:
7179

7280
def test_reverse_dict_one_for_one(self) -> None:
7381
_input = self.dict_basic
74-
_output = dict_reverse_keys_and_values(_input)
75-
_expected = {"1": "a", "2": "b", "3": "c"}
82+
_output: dict_int_str = dict_reverse_keys_and_values(_input)
83+
_expected: dict_str_str = {"1": "a", "2": "b", "3": "c"}
7684
assert _output == _expected
7785

7886
def test_reverse_dict_iterables(self) -> None:
@@ -110,7 +118,7 @@ def test_reverse_dict_iterables_with_duplicates(self) -> None:
110118
def test_reverse_dict_embedded_dicts(self) -> None:
111119
_input = self.dict_with_dicts
112120
_output = dict_reverse_keys_and_values(_input)
113-
_expected = {
121+
_expected: dict_str_str = {
114122
"11": "aa",
115123
"22": "bb",
116124
"33": "cc",

src/tests/test_strings.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@
1111

1212

1313
# ## Python StdLib Imports ----
14-
# Python StdLib Imports
1514
from unittest import TestCase
1615

1716
# ## Local First Party Imports ----
18-
# Local Module Imports
17+
from toolbox_python.collection_types import str_list
1918
from toolbox_python.strings import (
2019
str_contains,
2120
str_contains_all,
@@ -103,14 +102,14 @@ def setUp(self) -> None:
103102

104103
def test_str_separate_number_chars1(self) -> None:
105104
_input = "-12.1grams"
106-
_output: list[str] = str_separate_number_chars(_input)
107-
_expected: list[str] = ["-12.1", "grams"]
105+
_output: str_list = str_separate_number_chars(_input)
106+
_expected: str_list = ["-12.1", "grams"]
108107
assert _output == _expected
109108

110109
def test_str_separate_number_chars2(self) -> None:
111110
_input = "abcd2343 abw34324 abc3243-23A 123"
112-
_output: list[str] = str_separate_number_chars(_input)
113-
_expected: list[str] = [
111+
_output: str_list = str_separate_number_chars(_input)
112+
_expected: str_list = [
114113
"abcd",
115114
"2343",
116115
"abw",

src/toolbox_python/bools.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,21 @@
3333
# ---------------------------------------------------------------------------- #
3434

3535

36+
# ---------------------------------------------------------------------------- #
37+
# Imports ####
38+
# ---------------------------------------------------------------------------- #
39+
40+
41+
# ## Local First Party Imports ----
42+
from toolbox_python.collection_types import str_list
43+
44+
3645
# ---------------------------------------------------------------------------- #
3746
# Exports ####
3847
# ---------------------------------------------------------------------------- #
3948

4049

41-
__all__: list[str] = ["strtobool", "STR_TO_BOOL_MAP"]
50+
__all__: str_list = ["strtobool", "STR_TO_BOOL_MAP"]
4251

4352

4453
# ---------------------------------------------------------------------------- #

src/toolbox_python/checkers.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,20 @@
2525
from typeguard import typechecked
2626

2727
# ## Local First Party Imports ----
28-
from toolbox_python.collection_types import any_collection, scalar, str_collection
28+
from toolbox_python.collection_types import (
29+
any_collection,
30+
scalar,
31+
str_collection,
32+
str_list,
33+
)
2934

3035

3136
## --------------------------------------------------------------------------- #
3237
## Exports ####
3338
## --------------------------------------------------------------------------- #
3439

3540

36-
__all__: list[str] = [
41+
__all__: str_list = [
3742
"all_elements_contains",
3843
"any_element_contains",
3944
"assert_all_in",
@@ -638,7 +643,7 @@ def assert_all_values_of_type(
638643
if isinstance(check_type, type):
639644
msg += f"Must be '{check_type}'"
640645
else:
641-
types: list[str] = [f"'{typ.__name__}'" for typ in check_type]
646+
types: str_list = [f"'{typ.__name__}'" for typ in check_type]
642647
msg += f"Must be: {' or '.join(types)}"
643648
raise TypeError(msg)
644649

@@ -733,7 +738,7 @@ def assert_any_values_of_type(
733738
if isinstance(check_type, type):
734739
msg += f"Must be: '{check_type.__name__}'"
735740
else:
736-
types: list[str] = [f"'{typ.__name__}'" for typ in check_type]
741+
types: str_list = [f"'{typ.__name__}'" for typ in check_type]
737742
msg += f"Must be: {' or '.join(types)}"
738743
raise TypeError(msg)
739744

src/toolbox_python/classes.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,16 @@
4141
# ## Python StdLib Imports ----
4242
from typing import Any
4343

44+
# ## Local First Party Imports ----
45+
from toolbox_python.collection_types import str_list
46+
4447

4548
# ---------------------------------------------------------------------------- #
4649
# Exports ####
4750
# ---------------------------------------------------------------------------- #
4851

4952

50-
__all__: list[str] = ["get_full_class_name"]
53+
__all__: str_list = ["get_full_class_name"]
5154

5255

5356
# ---------------------------------------------------------------------------- #

src/toolbox_python/collection_types.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@
110110

111111

112112
dict_str_any = dict[str, Any]
113+
dict_str_str = dict[str, str]
114+
dict_int_str = dict[int, str]
113115

114116

115117
log_levels = Literal["debug", "info", "warning", "error", "critical"]

src/toolbox_python/defaults.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,23 @@
4040
from __future__ import annotations
4141

4242
# ## Python StdLib Imports ----
43-
from typing import Any, Optional, Union
43+
from typing import Any
4444

4545
# ## Python Third Party Imports ----
4646
from typeguard import typechecked
4747

4848
# ## Local First Party Imports ----
4949
from toolbox_python.bools import strtobool
5050
from toolbox_python.checkers import is_type
51+
from toolbox_python.collection_types import str_list
5152

5253

5354
# ---------------------------------------------------------------------------- #
5455
# Exports ####
5556
# ---------------------------------------------------------------------------- #
5657

5758

58-
__all__: list[str] = ["defaults", "Defaults"]
59+
__all__: str_list = ["defaults", "Defaults"]
5960

6061

6162
# ---------------------------------------------------------------------------- #
@@ -196,8 +197,8 @@ def __call__(self, *args, **kwargs) -> Any:
196197
def get(
197198
self,
198199
value: Any,
199-
default: Optional[Any] = None,
200-
cast: Optional[Union[str, type]] = None,
200+
default: Any | None = None,
201+
cast: str | type | None = None,
201202
) -> Any:
202203
"""
203204
!!! note "Summary"
@@ -250,8 +251,8 @@ def get(
250251

251252
def _validate_value_and_default(
252253
self,
253-
value: Optional[Any] = None,
254-
default: Optional[Any] = None,
254+
value: Any | None = None,
255+
default: Any | None = None,
255256
) -> Defaults:
256257
"""
257258
!!! note "Summary"
@@ -284,7 +285,7 @@ def _validate_value_and_default(
284285

285286
def _validate_type(
286287
self,
287-
check_type: Optional[Union[str, type]] = None,
288+
check_type: str | type | None = None,
288289
) -> Defaults:
289290
"""
290291
!!! note "Summary"
@@ -306,7 +307,7 @@ def _validate_type(
306307
??? tip "See Also"
307308
- [`Defaults.get()`][toolbox_python.defaults.Defaults.get]
308309
"""
309-
valid_types: list[str] = [
310+
valid_types: str_list = [
310311
"bool",
311312
"dict",
312313
"int",

src/toolbox_python/dictionaries.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@
4242
from typeguard import typechecked
4343

4444
# ## Local First Party Imports ----
45-
from toolbox_python.collection_types import dict_any, dict_str_int
45+
from toolbox_python.collection_types import dict_any, dict_str_any, str_list
4646

4747

4848
# ---------------------------------------------------------------------------- #
4949
# Exports ####
5050
# ---------------------------------------------------------------------------- #
5151

52-
__all__: list[str] = ["dict_reverse_keys_and_values"]
52+
__all__: str_list = ["dict_reverse_keys_and_values"]
5353

5454

5555
# ---------------------------------------------------------------------------- #
@@ -62,7 +62,7 @@
6262
@typechecked
6363
def dict_reverse_keys_and_values(
6464
dictionary: dict_any,
65-
) -> dict_str_int:
65+
) -> dict_str_any:
6666
"""
6767
!!! note "Summary"
6868
Take the `key` and `values` of a dictionary, and reverse them.
@@ -201,7 +201,7 @@ def dict_reverse_keys_and_values(
201201
!!! observation "Here, the process would be to run a recursive process when it recognises that any `value` is a `#!py dict` object. So long as there are no duplicate values in any of the contained `#!py dict`'s, the resulting output will be a big, flat dictionary."
202202
</div>
203203
"""
204-
output_dict: dict_str_int = dict()
204+
output_dict: dict_str_any = dict()
205205
for key, value in dictionary.items():
206206
if isinstance(value, (str, int, float)):
207207
output_dict[str(value)] = key
@@ -216,7 +216,7 @@ def dict_reverse_keys_and_values(
216216
)
217217
output_dict[str(elem)] = key
218218
elif isinstance(value, dict):
219-
interim_dict: dict_str_int = dict_reverse_keys_and_values(value)
219+
interim_dict: dict_str_any = dict_reverse_keys_and_values(value)
220220
output_dict = {
221221
**output_dict,
222222
**interim_dict,

src/toolbox_python/lists.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,20 @@
4747
from typeguard import typechecked
4848

4949
# ## Local First Party Imports ----
50-
from toolbox_python.collection_types import any_list, any_tuple, collection, scalar
50+
from toolbox_python.collection_types import (
51+
any_list,
52+
any_tuple,
53+
collection,
54+
scalar,
55+
str_list,
56+
)
5157

5258

5359
# ---------------------------------------------------------------------------- #
5460
# Exports ####
5561
# ---------------------------------------------------------------------------- #
5662

57-
__all__: list[str] = ["flatten", "flat_list", "product"]
63+
__all__: str_list = ["flatten", "flat_list", "product"]
5864

5965

6066
# ---------------------------------------------------------------------------- #

src/toolbox_python/output.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,13 +364,13 @@ def list_columns(
364364
Full credit goes to:<br>
365365
https://stackoverflow.com/questions/1524126/how-to-print-a-list-more-nicely#answer-36085705
366366
"""
367-
string_list: list[str] = [str(item) for item in obj]
367+
string_list: str_list = [str(item) for item in obj]
368368
if cols_wide > len(string_list):
369369
cols_wide = len(string_list)
370370
max_len: int = max(len(item) for item in string_list)
371371
if columnwise:
372372
cols_wide = int(ceil(len(string_list) / cols_wide))
373-
segmented_list: list[list[str]] = [
373+
segmented_list: list[str_list] = [
374374
string_list[index : index + cols_wide]
375375
for index in range(0, len(string_list), cols_wide)
376376
]
@@ -379,7 +379,7 @@ def list_columns(
379379
segmented_list[-1].extend(
380380
[""] * (len(string_list) - len(segmented_list[-1]))
381381
)
382-
combined_list: Union[list[list[str]], Any] = zip(*segmented_list)
382+
combined_list: Union[list[str_list], Any] = zip(*segmented_list)
383383
else:
384384
combined_list = segmented_list
385385
printer: str = "\n".join(

0 commit comments

Comments
 (0)