Skip to content

Commit 011b641

Browse files
GabDugadamchainz
andauthored
Fix overriding AppConfig.default_auto_field with string value (#1596)
Co-authored-by: Adam Johnson <me@adamj.eu>
1 parent 567aec4 commit 011b641

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

django-stubs/apps/config.pyi

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ from collections.abc import Iterator
33

44
from django.apps.registry import Apps
55
from django.db.models.base import Model
6-
from django.utils.functional import _StrOrPromise, cached_property
6+
from django.utils.functional import _Getter, _StrOrPromise
77

88
APPS_MODULE_NAME: str
99
MODELS_MODULE_NAME: str
@@ -16,10 +16,11 @@ class AppConfig:
1616
verbose_name: _StrOrPromise
1717
path: str
1818
models_module: str | None
19+
# Default auto_field is a cached_property on the base, but is usually subclassed as a str
20+
# If not subclassing with a str, a type ignore[override] is needed
1921
models: dict[str, type[Model]]
22+
default_auto_field: str | _Getter[str]
2023
def __init__(self, app_name: str, app_module: types.ModuleType | None) -> None: ...
21-
@cached_property
22-
def default_auto_field(self) -> str: ...
2324
@classmethod
2425
def create(cls, entry: str) -> AppConfig: ...
2526
def get_model(self, model_name: str, require_ready: bool = ...) -> type[Model]: ...
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
- case: test_appconfig_can_be_str
2+
main: |
3+
from django.apps.config import AppConfig
4+
from django.utils.functional import cached_property
5+
6+
class FooConfig(AppConfig):
7+
name = "foo"
8+
default_auto_field = "django.db.models.BigAutoField"
9+
10+
class BarConfig(AppConfig):
11+
name = "foo"
12+
@property
13+
def default_auto_field(self) -> str: # type: ignore[override]
14+
return "django.db.models.BigAutoField"
15+
16+
class BazConfig(AppConfig):
17+
name = "foo"
18+
@cached_property
19+
def default_auto_field(self) -> str: # type: ignore[override]
20+
return "django.db.models.BigAutoField"
21+
22+
class FooBarConfig(AppConfig):
23+
name = "foo"
24+
default_auto_field = cached_property(lambda self: "django.db.models.BigAutoField")
25+
26+
reveal_type(FooConfig.default_auto_field) # N: Revealed type is "builtins.str"
27+
reveal_type(BarConfig("bar", None).default_auto_field) # N: Revealed type is "builtins.str"
28+
reveal_type(BazConfig("baz", None).default_auto_field) # N: Revealed type is "builtins.str"
29+
reveal_type(FooBarConfig("baz", None).default_auto_field) # N: Revealed type is "builtins.str"

0 commit comments

Comments
 (0)