Add type hints to all test code (#1217)

* Add type hints to all test code

* Fixes

* Fix indentation

* Review fixes
This commit is contained in:
Marti Raudsepp
2022-10-31 11:20:10 +02:00
committed by GitHub
parent 9b4162beb1
commit e3c131bc61
16 changed files with 72 additions and 53 deletions

View File

@@ -164,12 +164,12 @@
qs = User.objects.annotate(Count('id'))
annotated_user = qs.get()
def animals_only(param: Animal):
def animals_only(param: Animal) -> None:
pass
# Make sure that even though attr access falls back to Any, the type is still checked
animals_only(annotated_user) # E: Argument 1 to "animals_only" has incompatible type "WithAnnotations[myapp__models__User]"; expected "Animal"
def users_allowed(param: User):
def users_allowed(param: User) -> None:
# But this function accepts only the original User type, so any attr access is not allowed within this function
param.foo # E: "User" has no attribute "foo"
# Passing in the annotated User to a function taking a (unannotated) User is OK

View File

@@ -19,7 +19,7 @@
main: |
from myapp.models import User
async def main():
async def main() -> None:
async for user in User.objects.all():
reveal_type(user) # N: Revealed type is "myapp.models.User"
installed_apps:

View File

@@ -46,7 +46,7 @@
_T = TypeVar('_T', bound=models.Model)
class Base(Generic[_T]):
def __init__(self, model_cls: Type[_T]):
def __init__(self, model_cls: Type[_T]) -> None:
self.model_cls = model_cls
reveal_type(self.model_cls._default_manager) # N: Revealed type is "django.db.models.manager.BaseManager[_T`1]"
class MyModel(models.Model):
@@ -71,7 +71,7 @@
_T = TypeVar('_T', bound=models.Model)
class Base(Generic[_T]):
def __init__(self, model_cls: Type[_T]):
def __init__(self, model_cls: Type[_T]) -> None:
self.model_cls = model_cls
reveal_type(self.model_cls._base_manager) # N: Revealed type is "django.db.models.manager.BaseManager[_T`1]"
class MyModel(models.Model):
@@ -350,11 +350,12 @@
- path: myapp/__init__.py
- path: myapp/models.py
content: |
from typing import Any
from django.db import models
class MyManager(models.Manager):
def get_instance(self) -> int:
pass
def get_instance_untyped(self, name):
def get_instance_untyped(self, name: str):
pass
class User(models.Model):
objects = MyManager()
@@ -389,21 +390,22 @@
installed_apps:
- myapp
out: |
myapp/models:4: error: Return type "MyModel" of "create" incompatible with return type "_T" in supertype "BaseManager"
myapp/models:5: error: Incompatible return value type (got "_T", expected "MyModel")
myapp/models:5: error: Return type "MyModel" of "create" incompatible with return type "_T" in supertype "BaseManager"
myapp/models:6: error: Incompatible return value type (got "_T", expected "MyModel")
files:
- path: myapp/__init__.py
- path: myapp/models.py
content: |
from typing import Any
from django.db import models
class MyModelManager(models.Manager):
def create(self, **kwargs) -> 'MyModel':
return super().create(**kwargs)
def create(self, **kwargs: Any) -> 'MyModel':
return super().create(**kwargs)
class MyModel(models.Model):
objects = MyModelManager()
objects = MyModelManager()
- case: override_manager_create2
@@ -416,15 +418,15 @@
- path: myapp/__init__.py
- path: myapp/models.py
content: |
from typing import Any
from django.db import models
class MyModelManager(models.Manager['MyModel']):
def create(self, **kwargs) -> 'MyModel':
return super().create(**kwargs)
def create(self, **kwargs: Any) -> 'MyModel':
return super().create(**kwargs)
class MyModel(models.Model):
objects = MyModelManager()
objects = MyModelManager()
- case: regression_manager_scope_foreign
main: |
@@ -488,7 +490,7 @@
class InvisibleUnresolvable(AbstractUnresolvable):
text = models.TextField()
def process_booking(user: User):
def process_booking(user: User) -> None:
reveal_type(User.objects)
reveal_type(User._default_manager)