Skip to content

Make caches Generic #608

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Dreamsorcerer opened this issue Dec 31, 2022 · 0 comments · May be fixed by #986
Open

Make caches Generic #608

Dreamsorcerer opened this issue Dec 31, 2022 · 0 comments · May be fixed by #986
Milestone

Comments

@Dreamsorcerer
Copy link
Member

We should consider making BaseCache Generic, so we can provide better type safety when relevant.

Code could then look something like this, with mypy checking:

cache: Cache[str] = Cache(...)
await cache.get("foo")  # -> str
await cache.set("foo", "bar")  # OK
await cache.set("foo", 45)  # Error: Expected str

Existing typing behaviour can be reproduced by annotating it with Cache[Any].

@Dreamsorcerer Dreamsorcerer added this to the 1.0 milestone Jan 2, 2023
elecnix added a commit to elecnix/aiocache that referenced this issue Apr 26, 2025
- Make BaseCache generic over both key and value types for improved type safety.
- Update all cache backends and usages to use BaseCache[KeyType, ValueType].
- Improve type annotations for cache methods.
- Update RedLock, OptimisticLock, and test utilities for new generics.

BREAKING CHANGE: BaseCache and all backends now require two type parameters (key, value). Existing code using a single type parameter must be updated.
Refs: aio-libs#608
@elecnix elecnix linked a pull request Apr 26, 2025 that will close this issue
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant