Skip to content

Commit 5eac0d3

Browse files
committed
Added type hinting to new container classes
1 parent 0592a5f commit 5eac0d3

5 files changed

Lines changed: 27 additions & 24 deletions

File tree

python_utils/containers.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# pyright: reportIncompatibleMethodOverride=false
22
import abc
33
import typing
4+
import collections
45

56
from . import types
67

@@ -19,6 +20,8 @@
1920
VT_cast = types.Optional[types.Callable[..., VT]]
2021
#: A type alias for the hashable values of the `UniqueList`
2122
HT = types.TypeVar('HT', bound=types.Hashable)
23+
#: A type alias for a regular generic type
24+
T = types.TypeVar('T')
2225

2326
# Using types.Union instead of | since Python 3.7 doesn't fully support it
2427
DictUpdateArgs = types.Union[
@@ -28,6 +31,8 @@
2831
'_typeshed.SupportsKeysAndGetItem[KT, VT]',
2932
]
3033

34+
OnDuplicate = types.Literal['ignore', 'raise']
35+
3136

3237
class CastedDictBase(types.Dict[KT, VT], abc.ABC):
3338
_key_cast: KT_cast[KT]
@@ -222,7 +227,7 @@ class UniqueList(types.List[HT]):
222227
def __init__(
223228
self,
224229
*args: HT,
225-
on_duplicate: types.Literal['raise', 'ignore'] = 'ignore',
230+
on_duplicate: OnDuplicate = 'ignore',
226231
):
227232
self.on_duplicate = on_duplicate
228233
self._set = set()
@@ -305,9 +310,19 @@ def __delitem__(
305310
super().__delitem__(index)
306311

307312

308-
class SlicableDeque(Generic[T], deque):
309-
def __getitem__(self, index: Union[int, slice]) -> Union[T, 'SlicableDeque[T]']:
310-
"""
313+
class SlicableDeque(types.Generic[T], collections.deque): # type: ignore
314+
@types.overload
315+
def __getitem__(self, index: types.SupportsIndex) -> T:
316+
...
317+
318+
@types.overload
319+
def __getitem__(self, index: slice) -> 'SlicableDeque[T]':
320+
...
321+
322+
def __getitem__(
323+
self, index: types.Union[types.SupportsIndex, slice]
324+
) -> types.Union[T, 'SlicableDeque[T]']:
325+
'''
311326
Return the item or slice at the given index.
312327
313328
>>> d = SlicableDeque[int]([1, 2, 3, 4, 5])
@@ -318,23 +333,12 @@ def __getitem__(self, index: Union[int, slice]) -> Union[T, 'SlicableDeque[T]']:
318333
>>> d[-2:]
319334
SlicableDeque(['b', 'c'])
320335
321-
"""
336+
'''
322337
if isinstance(index, slice):
323338
start, stop, step = index.indices(len(self))
324339
return self.__class__(self[i] for i in range(start, stop, step))
325340
else:
326-
return super().__getitem__(index)
327-
328-
def pop(self) -> T:
329-
"""
330-
Remove and return the rightmost element.
331-
332-
>>> d = SlicableDeque[float]([1.5, 2.5, 3.5])
333-
>>> d.pop()
334-
3.5
335-
336-
"""
337-
return super().pop()
341+
return types.cast(T, super().__getitem__(index))
338342

339343

340344
if __name__ == '__main__':

python_utils/converters.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,8 @@ def remap(
366366
new_max = types.cast(_TN, type_(new_max))
367367
new_min = types.cast(_TN, type_(new_min))
368368

369-
# These might not be floats but the Python type system doesn't understand the
370-
# generic type system in this case
369+
# These might not be floats but the Python type system doesn't understand
370+
# the generic type system in this case
371371
old_range = types.cast(float, old_max) - types.cast(float, old_min)
372372
new_range = types.cast(float, new_max) - types.cast(float, new_min)
373373

python_utils/decorators.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ def __listify(*args: types.Any, **kwargs: types.Any) -> TC:
107107
return collection(iter(()))
108108
else:
109109
raise TypeError(
110-
f'{function} returned `None` and `allow_empty` is `False`'
110+
f'{function} returned `None` and `allow_empty` '
111+
'is `False`'
111112
)
112113
else:
113114
return collection(result)

python_utils/types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# pyright: reportWildcardImportFromLibrary=false
22
import datetime
33
import decimal
4-
from typing_extensions import * # type: ignore # pragma: no cover # noqa: F403
4+
from typing_extensions import * # type: ignore # noqa: F403
55
from typing import * # type: ignore # pragma: no cover # noqa: F403
66
from types import * # type: ignore # pragma: no cover # noqa: F403
77

tox.ini

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
[tox]
2-
envlist = black, py37, py38, py39, py310, py311, pypy3, flake8, docs, mypy, pyright
2+
envlist = black, py38, py39, py310, py311, flake8, docs, mypy, pyright
33
skip_missing_interpreters = True
44

55
[testenv]
66
basepython =
7-
py37: python3.7
87
py38: python3.8
98
py39: python3.9
109
py310: python3.10
1110
py311: python3.11
12-
pypy: pypy
1311

1412
setenv = PY_IGNORE_IMPORTMISMATCH=1
1513
deps =

0 commit comments

Comments
 (0)