Skip to content

Commit

Permalink
Fix hasattr(settings) AssertionError with mypy 0.990 (#1239)
Browse files Browse the repository at this point in the history
Fixes #1235. It seems this was caused by a new feature in mypy 0.990 involving `hasattr()`.
  • Loading branch information
intgr authored Nov 8, 2022
1 parent 981e0e5 commit 35847cc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
4 changes: 3 additions & 1 deletion mypy_django_plugin/transformers/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ def get_user_model_hook(ctx: FunctionContext, django_context: DjangoContext) ->


def get_type_of_settings_attribute(ctx: AttributeContext, django_context: DjangoContext) -> MypyType:
assert isinstance(ctx.context, MemberExpr)
if not isinstance(ctx.context, MemberExpr):
return ctx.default_attr_type

setting_name = ctx.context.name

typechecker_api = helpers.get_typechecker_api(ctx)
Expand Down
13 changes: 13 additions & 0 deletions tests/typecheck/test_settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,16 @@
content: |
from pathlib import Path
MEDIA_ROOT = Path()
- case: settings_hasattr
main: |
from django.conf import settings
if hasattr(settings, 'AUTH_USER_MODEL') and settings.AUTH_USER_MODEL:
reveal_type(settings.AUTH_USER_MODEL)
if hasattr(settings, 'NON_EXISTANT_SETTING') and settings.NON_EXISTANT_SETTING:
reveal_type(settings.NON_EXISTANT_SETTING)
out: |
main:3: note: Revealed type is "builtins.str"
main:4: error: 'Settings' object has no attribute 'NON_EXISTANT_SETTING'
main:5: error: 'Settings' object has no attribute 'NON_EXISTANT_SETTING'
main:5: note: Revealed type is "Any"

0 comments on commit 35847cc

Please sign in to comment.