Skip to content

Commit 8f30d9a

Browse files
authored
Increase priority of mypy internal Django settings import (#2127)
As discussed in #2097 (reply in thread): * django-stubs 5.0.0 introduces new cases of the "Import cycle from Django settings module prevents type inference" error that did not occur in 4.2.7. * This was bisected down to the change in commit e517a1f (#2024). * Updated the priority of internal settings import to mypy's `PRI_HIGH` level. Related commit a10f8aa (#2098)
1 parent c36c34d commit 8f30d9a

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

mypy_django_plugin/main.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from functools import partial
44
from typing import Any, Callable, Dict, List, Optional, Tuple, Type
55

6-
from mypy.build import PRI_MYPY
6+
from mypy.build import PRI_HIGH, PRI_MYPY
77
from mypy.modulefinder import mypy_path
88
from mypy.nodes import MypyFile, TypeInfo
99
from mypy.options import Options
@@ -99,14 +99,14 @@ def _get_typeinfo_or_none(self, class_name: str) -> Optional[TypeInfo]:
9999
return sym.node
100100
return None
101101

102-
def _new_dependency(self, module: str) -> Tuple[int, str, int]:
102+
def _new_dependency(self, module: str, priority: int = PRI_MYPY) -> Tuple[int, str, int]:
103103
fake_lineno = -1
104-
return (PRI_MYPY, module, fake_lineno)
104+
return (priority, module, fake_lineno)
105105

106106
def get_additional_deps(self, file: MypyFile) -> List[Tuple[int, str, int]]:
107107
# for settings
108108
if file.fullname == "django.conf" and self.django_context.django_settings_module:
109-
return [self._new_dependency(self.django_context.django_settings_module)]
109+
return [self._new_dependency(self.django_context.django_settings_module, PRI_HIGH)]
110110

111111
# for values / values_list
112112
if file.fullname == "django.db.models":
Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,36 @@
11
- case: test_setting_circular_import
22
main: |
33
from myapp import lib
4-
custom_settings: |
5-
from myapp.lib import function_returning_int
6-
7-
IMMEDIATE_VALUE = 123
8-
CIRCULAR_WITHOUT_HINT = function_returning_int()
9-
CIRCULAR_WITH_HINT: int = function_returning_int()
4+
mypy_config: |
5+
[mypy.plugins.django-stubs]
6+
django_settings_module = myapp.settings
107
files:
118
- path: myapp/__init__.py
9+
- path: myapp/settings.py
10+
content: |
11+
from myapp.lib import function_returning_int, const_with_circular_import
12+
13+
IMMEDIATE_VALUE = 123
14+
CIRCULAR_WITH_HINT: int = function_returning_int()
15+
CIRCULAR_WITHOUT_HINT_FUNCTION = function_returning_int()
16+
CIRCULAR_WITHOUT_HINT_CONST = const_with_circular_import
1217
- path: myapp/lib.py
1318
content: |
14-
from django.conf import settings
19+
from typing import TYPE_CHECKING
20+
import django.conf
21+
22+
settings = django.conf.settings
1523
1624
def test() -> None:
1725
reveal_type(settings.IMMEDIATE_VALUE) # N: Revealed type is "builtins.int"
18-
reveal_type(settings.CIRCULAR_WITHOUT_HINT) # E: Import cycle from Django settings module prevents type inference for 'CIRCULAR_WITHOUT_HINT' [misc] # N: Revealed type is "Any"
1926
reveal_type(settings.CIRCULAR_WITH_HINT) # N: Revealed type is "builtins.int"
27+
reveal_type(settings.CIRCULAR_WITHOUT_HINT_FUNCTION) # E: Import cycle from Django settings module prevents type inference for 'CIRCULAR_WITHOUT_HINT_FUNCTION' [misc] # N: Revealed type is "Any"
28+
reveal_type(settings.CIRCULAR_WITHOUT_HINT_CONST) # E: Import cycle from Django settings module prevents type inference for 'CIRCULAR_WITHOUT_HINT_CONST' [misc] # N: Revealed type is "Any"
2029
2130
def function_returning_int() -> int:
2231
return 42
32+
33+
if TYPE_CHECKING:
34+
const_with_circular_import = settings.IMMEDIATE_VALUE
35+
else:
36+
const_with_circular_import = 0

0 commit comments

Comments
 (0)