Is redis-py the best Redis library for Python despite its typing issues?

0
15
Asked By CodeWizard97 On

Hey everyone! I'm currently building a small app using Python that interacts with Redis, and I'm using redis-py, which I believe is the standard library for this purpose. However, I've found the typing in this library to be quite messy, with many return types designated as `Any`, `Unknown`, or `Awaitable[T] | T`, which is really frustrating when trying to maintain a type-safe codebase. Given Python's strong ecosystem, I'm surprised that redis-py seems to be what we have to work with. I'm curious if anyone else has experienced this and whether redis-py is really the most commonly used Redis library out there. Are there any alternatives that are better typed or more modern that people are actually using in production?

5 Answers

Answered By GizmoGuru On

If you're looking for better type support, you might want to check out this stub package: types-redis. It’s supposed to provide additional typing for the library. Just a note though: ensure you're on version 5.0.0 or higher since the official redis package includes type annotations now, so you wouldn’t need the stub if you have that.

RationalThinker -

Thanks for the suggestion! Just so you know, using types-redis with the newer redis version can create conflicts since they might overlap.

Answered By PythonScribe On

Honestly, it's not as bad as it seems. The actual data types in Redis mainly revolve around strings, so the redis-py commands essentially map directly to the Redis command set. You can still write type-safe code by explicitly defining your variable types when fetching data, like `mything: list[str] = redis.lrange("key", 0, -1)`. It’s not ideal, but it works. And yes, I use it seriously for production tasks.

TypeCritic -

Totally! But I still think the type hints could definitely improve. I often find myself having to use `type: ignore` for async methods where the return type is listed as `Awaitable | Any`, which defeats the purpose of typing.

Answered By TechTracker On

The main point of Redis is that it’s just a K/V store, and the library does its job well for that purpose. Sure, the typings are a bit of a mess, but they aren’t necessary for its key functionalities. A lot of us are still able to work well with it despite that.

ContribKing -

Right? I feel like there’s a lot of focus on typing nowadays, but at the end of the day, Redis maintains its purpose as a quick cache or simple data store.

Answered By CodeExplorer On

I found a GitHub issue discussing the exact problems with typings and it seems that there has been a lot of frustration around this. While redis-py works fine for many, it definitely has its quirks and developers are still figuring out the best way to handle typing effectively. Unfortunately, I haven't seen any viable alternatives that match redis-py's reliability in production just yet.

InsightfulDev -

I appreciate the links and context! The `Awaitable[Any] | Any` typing is genuinely frustrating when trying to work with a type-safe approach in Python. It's disappointing to encounter such issues in a library that's so crucial like this.

Answered By DevNinja88 On

Yeah, I agree that the typing in redis-py is quite lacking. In one of my projects using it, I ended up creating a typing.Protocol with proper annotations for the specific functions I needed and casting the connection objects to that type. Honestly, if I had a fresh project, I'd probably go for Valkey instead of Redis products and use the Valkey-Glide client, though it has its limitations with version compatibility.

TypeMaster -

I’ve done something similar by wrapping the Redis object from the library in my own object for better typing. It makes it easy to switch to other storage options or have an in-memory version for testing.

ThanksForTheTip -

Sounds like a solid approach! It’s unfortunate that we often have to create workarounds for libraries that don't meet the type safety we need.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.