mirror of
https://github.com/davidhalter/django-stubs.git
synced 2025-12-06 20:24:31 +08:00
solve more use cases for related managers and settings
This commit is contained in:
@@ -119,7 +119,7 @@ class App(models.Model):
|
||||
def method(self) -> None:
|
||||
reveal_type(self.views) # E: Revealed type is 'django.db.models.query.QuerySet[main.View]'
|
||||
|
||||
[case models_related_managers_work_with_direct_model_inheritance_and_with_inheritance_from_other_model]
|
||||
[CASE models_related_managers_work_with_direct_model_inheritance_and_with_inheritance_from_other_model]
|
||||
from django.db.models import Model
|
||||
from django.db import models
|
||||
|
||||
@@ -134,4 +134,62 @@ class View2(View):
|
||||
|
||||
reveal_type(App().views) # E: Revealed type is 'django.db.models.query.QuerySet[main.View]'
|
||||
reveal_type(App().views2) # E: Revealed type is 'django.db.models.query.QuerySet[main.View2]'
|
||||
[out]
|
||||
[out]
|
||||
|
||||
[CASE models_imported_inside_init_file]
|
||||
from django.db import models
|
||||
from myapp.models import App
|
||||
class View(models.Model):
|
||||
app = models.ForeignKey(to='myapp.App', related_name='views', on_delete=models.CASCADE)
|
||||
reveal_type(View().app.views) # E: Revealed type is 'django.db.models.query.QuerySet[main.View]'
|
||||
|
||||
[file myapp/__init__.py]
|
||||
[file myapp/models/__init__.py]
|
||||
from .app import App
|
||||
[file myapp/models/app.py]
|
||||
from django.db import models
|
||||
class App(models.Model):
|
||||
pass
|
||||
|
||||
[CASE models_imported_inside_init_file_one_to_one_field]
|
||||
from django.db import models
|
||||
from myapp.models import User
|
||||
class Profile(models.Model):
|
||||
user = models.OneToOneField(to='myapp.User', related_name='profile', on_delete=models.CASCADE)
|
||||
reveal_type(Profile().user.profile) # E: Revealed type is 'main.Profile'
|
||||
|
||||
[file myapp/__init__.py]
|
||||
[file myapp/models/__init__.py]
|
||||
from .user import User
|
||||
[file myapp/models/user.py]
|
||||
from django.db import models
|
||||
class User(models.Model):
|
||||
pass
|
||||
|
||||
[CASE models_triple_circular_reference]
|
||||
from myapp.models import App
|
||||
reveal_type(App().owner.profile) # E: Revealed type is 'myapp.models.profile.Profile'
|
||||
|
||||
[file myapp/__init__.py]
|
||||
[file myapp/models/__init__.py]
|
||||
from .user import User
|
||||
from .profile import Profile
|
||||
from .app import App
|
||||
|
||||
[file myapp/models/user.py]
|
||||
from django.db import models
|
||||
class User(models.Model):
|
||||
pass
|
||||
|
||||
[file myapp/models/profile.py]
|
||||
from django.db import models
|
||||
from myapp.models import User
|
||||
class Profile(models.Model):
|
||||
user = models.OneToOneField(to='myapp.User', related_name='profile', on_delete=models.CASCADE)
|
||||
|
||||
[file myapp/models/app.py]
|
||||
from django.db import models
|
||||
class App(models.Model):
|
||||
owner = models.ForeignKey(to='myapp.User', on_delete=models.CASCADE, related_name='apps')
|
||||
|
||||
|
||||
|
||||
@@ -36,17 +36,47 @@ ROOT_DIR = Path(__file__)
|
||||
from django.conf import settings
|
||||
class Class:
|
||||
pass
|
||||
reveal_type(settings.MYSETTING) # E: Revealed type is 'builtins.int'
|
||||
reveal_type(settings.REGISTRY) # E: Revealed type is 'Union[main.Class, None]'
|
||||
reveal_type(settings.LIST) # E: Revealed type is 'Any'
|
||||
reveal_type(settings.BASE_LIST) # E: Revealed type is 'Any'
|
||||
[out]
|
||||
main:5: error: "LazySettings" has no attribute "LIST"
|
||||
main:6: error: "LazySettings" has no attribute "BASE_LIST"
|
||||
mysettings:4: error: Need type annotation for 'LIST'
|
||||
base:6: error: Need type annotation for 'BASE_LIST'
|
||||
|
||||
[env DJANGO_SETTINGS_MODULE=mysettings]
|
||||
[file mysettings.py]
|
||||
from typing import TYPE_CHECKING
|
||||
from typing import Optional
|
||||
from base import *
|
||||
|
||||
LIST = ['1', '2']
|
||||
|
||||
[file base.py]
|
||||
from typing import TYPE_CHECKING, Optional
|
||||
if TYPE_CHECKING:
|
||||
from main import Class
|
||||
|
||||
REGISTRY: Optional['Class'] = None
|
||||
BASE_LIST = ['3', '4']
|
||||
|
||||
[CASE test_circular_dependency_in_settings_works_if_settings_have_annotations]
|
||||
from django.conf import settings
|
||||
class Class:
|
||||
pass
|
||||
reveal_type(settings.MYSETTING) # E: Revealed type is 'builtins.int'
|
||||
reveal_type(settings.REGISTRY) # E: Revealed type is 'Union[main.Class, None]'
|
||||
reveal_type(settings.LIST) # E: Revealed type is 'builtins.list[builtins.str]'
|
||||
[out]
|
||||
|
||||
[env DJANGO_SETTINGS_MODULE=mysettings]
|
||||
[file mysettings.py]
|
||||
from typing import TYPE_CHECKING, Optional, List
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from main import Class
|
||||
|
||||
MYSETTING = 1122
|
||||
REGISTRY: Optional['Class'] = None
|
||||
LIST: List[str] = ['1', '2']
|
||||
|
||||
|
||||
Reference in New Issue
Block a user