redis / redis/redis-om-python

Improve type annotations for model classmethods

Open
#742 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement maintenance
Dominant language
Python
Stars
1.3k
Forks
128
PR merge metrics
No merged PRs in 30d

Description

Summary

The get() classmethod on RedisModel, HashModel, and JsonModel returns a generic type that doesn't properly reflect the actual model subclass. This breaks IDE type inference and linter checks.

Current behavior

class User(HashModel):
    name: str

user = await User.get("123")
# IDE thinks `user` is `Model`, not `User`
# user.name doesn't autocomplete

Expected behavior

user = await User.get("123")
# IDE knows `user` is `User`
# user.name autocompletes correctly

Technical details

Current signature:

async def get(cls: Type["Model"], pk: Any) -> "Model"

Should use proper TypeVar binding so the return type matches the calling class:

async def get(cls: Type[_T], pk: Any) -> _T

This pattern should be applied consistently to:

  • RedisModel.get()
  • HashModel.get()
  • JsonModel.get()
  • Any other classmethods that return model instances (e.g., save() returning self)

Prior work

PR #520 attempted this fix but was closed due to staleness. The approach was correct but needs to be updated for the current codebase.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Search the current definitions of RedisModel.get(), HashModel.get(), JsonModel.get(), and other model-returning classmethods, then compare the approach in closed PR #520 with the current codebase. Update the type annotations so subclass calls infer the concrete model type, and verify that IDE inference and linter checks recognize model attributes correctly.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, redis
Domain
backend, databases, developer-experience
Issue type
Refactor
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.