sbdchd / django-types

:doughnut: Type stubs for Django
MIT License
188 stars 62 forks source link

Is it possible to make Model compatible with it's subclasses? #91

Closed last-partizan closed 1 year ago

last-partizan commented 2 years ago

I found myself doing something like this

instance: Post = apps.get_model(var_post_model).objects.get()
# and
instance: Post = ContentType.objects.get(pk=ct).get_object_for_this_type(pk=pk)

Both of those functions are returning Model, which triggers type error when i want to specify exact type.

Is there a way to make Model compatible with it's subclasses?

sbdchd commented 2 years ago

Hmm, I think this would be tricky without some code gen / compiler magic since apps.get_model only has a string as its param.

https://docs.djangoproject.com/en/4.0/ref/applications/#django.apps.apps.get_model

One option would be to make them return Any, which might be more practical than Model

last-partizan commented 1 year ago

I was thinking about

_M = TypeVar("_M", bound=Model)
...
    def get_model(
        self, app_label: str, model_name: Optional[str] = ..., require_ready: bool = ...
    ) -> Type[_M]: ...

But, probably it's best to be left it in it's current state. When i know which types i will be getting - it's easier to just import them.