Skip to content

Improve type annotations for model classmethods #742

@abrookins

Description

@abrookins

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions