support for models.Model.objects, abstract mixins

This commit is contained in:
Maxim Kurnikov
2018-11-14 02:33:50 +03:00
parent 9a68263257
commit 41cc79b957
13 changed files with 200 additions and 103 deletions

View File

@@ -1,4 +1,3 @@
[mypy]
plugins =
mypy_django_plugin.plugins.postgres_fields,
mypy_django_plugin.plugins.related_fields
mypy_django_plugin.plugin

View File

@@ -1,7 +1,6 @@
[case testBasicModelFields]
from django.db import models
class User(models.Model):
id = models.AutoField(primary_key=True)
small_int = models.SmallIntegerField()
@@ -15,4 +14,4 @@ reveal_type(user.small_int) # E: Revealed type is 'builtins.int'
reveal_type(user.name) # E: Revealed type is 'builtins.str'
reveal_type(user.slug) # E: Revealed type is 'builtins.str'
reveal_type(user.text) # E: Revealed type is 'builtins.str'
[out]
[out]

View File

@@ -55,4 +55,17 @@ class Profile(models.Model):
profile = Profile()
reveal_type(profile.user_id) # E: Revealed type is 'builtins.int'
[out]
[out]
[case testToParameterKeywordMaybeAbsent]
from django.db import models
class User(models.Model):
pass
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile')
reveal_type(User().profile) # E: Revealed type is 'main.Profile'
[out]

View File

@@ -0,0 +1,24 @@
[case testEveryModelClassHasDefaultObjectsQuerySetAvailableAsAttribute]
from django.db import models
class User(models.Model):
pass
reveal_type(User.objects) # E: Revealed type is 'django.db.models.query.QuerySet[main.User]'
[out]
[case testGetReturnsModelInstanceIfInheritedFromAbstractMixin]
from django.db import models
class ModelMixin(models.Model):
class Meta:
abstract = True
class User(ModelMixin):
pass
reveal_type(ModelMixin.objects)
reveal_type(User.objects.get()) # E: Revealed type is 'main.User*'
[out]
main:10: error: Revealed type is 'Any'
main:10: error: "Type[ModelMixin]" has no attribute "objects"

View File

@@ -14,9 +14,10 @@ MYPY_INI_PATH = ROOT_DIR / 'test' / 'plugins.ini'
class DjangoTestSuite(DataSuite):
files = [
'check-objects-queryset.test',
'check-model-fields.test',
'check-postgres-fields.test',
'check-model-relations.test',
'check-model-relations.test'
]
data_prefix = str(TEST_DATA_DIR)