From 447651c770f2d5321c974a4f9ac4df8324afd06d Mon Sep 17 00:00:00 2001 From: Maxim Kurnikov Date: Thu, 6 Dec 2018 17:45:27 +0300 Subject: [PATCH] do not emit error, if attribute does not exist --- mypy_django_plugin/plugins/models.py | 2 ++ mypy_django_plugin/plugins/settings.py | 1 + test-data/typecheck/fields.test | 1 - test-data/typecheck/related_fields.test | 1 - test-data/typecheck/settings.test | 9 +++++++-- 5 files changed, 10 insertions(+), 4 deletions(-) diff --git a/mypy_django_plugin/plugins/models.py b/mypy_django_plugin/plugins/models.py index 7ecd1c8..ce2ef1c 100644 --- a/mypy_django_plugin/plugins/models.py +++ b/mypy_django_plugin/plugins/models.py @@ -176,3 +176,5 @@ def process_model_class(ctx: ClassDefContext) -> None: set_fieldname_attrs_for_related_fields(ctx) add_int_id_attribute_if_primary_key_true_is_not_present(ctx) set_objects_queryset_to_model_class(ctx) + + ctx.cls.info.fallback_to_any = True diff --git a/mypy_django_plugin/plugins/settings.py b/mypy_django_plugin/plugins/settings.py index ef3f24c..0f63ea9 100644 --- a/mypy_django_plugin/plugins/settings.py +++ b/mypy_django_plugin/plugins/settings.py @@ -59,6 +59,7 @@ def add_settings_to_django_conf_object(ctx: ClassDefContext, context.set_line(sym.node) api.msg.report(f"Need type annotation for '{sym.node.name()}'", context, severity='error', file=module_file.path) + ctx.cls.info.fallback_to_any = True class DjangoConfSettingsInitializerHook(object): diff --git a/test-data/typecheck/fields.test b/test-data/typecheck/fields.test index fad8b1f..96c0e9b 100644 --- a/test-data/typecheck/fields.test +++ b/test-data/typecheck/fields.test @@ -54,7 +54,6 @@ class User(models.Model): reveal_type(User().my_pk) # E: Revealed type is 'builtins.int' reveal_type(User().id) # E: Revealed type is 'Any' [out] -main:7: error: "User" has no attribute "id" [CASE test_meta_nested_class_allows_subclassing_in_multiple_inheritance] from typing import Any diff --git a/test-data/typecheck/related_fields.test b/test-data/typecheck/related_fields.test index 22a4106..92a05d6 100644 --- a/test-data/typecheck/related_fields.test +++ b/test-data/typecheck/related_fields.test @@ -110,7 +110,6 @@ class View(models.Model): reveal_type(View().app.views) # E: Revealed type is 'django.db.models.query.QuerySet[main.View]' reveal_type(View().app.unknown) # E: Revealed type is 'Any' [out] -main:7: error: "App" has no attribute "unknown" [file myapp/__init__.py] [file myapp/models.py] diff --git a/test-data/typecheck/settings.test b/test-data/typecheck/settings.test index 9d12d18..4d62ea8 100644 --- a/test-data/typecheck/settings.test +++ b/test-data/typecheck/settings.test @@ -40,8 +40,6 @@ reveal_type(settings.REGISTRY) # E: Revealed type is 'Union[main.Class, None]' reveal_type(settings.LIST) # E: Revealed type is 'Any' reveal_type(settings.BASE_LIST) # E: Revealed type is 'Any' [out] -main:5: error: "LazySettings" has no attribute "LIST" -main:6: error: "LazySettings" has no attribute "BASE_LIST" mysettings:4: error: Need type annotation for 'LIST' base:6: error: Need type annotation for 'BASE_LIST' @@ -80,3 +78,10 @@ MYSETTING = 1122 REGISTRY: Optional['Class'] = None LIST: List[str] = ['1', '2'] +[CASE allow_calls_to_nonexistent_members_for_now] +from django.conf import settings +reveal_type(settings.NOT_EXISTING) # E: Revealed type is 'Any' + +[env DJANGO_SETTINGS_MODULE=mysettings] +[file mysettings.py] +[out]