Repository: typeddjango/django-stubs

Issue:
mypy 0.990 breaks mypy_django_plugin
mypy 0.990 was released today. When I have mypy 0.990 and `mypy_django_plugin` enabled, it fails with `AssertionError` on code that uses the `django_cas_ng` package.

The problematic code appears to be:
```python
from django.conf import settings as django_settings
# ...
def get_service_url(request: HttpRequest, redirect_to: Optional[str] = None) -> str:
    """Generates application django service URL for CAS"""
    if hasattr(django_settings, 'CAS_ROOT_PROXIED_AS') and django_settings.CAS_ROOT_PROXIED_AS:
    # ^^^^^^^^^^^  
```

Link to source: https://github.com/django-cas-ng/django-cas-ng/blob/a67653cf85244aa137edbabb3c2e94e07f97c773/django_cas_ng/utils.py#L50-L53

Output:
```
.../site-packages/django_cas_ng/utils.py:53: error: INTERNAL ERROR -- Please try using mypy master on GitHub:
https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build
Please report a bug at https://github.com/python/mypy/issues
version: 0.990
```

The stacktrace:
```
Traceback (most recent call last):
  File "mypy/checkexpr.py", line 4665, in accept
  File "mypy/nodes.py", line 1957, in accept
  File "mypy/checkexpr.py", line 2769, in visit_op_expr
  File "mypy/checkexpr.py", line 3406, in check_boolean_op
  File "mypy/checker.py", line 5184, in find_isinstance_check
  File "mypy/checker.py", line 5223, in find_isinstance_check_helper
  File "mypy/checker.py", line 6486, in hasattr_type_maps
  File "mypy/checker.py", line 6522, in has_valid_attribute
  File "mypy/checkmember.py", line 191, in analyze_member_access
  File "mypy/checkmember.py", line 210, in _analyze_member_access
  File "mypy/checkmember.py", line 331, in analyze_instance_member_access
  File "mypy/checkmember.py", line 547, in analyze_member_var_access
  File ".../site-packages/mypy_django_plugin/transformers/settings.py", line 24, in get_type_of_settings_attribute
    assert isinstance(ctx.context, MemberExpr)
AssertionError: 
```

## System information

- OS: macOS
- `python` version: 3.11.0
- `django` version: 4.1.3
- `mypy` version: 0.990
- `django-stubs` version: 1.13.0
- `django-stubs-ext` version: 0.7.0



Discussion:
When I print out `ctx.context` in `get_type_of_settings_attribute`, I get `TempNode:-1(Any)`

ctx: `AttributeContext(type=django.conf.LazySettings, default_attr_type=Any, context=TempNode:-1(Any), api=<mypy.checker.TypeChecker object at 0x10e936700>)`

Resolving patch:
diff --git a/mypy_django_plugin/transformers/settings.py b/mypy_django_plugin/transformers/settings.py
--- a/mypy_django_plugin/transformers/settings.py
+++ b/mypy_django_plugin/transformers/settings.py
@@ -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)


Test patch:
diff --git a/tests/typecheck/test_settings.yml b/tests/typecheck/test_settings.yml
--- a/tests/typecheck/test_settings.yml
+++ b/tests/typecheck/test_settings.yml
@@ -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"