Skip to content
Merged
Show file tree
Hide file tree
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
25 changes: 20 additions & 5 deletions stubs/cachetools/@tests/stubtest_allowlist.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
cachetools.Cache.get
cachetools.cache.Cache.get

# it looks like cachetools stubs are really out of date, so none of the modules
# referenced exist anymore
cachetools\..*\.__(get|set|del)item__ # has extra argument
cachetools\..*TTLCache.__repr__
# stubs omit defaulted arguments that are meant to be optimizations, not provided by user

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it hurt that much to include the weird extra arguments in these signatures? I worry that if we allowlist all these functions, stubtest won't warn us if there are any other problems with any of them

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of these allowlistings correspond with a def statement in the stubs. They are all inherited from other classes that don't have the extra arguments. So if we define something incorrectly in the stubs, we'll get a new stubtest error that points at the definition.

@Akuli Akuli Mar 8, 2022 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And after thinking about this a bit more: Including the weird extra arguments also would mean including a new method definition for each of the lines I allowlisted. That would clutter the stubs and not help users in any way. CI checks should catch problems, not force you to do something weird and unnecessary IMO.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, sounds good! Will try to do a full review of the rest of the PR soon.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side point: I'm amazed a library that's concerned with such micro-optimisations is also using functools.total_ordering in the same module.

cachetools.FIFOCache.__delitem__
cachetools.FIFOCache.__setitem__
cachetools.LFUCache.__delitem__
cachetools.LFUCache.__getitem__
cachetools.LFUCache.__setitem__
cachetools.LRUCache.__delitem__
cachetools.LRUCache.__getitem__
cachetools.LRUCache.__setitem__
cachetools.MRUCache.__delitem__
cachetools.MRUCache.__getitem__
cachetools.MRUCache.__setitem__
cachetools.TLRUCache.__delitem__
cachetools.TLRUCache.__getitem__
cachetools.TLRUCache.__setitem__
cachetools.TTLCache.__delitem__
cachetools.TTLCache.__getitem__
cachetools.TTLCache.__setitem__
cachetools._TimedCache.__len__
cachetools._TimedCache.__repr__
2 changes: 1 addition & 1 deletion stubs/cachetools/METADATA.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = "4.2.*"
version = "5.0.*"
34 changes: 31 additions & 3 deletions stubs/cachetools/cachetools/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ from collections.abc import Iterator, Sequence
from contextlib import AbstractContextManager
from typing import Any, Callable, Generic, MutableMapping, TypeVar, overload

__all__ = ("Cache", "FIFOCache", "LFUCache", "LRUCache", "MRUCache", "RRCache", "TLRUCache", "TTLCache", "cached", "cachedmethod")
__version__: str

_KT = TypeVar("_KT")
_VT = TypeVar("_VT")
_T = TypeVar("_T")
Expand All @@ -12,6 +15,7 @@ class Cache(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
def __getitem__(self, key: _KT) -> _VT: ...
def __setitem__(self, key: _KT, value: _VT) -> None: ...
def __delitem__(self, key: _KT) -> None: ...
def __missing__(self, key: _KT) -> _VT: ...
def __iter__(self) -> Iterator[_KT]: ...
def __len__(self) -> int: ...
@overload # type: ignore[override]
Expand Down Expand Up @@ -45,18 +49,42 @@ class RRCache(Cache[_KT, _VT]):
@property
def choice(self) -> Callable[[Sequence[_KT]], _KT]: ...

class TTLCache(Cache[_KT, _VT]):
class _TimedCache(Cache[_KT, _VT]):
def __init__(
self, maxsize: float, ttl: float, timer: Callable[[], float] = ..., getsizeof: Callable[[_VT], float] | None = ...
self, maxsize: float, timer: Callable[[], float] = ..., getsizeof: Callable[[_VT], float] | None = ...
) -> None: ...
@property
def currsize(self) -> float: ...

class _Timer:
def __init__(self, timer: Callable[[], float]) -> None: ...
def __call__(self) -> float: ...
def __enter__(self) -> float: ...
def __exit__(self, *exc: object) -> None: ...

@property
Comment thread
Akuli marked this conversation as resolved.
def timer(self) -> Callable[[], float]: ...
def timer(self) -> _Timer: ...

class TTLCache(_TimedCache[_KT, _VT]):
def __init__(
self, maxsize: float, ttl: float, timer: Callable[[], float] = ..., getsizeof: Callable[[_VT], float] | None = ...
) -> None: ...
@property
def ttl(self) -> float: ...
def expire(self, time: float | None = ...) -> None: ...

class TLRUCache(_TimedCache[_KT, _VT]):
def __init__(
self,
maxsize: float,
ttu: Callable[[_KT, _VT, float], float],
timer: Callable[[], float] = ...,
getsizeof: Callable[[_VT], float] | None = ...,
) -> None: ...
@property
def ttu(self) -> Callable[[_KT, _VT, float], float]: ...
def expire(self, time: float | None = ...) -> None: ...

def cached(
cache: MutableMapping[_KT, Any] | None, key: Callable[..., _KT] = ..., lock: AbstractContextManager[Any] | None = ...
) -> IdentityFunction: ...
Expand Down
2 changes: 0 additions & 2 deletions stubs/cachetools/cachetools/cache.pyi

This file was deleted.

2 changes: 0 additions & 2 deletions stubs/cachetools/cachetools/fifo.pyi

This file was deleted.

1 change: 1 addition & 0 deletions stubs/cachetools/cachetools/func.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from _typeshed import IdentityFunction
from typing import Callable, Sequence, TypeVar

__all__ = ("fifo_cache", "lfu_cache", "lru_cache", "mru_cache", "rr_cache", "ttl_cache")
_T = TypeVar("_T")

def fifo_cache(maxsize: float | None = ..., typed: bool = ...) -> IdentityFunction: ...
Expand Down
2 changes: 2 additions & 0 deletions stubs/cachetools/cachetools/keys.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from typing import Hashable

__all__ = ("hashkey", "typedkey")

def hashkey(*args: Hashable, **kwargs: Hashable) -> tuple[Hashable, ...]: ...
def typedkey(*args: Hashable, **kwargs: Hashable) -> tuple[Hashable, ...]: ...
2 changes: 0 additions & 2 deletions stubs/cachetools/cachetools/lfu.pyi

This file was deleted.

2 changes: 0 additions & 2 deletions stubs/cachetools/cachetools/lru.pyi

This file was deleted.

2 changes: 0 additions & 2 deletions stubs/cachetools/cachetools/mru.pyi

This file was deleted.

2 changes: 0 additions & 2 deletions stubs/cachetools/cachetools/rr.pyi

This file was deleted.

2 changes: 0 additions & 2 deletions stubs/cachetools/cachetools/ttl.pyi

This file was deleted.