mirror of
https://github.com/davidhalter/django-stubs.git
synced 2025-12-08 21:14:49 +08:00
latest changes
This commit is contained in:
0
test/pytest_tests/__init__.py
Normal file
0
test/pytest_tests/__init__.py
Normal file
9
test/pytest_tests/base.py
Normal file
9
test/pytest_tests/base.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from test.pytest_plugin import MypyTypecheckTestCase
|
||||
|
||||
|
||||
class BaseDjangoPluginTestCase(MypyTypecheckTestCase):
|
||||
def ini_file(self):
|
||||
return """
|
||||
[mypy]
|
||||
plugins = mypy_django_plugin.main
|
||||
"""
|
||||
21
test/pytest_tests/test_model_fields.py
Normal file
21
test/pytest_tests/test_model_fields.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from test.pytest_plugin import reveal_type
|
||||
from test.pytest_tests.base import BaseDjangoPluginTestCase
|
||||
|
||||
|
||||
class TestBasicModelFields(BaseDjangoPluginTestCase):
|
||||
def test_model_field_classes_present_as_primitives(self):
|
||||
from django.db import models
|
||||
|
||||
class User(models.Model):
|
||||
id = models.AutoField(primary_key=True)
|
||||
small_int = models.SmallIntegerField()
|
||||
name = models.CharField(max_length=255)
|
||||
slug = models.SlugField(max_length=255)
|
||||
text = models.TextField()
|
||||
|
||||
user = User()
|
||||
reveal_type(user.id) # E: Revealed type is 'builtins.int'
|
||||
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'
|
||||
@@ -1,16 +1,9 @@
|
||||
from test.pytest_plugin import MypyTypecheckTestCase, reveal_type
|
||||
from test.pytest_plugin import reveal_type
|
||||
from test.pytest_tests.base import BaseDjangoPluginTestCase
|
||||
|
||||
|
||||
class BaseDjangoPluginTestCase(MypyTypecheckTestCase):
|
||||
def ini_file(self):
|
||||
return """
|
||||
[mypy]
|
||||
plugins = mypy_django_plugin.main
|
||||
"""
|
||||
|
||||
|
||||
class MyTestCase(BaseDjangoPluginTestCase):
|
||||
def check_foreign_key_field(self):
|
||||
class TestForeignKey(BaseDjangoPluginTestCase):
|
||||
def test_foreign_key_field(self):
|
||||
from django.db import models
|
||||
|
||||
class Publisher(models.Model):
|
||||
@@ -26,7 +19,7 @@ class MyTestCase(BaseDjangoPluginTestCase):
|
||||
publisher = Publisher()
|
||||
reveal_type(publisher.books) # E: Revealed type is 'django.db.models.query.QuerySet[main.Book]'
|
||||
|
||||
def check_every_foreign_key_creates_field_name_with_appended_id(self):
|
||||
def test_every_foreign_key_creates_field_name_with_appended_id(self):
|
||||
from django.db import models
|
||||
|
||||
class Publisher(models.Model):
|
||||
@@ -39,7 +32,7 @@ class MyTestCase(BaseDjangoPluginTestCase):
|
||||
book = Book()
|
||||
reveal_type(book.publisher_id) # E: Revealed type is 'builtins.int'
|
||||
|
||||
def check_foreign_key_different_order_of_params(self):
|
||||
def test_foreign_key_different_order_of_params(self):
|
||||
from django.db import models
|
||||
|
||||
class Publisher(models.Model):
|
||||
@@ -47,10 +40,50 @@ class MyTestCase(BaseDjangoPluginTestCase):
|
||||
|
||||
class Book(models.Model):
|
||||
publisher = models.ForeignKey(on_delete=models.CASCADE, to=Publisher,
|
||||
related_name='books')
|
||||
related_name='books')
|
||||
|
||||
book = Book()
|
||||
reveal_type(book.publisher) # E: Revealed type is 'main.Publisher*'
|
||||
|
||||
publisher = Publisher()
|
||||
reveal_type(publisher.books) # E: Revealed type is 'django.db.models.query.QuerySet[main.Book]'
|
||||
|
||||
|
||||
class TestOneToOneField(BaseDjangoPluginTestCase):
|
||||
def test_onetoone_field(self):
|
||||
from django.db import models
|
||||
|
||||
class User(models.Model):
|
||||
pass
|
||||
|
||||
class Profile(models.Model):
|
||||
user = models.OneToOneField(to=User, on_delete=models.CASCADE, related_name='profile')
|
||||
|
||||
profile = Profile()
|
||||
reveal_type(profile.user) # E: Revealed type is 'main.User*'
|
||||
|
||||
user = User()
|
||||
reveal_type(user.profile) # E: Revealed type is 'main.Profile'
|
||||
|
||||
def test_onetoone_field_with_underscore_id(self):
|
||||
from django.db import models
|
||||
|
||||
class User(models.Model):
|
||||
pass
|
||||
|
||||
class Profile(models.Model):
|
||||
user = models.OneToOneField(to=User, on_delete=models.CASCADE, related_name='profile')
|
||||
|
||||
profile = Profile()
|
||||
reveal_type(profile.user_id) # E: Revealed type is 'builtins.int'
|
||||
|
||||
def test_parameter_to_keyword_may_be_absent(self):
|
||||
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'
|
||||
|
||||
28
test/pytest_tests/test_objects_queryset.py
Normal file
28
test/pytest_tests/test_objects_queryset.py
Normal file
@@ -0,0 +1,28 @@
|
||||
from test.pytest_plugin import reveal_type, output
|
||||
from test.pytest_tests.base import BaseDjangoPluginTestCase
|
||||
|
||||
|
||||
class TestObjectsQueryset(BaseDjangoPluginTestCase):
|
||||
def test_every_model_has_objects_queryset_available(self):
|
||||
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]'
|
||||
|
||||
@output("""
|
||||
main:10: error: Revealed type is 'Any'
|
||||
main:10: error: "Type[ModelMixin]" has no attribute "objects"
|
||||
""")
|
||||
def test_objects_get_returns_model_instance(self):
|
||||
from django.db import models
|
||||
|
||||
class ModelMixin(models.Model):
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
class User(ModelMixin):
|
||||
pass
|
||||
|
||||
reveal_type(User.objects.get()) # E: Revealed type is 'main.User*'
|
||||
37
test/pytest_tests/test_parse_settings.py
Normal file
37
test/pytest_tests/test_parse_settings.py
Normal file
@@ -0,0 +1,37 @@
|
||||
from test.pytest_plugin import reveal_type, file, env
|
||||
from test.pytest_tests.base import BaseDjangoPluginTestCase
|
||||
|
||||
|
||||
class TestParseSettingsFromFile(BaseDjangoPluginTestCase):
|
||||
@env(DJANGO_SETTINGS_MODULE='mysettings')
|
||||
def test_case(self):
|
||||
from django.conf import settings
|
||||
|
||||
reveal_type(settings.ROOT_DIR) # E: Revealed type is 'builtins.str'
|
||||
reveal_type(settings.OBJ) # E: Revealed type is 'django.utils.functional.LazyObject'
|
||||
reveal_type(settings.NUMBERS) # E: Revealed type is 'builtins.list[Any]'
|
||||
reveal_type(settings.DICT) # E: Revealed type is 'builtins.dict[Any, Any]'
|
||||
|
||||
@file('mysettings.py')
|
||||
def mysettings_py_file(self):
|
||||
SECRET_KEY = 112233
|
||||
ROOT_DIR = '/etc'
|
||||
NUMBERS = ['one', 'two']
|
||||
DICT = {} # type: ignore
|
||||
|
||||
from django.utils.functional import LazyObject
|
||||
|
||||
OBJ = LazyObject()
|
||||
|
||||
|
||||
class TestSettingInitializableToNone(BaseDjangoPluginTestCase):
|
||||
@env(DJANGO_SETTINGS_MODULE='mysettings')
|
||||
def test_case(self):
|
||||
from django.conf import settings
|
||||
|
||||
reveal_type(settings.NONE_SETTING) # E: Revealed type is 'builtins.object'
|
||||
|
||||
@file('mysettings.py')
|
||||
def mysettings_py_file(self):
|
||||
SECRET_KEY = 112233
|
||||
NONE_SETTING = None
|
||||
27
test/pytest_tests/test_postgres_fields.py
Normal file
27
test/pytest_tests/test_postgres_fields.py
Normal file
@@ -0,0 +1,27 @@
|
||||
from test.pytest_plugin import reveal_type
|
||||
from test.pytest_tests.base import BaseDjangoPluginTestCase
|
||||
|
||||
|
||||
class TestArrayField(BaseDjangoPluginTestCase):
|
||||
def test_descriptor_access(self):
|
||||
from django.db import models
|
||||
from django.contrib.postgres.fields import ArrayField
|
||||
|
||||
class User(models.Model):
|
||||
array = ArrayField(base_field=models.Field())
|
||||
|
||||
user = User()
|
||||
reveal_type(user.array) # E: Revealed type is 'builtins.list[Any]'
|
||||
|
||||
def test_base_field_parsed_into_generic_attribute(self):
|
||||
from django.db import models
|
||||
from django.contrib.postgres.fields import ArrayField
|
||||
|
||||
class User(models.Model):
|
||||
members = ArrayField(base_field=models.IntegerField())
|
||||
members_as_text = ArrayField(base_field=models.CharField(max_length=255))
|
||||
|
||||
user = User()
|
||||
reveal_type(user.members) # E: Revealed type is 'builtins.list[builtins.int*]'
|
||||
reveal_type(user.members_as_text) # E: Revealed type is 'builtins.list[builtins.str*]'
|
||||
|
||||
74
test/pytest_tests/test_to_attr_as_string.py
Normal file
74
test/pytest_tests/test_to_attr_as_string.py
Normal file
@@ -0,0 +1,74 @@
|
||||
from test.pytest_plugin import file, reveal_type, env
|
||||
from test.pytest_tests.base import BaseDjangoPluginTestCase
|
||||
|
||||
|
||||
class TestForeignKey(BaseDjangoPluginTestCase):
|
||||
@env(DJANGO_SETTINGS_MODULE='mysettings')
|
||||
def _test_to_parameter_could_be_specified_as_string(self):
|
||||
from apps.myapp.models import Publisher
|
||||
|
||||
publisher = Publisher()
|
||||
reveal_type(publisher.books) # E: Revealed type is 'django.db.models.query.QuerySet[apps.myapp2.models.Book]'
|
||||
|
||||
# @env(DJANGO_SETTINGS_MODULE='mysettings')
|
||||
# def _test_creates_underscore_id_attr(self):
|
||||
# from apps.myapp2.models import Book
|
||||
#
|
||||
# book = Book()
|
||||
# reveal_type(book.publisher) # E: Revealed type is 'apps.myapp.models.Publisher'
|
||||
# reveal_type(book.publisher_id) # E: Revealed type is 'builtins.int'
|
||||
|
||||
@file('mysettings.py')
|
||||
def mysettings(self):
|
||||
SECRET_KEY = '112233'
|
||||
ROOT_DIR = '<TMP>'
|
||||
APPS_DIR = '<TMP>/apps'
|
||||
|
||||
INSTALLED_APPS = ('apps.myapp', 'apps.myapp2')
|
||||
|
||||
@file('apps/myapp/models.py', make_parent_packages=True)
|
||||
def apps_myapp_models(self):
|
||||
from django.db import models
|
||||
|
||||
class Publisher(models.Model):
|
||||
pass
|
||||
|
||||
@file('apps/myapp2/models.py', make_parent_packages=True)
|
||||
def apps_myapp2_models(self):
|
||||
from django.db import models
|
||||
|
||||
class Book(models.Model):
|
||||
publisher = models.ForeignKey(to='myapp.Publisher', on_delete=models.CASCADE,
|
||||
related_name='books')
|
||||
|
||||
|
||||
class TestOneToOneField(BaseDjangoPluginTestCase):
|
||||
@env(DJANGO_SETTINGS_MODULE='mysettings')
|
||||
def test_to_parameter_could_be_specified_as_string(self):
|
||||
from apps.myapp.models import User
|
||||
|
||||
user = User()
|
||||
reveal_type(user.profile) # E: Revealed type is 'apps.myapp2.models.Profile'
|
||||
|
||||
@file('mysettings.py')
|
||||
def mysettings(self):
|
||||
SECRET_KEY = '112233'
|
||||
ROOT_DIR = '<TMP>'
|
||||
APPS_DIR = '<TMP>/apps'
|
||||
|
||||
INSTALLED_APPS = ('apps.myapp', 'apps.myapp2')
|
||||
|
||||
@file('apps/myapp/models.py', make_parent_packages=True)
|
||||
def apps_myapp_models(self):
|
||||
from django.db import models
|
||||
|
||||
class User(models.Model):
|
||||
pass
|
||||
|
||||
@file('apps/myapp2/models.py', make_parent_packages=True)
|
||||
def apps_myapp2_models(self):
|
||||
from django.db import models
|
||||
|
||||
class Profile(models.Model):
|
||||
user = models.OneToOneField(to='myapp.User', on_delete=models.CASCADE,
|
||||
related_name='profile')
|
||||
Reference in New Issue
Block a user