From 5d8cdbcf2975030c3d40ad2adbc4a8bc336c07e7 Mon Sep 17 00:00:00 2001 From: Maxim Kurnikov Date: Wed, 20 Feb 2019 02:38:39 +0300 Subject: [PATCH] fix integer set type --- django-stubs/conf/global_settings.pyi | 4 ++-- django-stubs/db/models/fields/__init__.pyi | 3 +-- mypy_django_plugin/helpers.py | 2 +- mypy_django_plugin/transformers/fields.py | 4 ++-- scripts/mypy.ini | 2 +- scripts/typecheck_tests.py | 5 +++-- test-data/typecheck/model_create.test | 6 +++--- test-data/typecheck/model_init.test | 8 ++++---- test-data/typecheck/related_fields.test | 4 ++-- test-data/typecheck/settings.test | 2 +- 10 files changed, 20 insertions(+), 20 deletions(-) diff --git a/django-stubs/conf/global_settings.pyi b/django-stubs/conf/global_settings.pyi index 5a6011b..292ba21 100644 --- a/django-stubs/conf/global_settings.pyi +++ b/django-stubs/conf/global_settings.pyi @@ -5,7 +5,7 @@ by the DJANGO_SETTINGS_MODULE environment variable. # This is defined here as a do-nothing function because we can't import # django.utils.translation -- that module depends on the settings. -from typing import Any, Dict, List, Optional, Pattern, Tuple, Protocol, Union, Callable, TYPE_CHECKING +from typing import Any, Dict, List, Optional, Pattern, Tuple, Protocol, Union, Callable, TYPE_CHECKING, Sequence #################### # CORE # @@ -377,7 +377,7 @@ CACHE_MIDDLEWARE_ALIAS = "default" AUTH_USER_MODEL: str = ... -AUTHENTICATION_BACKENDS: List[str] = ... +AUTHENTICATION_BACKENDS: Sequence[str] = ... LOGIN_URL = "/accounts/login/" diff --git a/django-stubs/db/models/fields/__init__.pyi b/django-stubs/db/models/fields/__init__.pyi index 5713dca..392d107 100644 --- a/django-stubs/db/models/fields/__init__.pyi +++ b/django-stubs/db/models/fields/__init__.pyi @@ -8,7 +8,6 @@ from django.core.exceptions import FieldDoesNotExist as FieldDoesNotExist from django.db.models.expressions import Combinable from django.db.models.query_utils import RegisterLookupMixin from django.forms import Field as FormField, Widget -from typing_extensions import Literal from .mixins import NOT_PROVIDED as NOT_PROVIDED @@ -72,7 +71,7 @@ class Field(RegisterLookupMixin, Generic[_ST, _GT]): def to_python(self, value: Any) -> Any: ... class IntegerField(Field[_ST, _GT]): - _pyi_private_set_type: Union[int, Combinable, Literal[""]] + _pyi_private_set_type: Union[float, int, str, Combinable] _pyi_private_get_type: int class PositiveIntegerRelDbTypeMixin: diff --git a/mypy_django_plugin/helpers.py b/mypy_django_plugin/helpers.py index 035ad5d..e42814d 100644 --- a/mypy_django_plugin/helpers.py +++ b/mypy_django_plugin/helpers.py @@ -246,7 +246,7 @@ def extract_primary_key_type_for_get(model: TypeInfo) -> Optional[Type]: def make_optional(typ: Type): - return UnionType.make_simplified_union([typ, NoneTyp()]) + return UnionType.make_union([typ, NoneTyp()]) def make_required(typ: Type) -> Type: diff --git a/mypy_django_plugin/transformers/fields.py b/mypy_django_plugin/transformers/fields.py index cefeff8..66eebcc 100644 --- a/mypy_django_plugin/transformers/fields.py +++ b/mypy_django_plugin/transformers/fields.py @@ -55,8 +55,8 @@ def convert_any_to_type(typ: Type, referred_to_type: Type) -> Type: converted_items = [] for item in typ.items: converted_items.append(convert_any_to_type(item, referred_to_type)) - return UnionType.make_simplified_union(converted_items, - line=typ.line, column=typ.column) + return UnionType.make_union(converted_items, + line=typ.line, column=typ.column) if isinstance(typ, Instance): args = [] for default_arg in typ.args: diff --git a/scripts/mypy.ini b/scripts/mypy.ini index bccc61f..0933a35 100644 --- a/scripts/mypy.ini +++ b/scripts/mypy.ini @@ -6,7 +6,7 @@ warn_no_return = False show_traceback = True warn_redundant_casts = True allow_redefinition = True -incremental = True +incremental = False plugins = mypy_django_plugin.main diff --git a/scripts/typecheck_tests.py b/scripts/typecheck_tests.py index 2f67271..6369b65 100644 --- a/scripts/typecheck_tests.py +++ b/scripts/typecheck_tests.py @@ -286,7 +286,8 @@ IGNORED_ERRORS = { ], 'schema': [ 'Incompatible type for "info" of "Note" (got "None", expected "Union[str, Combinable]")', - 'Incompatible type for "detail_info" of "NoteRename" (got "None", expected "Union[str, Combinable]")' + 'Incompatible type for "detail_info" of "NoteRename" (got "None", expected "Union[str, Combinable]")', + 'Incompatible type for "year" of "UniqueTest" (got "None", expected "Union[float, int, str, Combinable]")' ], 'settings_tests': [ 'Argument 1 to "Settings" has incompatible type "Optional[str]"; expected "str"' @@ -348,7 +349,7 @@ IGNORED_ERRORS = { ], 'sessions_tests': [ 'base class "SessionTestsMixin" defined the type as "None")', - 'Incompatible types in assignment (expression has type "None", variable has type "int")' + 'Incompatible types in assignment (expression has type "None", variable has type "int")', ], 'select_related_onetoone': [ '"None" has no attribute' diff --git a/test-data/typecheck/model_create.test b/test-data/typecheck/model_create.test index 5788670..a688eb9 100644 --- a/test-data/typecheck/model_create.test +++ b/test-data/typecheck/model_create.test @@ -6,7 +6,7 @@ class User(models.Model): age = models.IntegerField() User.objects.create(name='Max', age=10) -User.objects.create(age='hello') # E: Incompatible type for "age" of "User" (got "str", expected "Union[int, Combinable, Literal['']]") +User.objects.create(age=[]) # E: Incompatible type for "age" of "User" (got "List[Any]", expected "Union[float, int, str, Combinable]") [out] [CASE model_recognises_parent_attributes] @@ -59,6 +59,6 @@ MyModel.objects.create(id=None) class MyModel2(models.Model): id = models.IntegerField(primary_key=True, default=None) -MyModel2(id=None) # E: Incompatible type for "id" of "MyModel2" (got "None", expected "Union[int, Combinable, Literal['']]") -MyModel2.objects.create(id=None) # E: Incompatible type for "id" of "MyModel2" (got "None", expected "Union[int, Combinable, Literal['']]") +MyModel2(id=None) # E: Incompatible type for "id" of "MyModel2" (got "None", expected "Union[float, int, str, Combinable]") +MyModel2.objects.create(id=None) # E: Incompatible type for "id" of "MyModel2" (got "None", expected "Union[float, int, str, Combinable]") [out] \ No newline at end of file diff --git a/test-data/typecheck/model_init.test b/test-data/typecheck/model_init.test index 4cdc575..76e9b14 100644 --- a/test-data/typecheck/model_init.test +++ b/test-data/typecheck/model_init.test @@ -14,9 +14,9 @@ from django.db import models class MyUser(models.Model): name = models.CharField(max_length=100) age = models.IntegerField() -user = MyUser(name='hello', age='world') +user = MyUser(name='hello', age=[]) [out] -main:6: error: Incompatible type for "age" of "MyUser" (got "str", expected "Union[int, Combinable, Literal['']]") +main:6: error: Incompatible type for "age" of "MyUser" (got "List[Any]", expected "Union[float, int, str, Combinable]") [CASE arguments_to_init_combined_from_base_classes] from django.db import models @@ -63,7 +63,7 @@ from django.db import models class MyUser1(models.Model): mypk = models.IntegerField(primary_key=True) -user = MyUser1(pk='hello') # E: Incompatible type for "pk" of "MyUser1" (got "str", expected "Union[int, Combinable, Literal['']]") +user = MyUser1(pk=[]) # E: Incompatible type for "pk" of "MyUser1" (got "List[Any]", expected "Union[float, int, str, Combinable]") [out] [CASE can_set_foreign_key_by_its_primary_key] @@ -115,7 +115,7 @@ MyModel(1) class MyModel2(models.Model): name = models.IntegerField() MyModel2(1, 12) -MyModel2(1, 'Maxim') # E: Incompatible type for "name" of "MyModel2" (got "str", expected "Union[int, Combinable, Literal['']]") +MyModel2(1, []) # E: Incompatible type for "name" of "MyModel2" (got "List[Any]", expected "Union[float, int, str, Combinable]") [out] [CASE arguments_passed_as_dictionary_unpacking_are_not_supported] diff --git a/test-data/typecheck/related_fields.test b/test-data/typecheck/related_fields.test index 7eb21ca..bf6e655 100644 --- a/test-data/typecheck/related_fields.test +++ b/test-data/typecheck/related_fields.test @@ -271,9 +271,9 @@ class Book2(models.Model): reveal_type(Book2().publisher_id) # E: Revealed type is 'builtins.int' Book2(publisher_id=1) -Book2(publisher_id='hello') # E: Incompatible type for "publisher_id" of "Book2" (got "str", expected "Union[int, Combinable, Literal[''], None]") +Book2(publisher_id=[]) # E: Incompatible type for "publisher_id" of "Book2" (got "List[Any]", expected "Union[float, int, str, Combinable, None]") Book2.objects.create(publisher_id=1) -Book2.objects.create(publisher_id='hello') # E: Incompatible type for "publisher_id" of "Book2" (got "str", expected "Union[int, Combinable, Literal['']]") +Book2.objects.create(publisher_id=[]) # E: Incompatible type for "publisher_id" of "Book2" (got "List[Any]", expected "Union[float, int, str, Combinable]") [out] [CASE if_model_is_defined_as_name_of_the_class_look_for_it_in_the_same_file] diff --git a/test-data/typecheck/settings.test b/test-data/typecheck/settings.test index 33c6d3e..2d5ad1f 100644 --- a/test-data/typecheck/settings.test +++ b/test-data/typecheck/settings.test @@ -41,7 +41,7 @@ ROOT_DIR = Path(__file__) from django.conf import settings reveal_type(settings.AUTH_USER_MODEL) # E: Revealed type is 'builtins.str' -reveal_type(settings.AUTHENTICATION_BACKENDS) # E: Revealed type is 'builtins.list[builtins.str]' +reveal_type(settings.AUTHENTICATION_BACKENDS) # E: Revealed type is 'typing.Sequence[builtins.str]' [out] [CASE test_circular_dependency_in_settings_works_if_settings_have_annotations]