Replace models.Model annotations with type variables (#603)

* Replace models.Model annotations with type variables

* Adds generic type args to generic views

* Adds more tests

* Revert "Adds generic type args to generic views"

This reverts commit 6522f30cdb9027483f46d77167394c84eb7b7f4b.

* Adds Generic support for DetailView and ListView

Co-authored-by: sobolevn <mail@sobolevn.me>
This commit is contained in:
Sondre Lillebø Gundersen
2021-05-01 12:21:19 +02:00
committed by GitHub
parent 5c3898d3b0
commit e4de8453cf
8 changed files with 133 additions and 16 deletions

View File

@@ -0,0 +1,55 @@
- case: generic_detail_view
main: |
from django.views.generic import DetailView
from django.db.models import QuerySet
from myapp.models import MyModel
class MyDetailView(DetailView[MyModel]):
model = MyModel
queryset = MyModel.objects.all()
def get_queryset(self) -> QuerySet[MyModel]:
self.get_object(super().get_queryset())
return super().get_queryset()
custom_settings: |
INSTALLED_APPS = ('myapp',)
files:
- path: myapp/__init__.py
- path: myapp/models.py
content: |
from django.db import models
class MyModel(models.Model):
...
- case: generic_detail_view_wrong
main: |
from django.views.generic import DetailView
from django.db.models import QuerySet
from myapp.models import MyModel, Other
class MyDetailView(DetailView[Other]):
model = MyModel
queryset = MyModel.objects.all()
def get_queryset(self) -> QuerySet[MyModel]:
self.get_object(super().get_queryset())
return super().get_queryset()
custom_settings: |
INSTALLED_APPS = ('myapp',)
files:
- path: myapp/__init__.py
- path: myapp/models.py
content: |
from django.db import models
class MyModel(models.Model):
...
class Other(models.Model):
...
out: |
main:7: error: Incompatible types in assignment (expression has type "Type[MyModel]", base class "SingleObjectMixin" defined the type as "Type[Other]")
main:8: error: Incompatible types in assignment (expression has type "Manager[MyModel]", base class "SingleObjectMixin" defined the type as "QuerySet[Other]")
main:10: error: Return type "QuerySet[MyModel]" of "get_queryset" incompatible with return type "QuerySet[Other]" in supertype "SingleObjectMixin"
main:12: error: Incompatible return value type (got "QuerySet[Other]", expected "QuerySet[MyModel]")

View File

@@ -0,0 +1,52 @@
- case: generic_list_view
main: |
from django.views.generic import ListView
from django.db.models import QuerySet
from myapp.models import MyModel
class MyListView(ListView[MyModel]):
model = MyModel
queryset = MyModel.objects.all()
def get_queryset(self) -> QuerySet[MyModel]:
...
custom_settings: |
INSTALLED_APPS = ('myapp',)
files:
- path: myapp/__init__.py
- path: myapp/models.py
content: |
from django.db import models
class MyModel(models.Model):
...
- case: generic_list_view_wrong
main: |
from django.views.generic import ListView
from django.db.models import QuerySet
from myapp.models import MyModel, Other
class MyListView(ListView[Other]):
model = MyModel
queryset = MyModel.objects.all()
def get_queryset(self) -> QuerySet[MyModel]:
...
custom_settings: |
INSTALLED_APPS = ('myapp',)
files:
- path: myapp/__init__.py
- path: myapp/models.py
content: |
from django.db import models
class MyModel(models.Model):
...
class Other(models.Model):
...
out: |
main:7: error: Incompatible types in assignment (expression has type "Type[MyModel]", base class "MultipleObjectMixin" defined the type as "Optional[Type[Other]]")
main:8: error: Incompatible types in assignment (expression has type "Manager[MyModel]", base class "MultipleObjectMixin" defined the type as "Optional[QuerySet[Other]]")
main:10: error: Return type "QuerySet[MyModel]" of "get_queryset" incompatible with return type "QuerySet[Other]" in supertype "MultipleObjectMixin"