From 2b7b7748448c76f12ab1224635838d6c7033dca3 Mon Sep 17 00:00:00 2001 From: August Vi Date: Mon, 2 Nov 2026 18:56:04 -0800 Subject: [PATCH] Let the incr and decr stubs accept default=None and type-check calls that rely on it. --- CHANGELOG.md | 6 ++++++ diskcache_community/_types.pyi | 12 ++++++++++-- tests/typecheck_stubs.py | 22 ++++++++++++++++++++++ tox.ini | 2 +- 4 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 tests/typecheck_stubs.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 2af4b80..f5dcaf2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ diskcache-community forked from DiskCache at upstream commit ebfa37c (3 March 2024), two commits after the 5.6.3 release. Each entry names the pull request in this repository. Imported upstream pull requests are referenced by number, and their commits keep their original authors in the git history. +## Unreleased + +### Fixes + +- `incr` and `decr` on `Cache` and `FanoutCache` type-check with `default=None`, which makes a missing key raise `KeyError`: the stubs typed `default` as `int`, so type checkers rejected the call. `tox -e mypy` now also checks `tests/typecheck_stubs.py`, a file of calls that must type-check against the stubs (PR #41). + ## 5.8.2 (2026-11-02) Adds `SafeJSONDisk` and `SafeDisk.with_numpy()`, keeps float NaN values, and makes `get` and `set` faster. The `cache.db` schema and the file layout are unchanged. diff --git a/diskcache_community/_types.pyi b/diskcache_community/_types.pyi index e5a38d9..ff1b5c9 100644 --- a/diskcache_community/_types.pyi +++ b/diskcache_community/_types.pyi @@ -173,10 +173,18 @@ class BaseCache(Protocol): retry: bool = ..., ) -> bool: ... def incr( - self, key: Any, delta: int = ..., default: int = ..., retry: bool = ... + self, + key: Any, + delta: int = ..., + default: int | None = ..., + retry: bool = ..., ) -> int: ... def decr( - self, key: Any, delta: int = ..., default: int = ..., retry: bool = ... + self, + key: Any, + delta: int = ..., + default: int | None = ..., + retry: bool = ..., ) -> int: ... def get( # noqa: PLR0913 self, diff --git a/tests/typecheck_stubs.py b/tests/typecheck_stubs.py new file mode 100644 index 0000000..106b8c4 --- /dev/null +++ b/tests/typecheck_stubs.py @@ -0,0 +1,22 @@ +"""Calls that must type-check against the shipped stubs. + +``tox -e mypy`` checks this file; pytest does not collect it. +""" + +import diskcache_community as dc + + +def incr_without_default(cache: dc.Cache, key: str) -> int: + return cache.incr(key, 1, default=None) + + +def decr_without_default(cache: dc.Cache, key: str) -> int: + return cache.decr(key, 1, default=None) + + +def fanout_incr_without_default(cache: dc.FanoutCache, key: str) -> int: + return cache.incr(key, 1, default=None) + + +def fanout_decr_without_default(cache: dc.FanoutCache, key: str) -> int: + return cache.decr(key, 1, default=None) diff --git a/tox.ini b/tox.ini index 982e604..f56b9a7 100644 --- a/tox.ini +++ b/tox.ini @@ -45,7 +45,7 @@ commands=isort --check {toxinidir}/diskcache_community {toxinidir}/tests deps=isort [testenv:mypy] -commands=mypy {toxinidir}/diskcache_community +commands=mypy {toxinidir}/diskcache_community {toxinidir}/tests/typecheck_stubs.py deps=mypy [testenv:pylint]