Skip to content

Commit 35847cc

Browse files
authored
Fix hasattr(settings) AssertionError with mypy 0.990 (#1239)
Fixes #1235. It seems this was caused by a new feature in mypy 0.990 involving `hasattr()`.
1 parent 981e0e5 commit 35847cc

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

mypy_django_plugin/transformers/settings.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ def get_user_model_hook(ctx: FunctionContext, django_context: DjangoContext) ->
2121

2222

2323
def get_type_of_settings_attribute(ctx: AttributeContext, django_context: DjangoContext) -> MypyType:
24-
assert isinstance(ctx.context, MemberExpr)
24+
if not isinstance(ctx.context, MemberExpr):
25+
return ctx.default_attr_type
26+
2527
setting_name = ctx.context.name
2628

2729
typechecker_api = helpers.get_typechecker_api(ctx)

tests/typecheck/test_settings.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,16 @@
4545
content: |
4646
from pathlib import Path
4747
MEDIA_ROOT = Path()
48+
49+
- case: settings_hasattr
50+
main: |
51+
from django.conf import settings
52+
if hasattr(settings, 'AUTH_USER_MODEL') and settings.AUTH_USER_MODEL:
53+
reveal_type(settings.AUTH_USER_MODEL)
54+
if hasattr(settings, 'NON_EXISTANT_SETTING') and settings.NON_EXISTANT_SETTING:
55+
reveal_type(settings.NON_EXISTANT_SETTING)
56+
out: |
57+
main:3: note: Revealed type is "builtins.str"
58+
main:4: error: 'Settings' object has no attribute 'NON_EXISTANT_SETTING'
59+
main:5: error: 'Settings' object has no attribute 'NON_EXISTANT_SETTING'
60+
main:5: note: Revealed type is "Any"

0 commit comments

Comments
 (0)