Skip to content
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

error: BaseCache has no attribute lock / keys / ... #1082

Closed
UnknownPlatypus opened this issue Aug 1, 2022 · 3 comments
Closed

error: BaseCache has no attribute lock / keys / ... #1082

UnknownPlatypus opened this issue Aug 1, 2022 · 3 comments
Labels
bug Something isn't working

Comments

@UnknownPlatypus
Copy link
Contributor

Bug report

What's wrong

Thanks to #998 , we have a more accurate type for django.core.cache.cache -> BaseCache.

However, this is a problem for third-party/custom cache backend that implement other methods.
For example django-redis lock/keys/... methods.

How is that should be

I'm not sure how to deal with this, and how to declare that my cache backend is using a different type (RedisCache for ex.)
Any idea how I can address this issue ? This is a blocker for me as my project heavily rely on django-redis.

System information

  • OS:
  • python version: 3.8
  • django version: 3.2.14
  • mypy version: 0.971
  • django-stubs version: 1.12.0
  • django-stubs-ext version: 0.5.0
@UnknownPlatypus UnknownPlatypus added the bug Something isn't working label Aug 1, 2022
@adamchainz
Copy link
Contributor

Adding a type-narrowing assert at the module level should do the trick:

from django.core.cache import caches
from djagno_redis.cache import RedisCache
cache = caches["default"]  # or whatever the name of your cache is
assert isinstance(cache, RedisCache)
# from here on, Mypy knows it's a RedisCache

Hope that helps!

@UnknownPlatypus
Copy link
Contributor Author

This should work, but that's a bit annoying to have to do that multiple time.
(Won't work for django-redis tho because they lack type hints)

Anyway, I don't have better idea for now.
Thanks for the idea

@UnknownPlatypus UnknownPlatypus changed the title error: "BaseCache" has no attribute "lock" error: BaseCache has no attribute lock / keys / ... Sep 11, 2022
@adamchainz
Copy link
Contributor

You can always create myapp.cache and import the cache object, that uses the above type narrowing, and import from there throughout your code base.

django-redis is kinda outdated since the Redis backend was introduced to Django core. There's perhaps space to add some extra functions to Django core's redis backend, if you propose it. Otherwise you could consider a package that extends the built-in backend.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

No branches or pull requests

2 participants