Skip to content

feat(typing)!: make cache generic over value (#608) #986

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
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

elecnix
Copy link

@elecnix elecnix commented Apr 26, 2025

What do these changes do?

  • 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.

Are there changes in behavior for the user?

BREAKING CHANGE: BaseCache and all backends now require two type parameters (key, value). Existing code using a single type parameter must be updated.

Before, users could write:

cache: BaseCache[str] = ...

Users must now write:

cache: BaseCache[str, Any] = ...
# or for stricter typing:
cache: BaseCache[str, int] = ...

Related issue number

Fixes: #608

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes

- 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

import aiomcache

from aiocache.base import BaseCache
from aiocache.serializers import JsonSerializer


class MemcachedBackend(BaseCache[bytes]):
class MemcachedBackend(BaseCache[bytes, Any]):
Copy link
Member

Choose a reason for hiding this comment

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

I think all these need to be defined as a TypeVar. Then the user would need to annotate their code as: MemcachedCache[Any] and similar.

Though, probably better to get #684 done before this one.

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 this pull request may close these issues.

Make caches Generic
2 participants