Skip to content

Commit

Permalink
Improve typing
Browse files Browse the repository at this point in the history
  • Loading branch information
kiendang committed Oct 27, 2023
1 parent 573a6f9 commit 6fdab63
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions packages/syft/src/syft/types/dicttuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,28 @@
# once to extract both keys and values, then passing keys to __new__, values to __init__
# within the same function call.
class _Meta(type):
@overload
def __call__(cls: type[_T]) -> _T:
...

@overload
def __call__(cls: type[_T], __value: Iterable[tuple[_KT, _VT]]) -> _T:
...

@overload
def __call__(cls: type[_T], __value: Mapping[_KT, _VT]) -> _T:
...

@overload
def __call__(cls: type[_T], __value: Iterable[_VT], __key: Collection[_KT]) -> _T:
...

@overload
def __call__(
cls: type[_T], __value: Iterable[_VT], __key: Callable[[_VT], _KT]
) -> _T:
...

def __call__(
cls: type[_T],
__value: Optional[Iterable] = None,
Expand Down Expand Up @@ -130,6 +152,7 @@ class DictTuple(tuple[_VT, ...], Generic[_KT, _VT], metaclass=_Meta):

__mapping: MappingProxyType[_KT, int]

# These overloads are copied from _Meta.__call__ just for IDE hints
@overload
def __init__(self) -> None:
...
Expand All @@ -150,9 +173,7 @@ def __init__(self, __value: Iterable[_VT], __key: Collection[_KT]) -> None:
def __init__(self, __value: Iterable[_VT], __key: Callable[[_VT], _KT]) -> None:
...

def __init__(
self, __value: Optional[Union[Mapping[_KT, int], Iterable[_KT]]] = None, /
) -> None:
def __init__(self, __value=None, /):
if isinstance(__value, MappingProxyType):
self.__mapping = __value
elif isinstance(__value, Mapping):
Expand Down Expand Up @@ -189,7 +210,7 @@ def __getitem__(self, __key: slice) -> Self:
def __getitem__(self, __key: SupportsIndex) -> _VT:
...

def __getitem__(self, __key):
def __getitem__(self, __key, /):
if isinstance(__key, slice):
return self.__class__(
super().__getitem__(__key),
Expand Down

0 comments on commit 6fdab63

Please sign in to comment.