Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions inheritance_dict/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
type, it will walk over the Method Resolution Order (MRO) looking for a value.
"""

from collections.abc import Iterable
from collections.abc import Callable, Iterable
from typing import TypeVar

__all__ = [
"concat_map",
Expand All @@ -16,8 +17,11 @@
]
MISSING = object()

T = TypeVar("T")
U = TypeVar("U")

def concat_map(func, items):

def concat_map(func: Callable[[T], Iterable[U]], items: Iterable[T]) -> Iterable[U]:
"""
Yield items from the iterables produced by applying func to each element of items.

Expand All @@ -36,7 +40,7 @@ class BaseDict(dict):
type.
"""

def _get_keys(self, key) -> Iterable[object]:
def _get_keys(self, key: object) -> Iterable[object]:
"""
Return an iterable of candidate lookup keys for dictionary lookup.

Expand All @@ -50,7 +54,7 @@ def _get_keys(self, key) -> Iterable[object]:
"""
return (key,)

def _set_key(self, key) -> object:
def _set_key(self, key: object) -> object:
"""
Return the key that should be used to store a value.

Expand All @@ -63,7 +67,7 @@ def _set_key(self, key) -> object:
"""
return key

def __getitem__(self, key):
def __getitem__(self, key: object) -> object:
"""
Return the value mapped to `key` by trying candidate lookup keys produced by `_get_keys`.

Expand All @@ -76,7 +80,7 @@ def __getitem__(self, key):
return result
raise KeyError(key)

def get(self, key, default=None):
def get(self, key: object, default: object = None) -> object:
"""
Return the value mapped to `key` or `default` if no mapping exists.

Expand All @@ -89,7 +93,7 @@ def get(self, key, default=None):
except KeyError:
return default

def setdefault(self, key, default=None):
def setdefault(self, key: object, default: object = None) -> object:
"""
Return the value for `key` if present; otherwise insert `default` for `key` and return it.

Expand All @@ -111,7 +115,7 @@ def setdefault(self, key, default=None):
self[self._set_key(key)] = default
return default

def __repr__(self):
def __repr__(self) -> str:
"""
Return a canonical string representation of the mapping.

Expand All @@ -136,7 +140,7 @@ class FallbackMixin: # pylint: disable=too-few-public-methods
none result in a hit.
"""

def _get_keys(self, key) -> Iterable[object]:
def _get_keys(self, key: object) -> Iterable[object]:
"""
Return an iterable of candidate lookup keys, expanding tuple keys by concatenating
the candidate sequences for each element.
Expand All @@ -155,7 +159,7 @@ def _get_keys(self, key) -> Iterable[object]:
return concat_map(super()._get_keys, key)
return super()._get_keys(key)

def _set_key(self, key) -> object:
def _set_key(self, key: object) -> object:
"""
If the key is a tuple, return the first item of the tuple, such that

Expand All @@ -174,7 +178,7 @@ class InheritanceDict(BaseDict):
resolution order of the type, until a superclass is a hit.
"""

def _get_keys(self, key) -> Iterable[object]:
def _get_keys(self, key: object) -> Iterable[object]:
"""
Yield lookup candidate keys.

Expand All @@ -200,7 +204,7 @@ class TypeConvertingInheritanceDict(InheritanceDict):
retries the lookup using the key's type and resolves via that type's MRO.
"""

def _get_keys(self, key):
def _get_keys(self, key: object) -> Iterable[object]:
"""
Yield candidate lookup keys for a lookup key.

Expand Down