From 297b11cbed2f344ea6e5ee97bc3f21d6b3384d3b Mon Sep 17 00:00:00 2001 From: Marti Raudsepp Date: Fri, 10 Nov 2023 13:08:14 +0200 Subject: [PATCH] Prepare for mypy 1.7 --- README.md | 1 + django-stubs/contrib/admin/utils.pyi | 2 +- django-stubs/core/files/base.pyi | 2 +- django-stubs/db/backends/utils.pyi | 6 +++--- django-stubs/db/models/sql/compiler.pyi | 4 ++-- django-stubs/forms/models.pyi | 2 +- django-stubs/template/response.pyi | 4 ++-- django-stubs/utils/encoding.pyi | 2 +- django-stubs/utils/formats.pyi | 4 ++-- django-stubs/utils/xmlutils.pyi | 3 ++- requirements.txt | 2 +- setup.py | 2 +- tests/typecheck/db/migrations/test_operations.yml | 2 +- tests/typecheck/fields/test_related.yml | 4 ++-- .../managers/querysets/test_from_queryset.yml | 10 +++++----- tests/typecheck/models/test_init.yml | 2 +- 16 files changed, 27 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index c7cd38c1d..b2bf1c668 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,7 @@ We rely on different `django` and `mypy` versions: | django-stubs | Mypy version | Django version | Django partial support | Python version | |----------------|--------------|----------------|------------------------|----------------| +| (next version) | 1.7.x | 4.2 | 4.1, 3.2 | 3.8 - 3.12 | | 4.2.6 | 1.6.x | 4.2 | 4.1, 3.2 | 3.8 - 3.12 | | 4.2.5 | 1.6.x | 4.2 | 4.1, 3.2 | 3.8 - 3.12 | | 4.2.4 | 1.5.x | 4.2 | 4.1, 3.2 | 3.8 - 3.11 | diff --git a/django-stubs/contrib/admin/utils.pyi b/django-stubs/contrib/admin/utils.pyi index 4f851d7ae..d35164f62 100644 --- a/django-stubs/contrib/admin/utils.pyi +++ b/django-stubs/contrib/admin/utils.pyi @@ -69,7 +69,7 @@ def lookup_field( name: Callable | str, obj: Model, model_admin: BaseModelAdmin | None = ... ) -> tuple[Field | None, str | None, Any]: ... @overload -def label_for_field( # type: ignore[misc] +def label_for_field( # type: ignore[overload-overlap] name: Callable | str, model: type[Model], model_admin: BaseModelAdmin | None = ..., diff --git a/django-stubs/core/files/base.pyi b/django-stubs/core/files/base.pyi index 2d95871a4..e4d0e9fb7 100644 --- a/django-stubs/core/files/base.pyi +++ b/django-stubs/core/files/base.pyi @@ -9,7 +9,7 @@ from typing_extensions import Self class File(FileProxyMixin[AnyStr], IO[AnyStr]): DEFAULT_CHUNK_SIZE: int file: IO[AnyStr] | None - name: str | None # type: ignore[assignment] + name: str | None mode: str def __init__(self, file: IO[AnyStr] | None, name: str | None = ...) -> None: ... def __bool__(self) -> bool: ... diff --git a/django-stubs/db/backends/utils.pyi b/django-stubs/db/backends/utils.pyi index ad6e540c2..7d1b2a0c4 100644 --- a/django-stubs/db/backends/utils.pyi +++ b/django-stubs/db/backends/utils.pyi @@ -69,15 +69,15 @@ class CursorDebugWrapper(CursorWrapper): ) -> Generator[None, None, None]: ... @overload -def typecast_date(s: None | Literal[""]) -> None: ... # type: ignore[misc] +def typecast_date(s: None | Literal[""]) -> None: ... # type: ignore[overload-overlap] @overload def typecast_date(s: str) -> datetime.date: ... @overload -def typecast_time(s: None | Literal[""]) -> None: ... # type: ignore[misc] +def typecast_time(s: None | Literal[""]) -> None: ... # type: ignore[overload-overlap] @overload def typecast_time(s: str) -> datetime.time: ... @overload -def typecast_timestamp(s: None | Literal[""]) -> None: ... # type: ignore[misc] +def typecast_timestamp(s: None | Literal[""]) -> None: ... # type: ignore[overload-overlap] @overload def typecast_timestamp(s: str) -> datetime.datetime: ... def split_identifier(identifier: str) -> tuple[str, str]: ... diff --git a/django-stubs/db/models/sql/compiler.pyi b/django-stubs/db/models/sql/compiler.pyi index 0931dbf4f..8cfa12887 100644 --- a/django-stubs/db/models/sql/compiler.pyi +++ b/django-stubs/db/models/sql/compiler.pyi @@ -97,7 +97,7 @@ class SQLCompiler: ) -> Iterator[Sequence[Any]]: ... def has_results(self) -> bool: ... @overload - def execute_sql( # type: ignore[misc] + def execute_sql( # type: ignore[overload-overlap] self, result_type: Literal["cursor"] = ..., chunked_fetch: bool = ..., chunk_size: int = ... ) -> CursorWrapper: ... @overload @@ -105,7 +105,7 @@ class SQLCompiler: self, result_type: Literal["no results"] | None = ..., chunked_fetch: bool = ..., chunk_size: int = ... ) -> None: ... @overload - def execute_sql( # type: ignore[misc] + def execute_sql( # type: ignore[overload-overlap] self, result_type: Literal["single"] = ..., chunked_fetch: bool = ..., chunk_size: int = ... ) -> Iterable[Sequence[Any]] | None: ... @overload diff --git a/django-stubs/forms/models.pyi b/django-stubs/forms/models.pyi index cd43a8798..b801a2ff0 100644 --- a/django-stubs/forms/models.pyi +++ b/django-stubs/forms/models.pyi @@ -304,7 +304,7 @@ class ModelMultipleChoiceField(ModelChoiceField): def modelform_defines_fields(form_class: type[ModelForm]) -> bool: ... @overload -def _get_foreign_key( # type: ignore[misc] +def _get_foreign_key( # type: ignore[overload-overlap] parent_model: type[Model], model: type[Model], fk_name: str | None = ..., can_fail: Literal[True] = ... ) -> ForeignKey | None: ... @overload diff --git a/django-stubs/template/response.pyi b/django-stubs/template/response.pyi index 1e94b0a58..e8c9e0bd8 100644 --- a/django-stubs/template/response.pyi +++ b/django-stubs/template/response.pyi @@ -19,7 +19,7 @@ class ContentNotRenderedError(Exception): ... class SimpleTemplateResponse(HttpResponse): content: Any closed: bool - cookies: SimpleCookie[str] + cookies: SimpleCookie[str] # type: ignore[type-arg] status_code: int rendering_attrs: Any template_name: _TemplateForResponseT @@ -50,7 +50,7 @@ class TemplateResponse(SimpleTemplateResponse): closed: bool context: RequestContext context_data: dict[str, Any] | None - cookies: SimpleCookie[str] + cookies: SimpleCookie[str] # type: ignore[type-arg] csrf_cookie_set: bool json: functools.partial _request: HttpRequest diff --git a/django-stubs/utils/encoding.pyi b/django-stubs/utils/encoding.pyi index 97b73128e..c8df836d7 100644 --- a/django-stubs/utils/encoding.pyi +++ b/django-stubs/utils/encoding.pyi @@ -60,7 +60,7 @@ def iri_to_uri(iri: None) -> None: ... @overload def iri_to_uri(iri: str | Promise) -> str: ... @overload -def uri_to_iri(uri: None) -> None: ... # type: ignore[misc] +def uri_to_iri(uri: None) -> None: ... # type: ignore[overload-overlap] @overload def uri_to_iri(uri: Any) -> str: ... def escape_uri_path(path: str) -> str: ... diff --git a/django-stubs/utils/formats.pyi b/django-stubs/utils/formats.pyi index a96f21193..e2182a19b 100644 --- a/django-stubs/utils/formats.pyi +++ b/django-stubs/utils/formats.pyi @@ -30,13 +30,13 @@ _T = TypeVar("_T") # details it works as expected (all values from Union are `localize`d to str, # while type of others is preserved) @overload -def localize( # type: ignore[misc] +def localize( # type: ignore[overload-overlap] value: builtin_datetime | date | time | Decimal | float | str, use_l10n: bool | None = ... ) -> str: ... @overload def localize(value: _T, use_l10n: bool | None = ...) -> _T: ... @overload -def localize_input( # type: ignore[misc] +def localize_input( # type: ignore[overload-overlap] value: builtin_datetime | date | time | Decimal | float | str, default: str | None = ... ) -> str: ... @overload diff --git a/django-stubs/utils/xmlutils.pyi b/django-stubs/utils/xmlutils.pyi index 955b42183..78f5bda2b 100644 --- a/django-stubs/utils/xmlutils.pyi +++ b/django-stubs/utils/xmlutils.pyi @@ -1,8 +1,9 @@ from xml.sax.saxutils import XMLGenerator +from xml.sax.xmlreader import AttributesImpl class UnserializableContentError(ValueError): ... class SimplerXMLGenerator(XMLGenerator): def addQuickElement(self, name: str, contents: str | None = ..., attrs: dict[str, str] | None = ...) -> None: ... def characters(self, content: str) -> None: ... - def startElement(self, name: str, attrs: dict[str, str]) -> None: ... + def startElement(self, name: str, attrs: AttributesImpl) -> None: ... diff --git a/requirements.txt b/requirements.txt index 8f31fe6a1..38aa0e16e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,4 +12,4 @@ Django==4.2.7 -e .[compatible-mypy] # Overrides: -mypy==1.6.1 +git+https://github.com/python/mypy@release-1.7 diff --git a/setup.py b/setup.py index 18bb048eb..d561aa8a7 100644 --- a/setup.py +++ b/setup.py @@ -32,7 +32,7 @@ def find_stub_files(name: str) -> List[str]: # Keep compatible-mypy major.minor version pinned to what we use in CI (requirements.txt) extras_require = { - "compatible-mypy": ["mypy~=1.6.0"], + "compatible-mypy": ["mypy~=1.7.0"], } setup( diff --git a/tests/typecheck/db/migrations/test_operations.yml b/tests/typecheck/db/migrations/test_operations.yml index 3ddc9a9f7..c51bd8944 100644 --- a/tests/typecheck/db/migrations/test_operations.yml +++ b/tests/typecheck/db/migrations/test_operations.yml @@ -25,7 +25,7 @@ RunSQL(sql=["SOME SQLS", ("SOME SQLS %s, %s", [object(), "ANOTHER PARAM"])]) - RunSQL(sql=("SOME SQL", {})) # E: Argument "sql" to "RunSQL" has incompatible type "Tuple[str, Dict[, ]]"; expected "Union[str, Union[List[Union[str, Tuple[str, Union[Dict[str, Any], Union[List[Any], Tuple[Any, ...], Tuple[()]], None]]]], Tuple[Union[str, Tuple[str, Union[Dict[str, Any], Union[List[Any], Tuple[Any, ...], Tuple[()]], None]]], ...], Tuple[()]]]" [arg-type] + RunSQL(sql=("SOME SQL", {})) # E: Argument "sql" to "RunSQL" has incompatible type "Tuple[str, Dict[Never, Never]]"; expected "Union[str, Union[List[Union[str, Tuple[str, Union[Dict[str, Any], Union[List[Any], Tuple[Any, ...], Tuple[()]], None]]]], Tuple[Union[str, Tuple[str, Union[Dict[str, Any], Union[List[Any], Tuple[Any, ...], Tuple[()]], None]]], ...], Tuple[()]]]" [arg-type] RunSQL(sql=("SOME SQL", 1)) # E: Argument "sql" to "RunSQL" has incompatible type "Tuple[str, int]"; expected "Union[str, Union[List[Union[str, Tuple[str, Union[Dict[str, Any], Union[List[Any], Tuple[Any, ...], Tuple[()]], None]]]], Tuple[Union[str, Tuple[str, Union[Dict[str, Any], Union[List[Any], Tuple[Any, ...], Tuple[()]], None]]], ...], Tuple[()]]]" [arg-type] RunSQL(sql=("SOME SQL", None)) # E: Argument "sql" to "RunSQL" has incompatible type "Tuple[str, None]"; expected "Union[str, Union[List[Union[str, Tuple[str, Union[Dict[str, Any], Union[List[Any], Tuple[Any, ...], Tuple[()]], None]]]], Tuple[Union[str, Tuple[str, Union[Dict[str, Any], Union[List[Any], Tuple[Any, ...], Tuple[()]], None]]], ...], Tuple[()]]]" [arg-type] RunSQL(sql=("SOME SQLS %(VAL)s", {1: "FOO"})) # E: Argument "sql" to "RunSQL" has incompatible type "Tuple[str, Dict[int, str]]"; expected "Union[str, Union[List[Union[str, Tuple[str, Union[Dict[str, Any], Union[List[Any], Tuple[Any, ...], Tuple[()]], None]]]], Tuple[Union[str, Tuple[str, Union[Dict[str, Any], Union[List[Any], Tuple[Any, ...], Tuple[()]], None]]], ...], Tuple[()]]]" [arg-type] diff --git a/tests/typecheck/fields/test_related.yml b/tests/typecheck/fields/test_related.yml index 36543b6c7..6c5ece0ef 100644 --- a/tests/typecheck/fields/test_related.yml +++ b/tests/typecheck/fields/test_related.yml @@ -650,8 +650,8 @@ user = MyUser() reveal_type(user.book_set) # N: Revealed type is "myapp.models.Book_RelatedManager" reveal_type(user.article_set) # N: Revealed type is "myapp.models.Article_RelatedManager" - reveal_type(user.book_set.add) # N: Revealed type is "def (*objs: Union[myapp.models.Book, builtins.int], *, bulk: builtins.bool =)" - reveal_type(user.article_set.add) # N: Revealed type is "def (*objs: Union[myapp.models.Article, builtins.int], *, bulk: builtins.bool =)" + reveal_type(user.book_set.add) # N: Revealed type is "def (*objs: Union[myapp.models.Book, builtins.int], bulk: builtins.bool =)" + reveal_type(user.article_set.add) # N: Revealed type is "def (*objs: Union[myapp.models.Article, builtins.int], bulk: builtins.bool =)" reveal_type(user.book_set.filter) # N: Revealed type is "def (*args: Any, **kwargs: Any) -> myapp.models.LibraryEntityQuerySet" reveal_type(user.book_set.get()) # N: Revealed type is "myapp.models.Book" reveal_type(user.article_set.filter) # N: Revealed type is "def (*args: Any, **kwargs: Any) -> myapp.models.LibraryEntityQuerySet" diff --git a/tests/typecheck/managers/querysets/test_from_queryset.yml b/tests/typecheck/managers/querysets/test_from_queryset.yml index 4bae4dcef..59f9e8c56 100644 --- a/tests/typecheck/managers/querysets/test_from_queryset.yml +++ b/tests/typecheck/managers/querysets/test_from_queryset.yml @@ -257,7 +257,7 @@ - case: from_queryset_returns_intersection_of_manager_and_queryset main: | from myapp.models import MyModel, NewManager - reveal_type(NewManager()) # N: Revealed type is "myapp.models.ModelBaseManagerFromModelQuerySet[]" + reveal_type(NewManager()) # N: Revealed type is "myapp.models.ModelBaseManagerFromModelQuerySet[Never]" reveal_type(MyModel.objects) # N: Revealed type is "myapp.models.ModelBaseManagerFromModelQuerySet[myapp.models.MyModel]" reveal_type(MyModel.objects.get()) # N: Revealed type is "myapp.models.MyModel" reveal_type(MyModel.objects.manager_only_method()) # N: Revealed type is "builtins.int" @@ -283,12 +283,12 @@ - case: from_queryset_with_class_name_provided main: | from myapp.models import MyModel, NewManager, OtherModel, OtherManager - reveal_type(NewManager()) # N: Revealed type is "myapp.models.NewManager[]" + reveal_type(NewManager()) # N: Revealed type is "myapp.models.NewManager[Never]" reveal_type(MyModel.objects) # N: Revealed type is "myapp.models.NewManager[myapp.models.MyModel]" reveal_type(MyModel.objects.get()) # N: Revealed type is "myapp.models.MyModel" reveal_type(MyModel.objects.manager_only_method()) # N: Revealed type is "builtins.int" reveal_type(MyModel.objects.manager_and_queryset_method()) # N: Revealed type is "builtins.str" - reveal_type(OtherManager()) # N: Revealed type is "myapp.models.X[]" + reveal_type(OtherManager()) # N: Revealed type is "myapp.models.X[Never]" reveal_type(OtherModel.objects) # N: Revealed type is "myapp.models.X[myapp.models.OtherModel]" reveal_type(OtherModel.objects.manager_only_method()) # N: Revealed type is "builtins.int" reveal_type(OtherModel.objects.manager_and_queryset_method()) # N: Revealed type is "builtins.str" @@ -657,7 +657,7 @@ - case: handles_name_collision_with_generated_type main: | from myapp.models import MyModel, BaseManagerFromModelQuerySet - reveal_type(BaseManagerFromModelQuerySet()) # N: Revealed type is "myapp.models.BaseManagerFromModelQuerySet[]" + reveal_type(BaseManagerFromModelQuerySet()) # N: Revealed type is "myapp.models.BaseManagerFromModelQuerySet[Never]" reveal_type(MyModel.objects) # N: Revealed type is "myapp.models.BaseManagerFromModelQuerySet[myapp.models.MyModel]" installed_apps: - myapp @@ -679,7 +679,7 @@ main: | from myapp.models import MyModel, Generated, BaseManagerFromModelQuerySet reveal_type(BaseManagerFromModelQuerySet) # N: Revealed type is "builtins.int" - reveal_type(Generated()) # N: Revealed type is "myapp.models.BaseManagerFromModelQuerySet1[]" + reveal_type(Generated()) # N: Revealed type is "myapp.models.BaseManagerFromModelQuerySet1[Never]" reveal_type(MyModel.objects) # N: Revealed type is "myapp.models.BaseManagerFromModelQuerySet1[myapp.models.MyModel]" installed_apps: - myapp diff --git a/tests/typecheck/models/test_init.yml b/tests/typecheck/models/test_init.yml index 1582cf1e1..ddeb91a69 100644 --- a/tests/typecheck/models/test_init.yml +++ b/tests/typecheck/models/test_init.yml @@ -292,7 +292,7 @@ main:9: error: Incompatible types in assignment (expression has type "str", variable has type "int") [assignment] main:10: error: Incompatible types in assignment (expression has type "str", variable has type "Union[int, float]") [assignment] main:12: error: Incompatible types in assignment (expression has type "List[str]", variable has type "Sequence[Union[int, float]]") [assignment] - main:13: error: Incompatible types in assignment (expression has type "List[]", variable has type "Union[float, int, str, Combinable]") [assignment] + main:13: error: Incompatible types in assignment (expression has type "List[Never]", variable has type "Union[float, int, str, Combinable]") [assignment] main:15: error: Incompatible type for "redefined_set_type" of "MyModel" (got "str", expected "int") [misc] main:15: error: Incompatible type for "redefined_union_set_type" of "MyModel" (got "str", expected "Union[int, float]") [misc] main:15: error: Incompatible type for "redefined_array_set_type" of "MyModel" (got "int", expected "Sequence[Union[int, float]]") [misc]