From 02731ef41ec7f90c27bb0929448d547bdd872ca2 Mon Sep 17 00:00:00 2001 From: Willem Van Onsem Date: Sat, 13 Sep 2025 22:30:04 +0200 Subject: [PATCH 1/4] type hints --- inheritance_dict/__init__.py | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/inheritance_dict/__init__.py b/inheritance_dict/__init__.py index c73b899..1a0aa4d 100644 --- a/inheritance_dict/__init__.py +++ b/inheritance_dict/__init__.py @@ -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 Callable, Generator, TypeVar __all__ = [ "concat_map", @@ -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]) -> Generator[U]: """ Yield items from the iterables produced by applying func to each element of items. @@ -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. @@ -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. @@ -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`. @@ -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. @@ -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. @@ -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. @@ -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. @@ -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 @@ -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. @@ -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. From c1382c29c1270ed668c371056ed115f1c5257774 Mon Sep 17 00:00:00 2001 From: Willem Van Onsem Date: Sat, 13 Sep 2025 22:30:34 +0200 Subject: [PATCH 2/4] black --- inheritance_dict/__init__.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/inheritance_dict/__init__.py b/inheritance_dict/__init__.py index 1a0aa4d..0141795 100644 --- a/inheritance_dict/__init__.py +++ b/inheritance_dict/__init__.py @@ -17,8 +17,8 @@ ] MISSING = object() -T = TypeVar('T') -U = TypeVar('U') +T = TypeVar("T") +U = TypeVar("U") def concat_map(func: Callable[[T], Iterable[U]], items: Iterable[T]) -> Generator[U]: @@ -80,7 +80,7 @@ def __getitem__(self, key: object) -> object: return result raise KeyError(key) - def get(self, key: object, default: object=None) -> object: + def get(self, key: object, default: object = None) -> object: """ Return the value mapped to `key` or `default` if no mapping exists. @@ -93,7 +93,7 @@ def get(self, key: object, default: object=None) -> object: except KeyError: return default - def setdefault(self, key: object, default: object=None) -> object: + def setdefault(self, key: object, default: object = None) -> object: """ Return the value for `key` if present; otherwise insert `default` for `key` and return it. From 4a70e981283aaf10a9cb16e9df0de7d9f7e83085 Mon Sep 17 00:00:00 2001 From: Willem Van Onsem Date: Sat, 13 Sep 2025 22:34:01 +0200 Subject: [PATCH 3/4] pylint --- inheritance_dict/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inheritance_dict/__init__.py b/inheritance_dict/__init__.py index 0141795..09f396e 100644 --- a/inheritance_dict/__init__.py +++ b/inheritance_dict/__init__.py @@ -4,7 +4,7 @@ """ from collections.abc import Callable, Iterable -from typing import Callable, Generator, TypeVar +from typing import Generator, TypeVar __all__ = [ "concat_map", From 7f6c7e679b997947607694f5fbbd5df284a785c4 Mon Sep 17 00:00:00 2001 From: Willem Van Onsem Date: Sat, 13 Sep 2025 22:35:13 +0200 Subject: [PATCH 4/4] iterable --- inheritance_dict/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inheritance_dict/__init__.py b/inheritance_dict/__init__.py index 09f396e..5f4e3ec 100644 --- a/inheritance_dict/__init__.py +++ b/inheritance_dict/__init__.py @@ -4,7 +4,7 @@ """ from collections.abc import Callable, Iterable -from typing import Generator, TypeVar +from typing import TypeVar __all__ = [ "concat_map", @@ -21,7 +21,7 @@ U = TypeVar("U") -def concat_map(func: Callable[[T], Iterable[U]], items: Iterable[T]) -> Generator[U]: +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.