machinalis / mypy-django

PEP-484 type hints bindings for the Django web framework
BSD 3-Clause "New" or "Revised" License
223 stars 23 forks source link

Support QuerySet with Generic types #7

Open w0rp opened 7 years ago

w0rp commented 7 years ago

I have some modified stubs for QuerySet and Manager so they accept type parameters. This makes the following possible.

for foo in Foo.objects.all(): # The type of foo is inferred as Foo

Using it requires stubbing models like so.

# foo.py
from django.db import models

class Foo(models.Model):
    bar = models.CharField(max_length=255)
# foo.pyi
from django.db import models

class Foo(models.Model):
    objects = ... # type: models.Manager['Foo']
    bar = ... # type: str

Would you be interested in adding this to the project if I create a pull request?