just-work / django-testing-utils

MIT License
1 stars 3 forks source link

Mypy "type[Model]" has no attribute "objects" #67

Open tmozhaev opened 7 months ago

tmozhaev commented 7 months ago

It's almost basic functionality of django-stubs :(

tumb1er commented 7 months ago

https://github.com/typeddjango/django-stubs/blob/master/django-stubs/db/models/query.pyi#L47

django-stubs internally uses following typing for concrete models:

_T = TypeVar("_T", bound=Model, covariant=True)

class _QuerySet(Generic[_T, _Row], Collection[_Row], Reversible[_Row], Sized):
    model: type[_T]
    ...

QuerySet: TypeAlias = _QuerySet[_T, _T]

I think TypeVar and covariant may be a solution for this issue: just make base class generic and annotate model attribute the same way.

tumb1er commented 5 months ago

https://github.com/just-work/django-admin-smoke/pull/87/commits/c4cd684f52c27e464bbbbb3d17bf2e845afb9c18

Solved this in django-admin-smoke:

M = TypeVar("M", models.Model)
class MyTestCase(Generic[M]):
    model: Type[M]