Skip to content

Commit a9c2009

Browse files
committed
refactor(data-structures): use TypeVar to improve type hinting
1 parent 21a0d9e commit a9c2009

File tree

3 files changed

+10
-11
lines changed

3 files changed

+10
-11
lines changed

python_utils/data_structures.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
)
77

88
from python_utils.math import are_equal
9-
from python_utils.types_hinting import DictOrObject
9+
from python_utils.types_hinting import T
1010

1111

1212
def safe_find(records: Iterable, filter_func: Callable) -> Optional[Any]:
@@ -85,7 +85,7 @@ def filter_dicts(records: List[Dict], as_list=True, **filters) -> Union[List[Dic
8585
return filtered_records
8686

8787

88-
def filter_objects(objects: List[object], as_list=True, **filters) -> Union[List[object], Iterable[Dict]]:
88+
def filter_objects(objects: List[T], as_list=True, **filters) -> Union[List[T], Iterable[Dict]]:
8989
"""
9090
Pass a list of objects and filters as kwargs to get all filtered records as list or filter obj
9191
@@ -122,7 +122,7 @@ def filter_objects(objects: List[object], as_list=True, **filters) -> Union[List
122122
return filtered_objects
123123

124124

125-
def find_object(objects: List[object], **filters) -> Optional[object]:
125+
def find_object(objects: List[T], **filters) -> Optional[T]:
126126
"""
127127
Pass a list of objects and filters as kwargs to get first occurence record. If no filters
128128
passed return first object in the list
@@ -217,7 +217,7 @@ def get_values(dictionary: Dict, keys: List[Hashable], dtypes: Dict = None) -> D
217217

218218

219219
def get_differences(
220-
old_data: DictOrObject,
220+
old_data: Union[Dict, T],
221221
new_data: Dict,
222222
skip_keys: List[Hashable] = None,
223223
number_precision: int = 6
@@ -438,7 +438,7 @@ class Finder:
438438
}
439439
available_ops = ops.keys()
440440

441-
def __init__(self, records: List[DictOrObject]):
441+
def __init__(self, records: List[Union[Dict, T]]):
442442
self._records = records
443443

444444
@classmethod
@@ -449,7 +449,7 @@ def _compare(cls, value1: Any, op: str, value2: Any, ignore_types=False) -> bool
449449
return cls.ops[op](value1, value2)
450450

451451
@classmethod
452-
def _verify(cls, record: DictOrObject, ignore_types=False, **checks) -> bool:
452+
def _verify(cls, record: Union[Dict, T], ignore_types=False, **checks) -> bool:
453453
"""
454454
Verify that the record fulfills the given checks.
455455
@@ -480,7 +480,7 @@ def _verify(cls, record: DictOrObject, ignore_types=False, **checks) -> bool:
480480
return True
481481

482482
@staticmethod
483-
def _get_value(record: DictOrObject, attr: str) -> Optional[Any]:
483+
def _get_value(record: Union[Dict, T], attr: str) -> Optional[Any]:
484484
"""
485485
Get the value of the attribute for the given record. Used to process dicts and objects uniformly.
486486

python_utils/types_hinting.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from decimal import Decimal
2-
from typing import Dict, Union
2+
from typing import Union, TypeVar
33

4-
5-
DictOrObject = Union[Dict, object]
4+
T = TypeVar('T')
65

76
NumberIFD = Union[int, float, Decimal]

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = cardo-python-utils
3-
version = 0.1.3
3+
version = 0.1.4
44
description = Python library enhanced with a wide range of functions for different scenarios.
55
long_description = file: README.rst
66
url = https://github.com/CardoAI/cardo-python-utils

0 commit comments

Comments
 (0)