Typing LRU Cache - Part 2 #1256
-
Sorry to keep bringing this up, but based on this answer from the original question, I'm wondering if there's not a simpler way to get Below is just the from __future__ import annotations
from collections import OrderedDict
from typing import Iterable, TypeVar, overload
from _typeshed import SupportsKeysAndGetItem
_KT = TypeVar('_KT')
_VT = TypeVar('_VT')
class LRU(OrderedDict):
@overload
def __init__(self) -> None:
...
@overload
def __init__(self: dict[str, _VT], **kwargs: _VT) -> None:
...
@overload
def __init__(self, __map: SupportsKeysAndGetItem[_KT, _VT]) -> None:
...
@overload
def __init__(
self: dict[str, _VT], __map: SupportsKeysAndGetItem[str, _VT], **kwargs: _VT
) -> None:
...
@overload
def __init__(self, __iterable: Iterable[tuple[_KT, _VT]]) -> None:
...
@overload
def __init__(
self: dict[str, _VT], __iterable: Iterable[tuple[str, _VT]], **kwargs: _VT
) -> None:
...
@overload
def __init__(self: dict[str, str], __iterable: Iterable[list[str]]) -> None:
...
def __init__(
self: dict[str, _VT] | dict[str, str],
__map_or_iterable: SupportsKeysAndGetItem[_KT, _VT]
| SupportsKeysAndGetItem[str, _VT]
| Iterable[tuple[_KT, _VT]]
| Iterable[tuple[str, _VT]]
| Iterable[list[str]]
| None = None,
**kwargs: _VT | None,
) -> None:
# Put implementation here...
pass Is implementing new container types in Python with type hinting always this verbose? Is there not a simpler way? I've heard some people say they never need to use How are others doing these things in practice? I understand if there is no better way, and I have nothing but respect for the typing team and what has been accomplished with static typing, but this is REALLY tedious... |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Oh my lord, I suppose there are times when my hand isn't big enough for the face palm I want to give myself: class LRU(OrderedDict):
def __init__(
self,
*args: object,
max_size: int = 128,
**kwargs: object,
) -> None:
self.max_size = max_size
super().__init__(*args, **kwargs) For all intents and purposes, the above should work just fine. Here it is on the mypy playground for future readers who want to see this in action. Sorry everyone! |
Beta Was this translation helpful? Give feedback.
Oh my lord, I suppose there are times when my hand isn't big enough for the face palm I want to give myself:
For all intents and purposes, the above should work just fine. Here it is on the mypy playground for future readers who want to see this in action.
Sorry everyone!