From a4d734dc8a332117585a8cdae37c6e73803ea1c2 Mon Sep 17 00:00:00 2001 From: Marti Raudsepp Date: Thu, 27 Apr 2023 17:29:42 +0300 Subject: [PATCH 1/3] Completely drop Python 3.7 compatibility * Latest Django 4.2 no longer supports Python 3.7 * Our CI dependency `pre-commit` no longer supports Python 3.7 * Python 3.7 support ends in 2 months (https://endoflife.date/python) * We can clean up a few type stubs --- README.md | 2 -- django-stubs/core/mail/message.pyi | 11 ++--------- django_stubs_ext/setup.py | 3 +-- setup.py | 3 +-- 4 files changed, 4 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index d48b4faf3..17916d646 100644 --- a/README.md +++ b/README.md @@ -124,8 +124,6 @@ This happens because these Django classes do not support [`__class_getitem__`](h django_stubs_ext.monkeypatch() ``` - Note: This monkey patching approach will only work when using Python 3.7 and higher, when the `__class_getitem__` magic method was introduced. - You can add extra types to patch with `django_stubs_ext.monkeypatch(extra_classes=[YourDesiredType])` 2. You can use strings instead: `'QuerySet[MyModel]'` and `'Manager[MyModel]'`, this way it will work as a type for `mypy` and as a regular `str` in runtime. diff --git a/django-stubs/core/mail/message.pyi b/django-stubs/core/mail/message.pyi index 23b95fa15..fe79bd3aa 100644 --- a/django-stubs/core/mail/message.pyi +++ b/django-stubs/core/mail/message.pyi @@ -6,10 +6,7 @@ from email.mime.base import MIMEBase from email.mime.message import MIMEMessage from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText - -# switch to tuple once https://github.com/python/mypy/issues/11098 is fixed -# remove Optional once python 3.7 is dropped (Tuple[str | None, ...] works with mypy on py3.10) -from typing import Any, Optional, Tuple, overload # noqa: Y022, Y037 +from typing import Any, overload from django.utils.functional import _StrOrPromise from typing_extensions import TypeAlias @@ -66,12 +63,8 @@ class SafeMIMEMultipart(MIMEMixin, MIMEMultipart): # type: ignore def __setitem__(self, name: str, val: str) -> None: ... _AttachmentContent: TypeAlias = bytes | EmailMessage | Message | SafeMIMEText | str -# switch to tuple once https://github.com/python/mypy/issues/11098 is fixed -# remove Optional once python 3.7 is dropped (Tuple[str | None, ...] works with mypy on py3.10) _AttachmentTuple: TypeAlias = ( - Tuple[str, _AttachmentContent] - | Tuple[Optional[str], _AttachmentContent, str] - | Tuple[str, _AttachmentContent, None] + tuple[str, _AttachmentContent] | tuple[str | None, _AttachmentContent, str] | tuple[str, _AttachmentContent, None] ) class EmailMessage: diff --git a/django_stubs_ext/setup.py b/django_stubs_ext/setup.py index b2e037e50..f773b2883 100644 --- a/django_stubs_ext/setup.py +++ b/django_stubs_ext/setup.py @@ -25,14 +25,13 @@ maintainer="Marti Raudsepp", maintainer_email="marti@juffo.org", py_modules=[], - python_requires=">=3.7", + python_requires=">=3.8", install_requires=dependencies, packages=["django_stubs_ext", *find_packages(exclude=["scripts"])], package_data={"django_stubs_ext": ["py.typed"]}, classifiers=[ "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", - "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", diff --git a/setup.py b/setup.py index 66e1a8404..6e1096d81 100644 --- a/setup.py +++ b/setup.py @@ -47,7 +47,7 @@ def find_stub_files(name: str) -> List[str]: maintainer="Marti Raudsepp", maintainer_email="marti@juffo.org", py_modules=[], - python_requires=">=3.7", + python_requires=">=3.8", install_requires=dependencies, extras_require=extras_require, packages=["django-stubs", *find_packages(exclude=["scripts"])], @@ -58,7 +58,6 @@ def find_stub_files(name: str) -> List[str]: classifiers=[ "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", - "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", From 564ed2a0a201b1ba6db65d39f5a46368d768540e Mon Sep 17 00:00:00 2001 From: Marti Raudsepp Date: Sat, 29 Apr 2023 21:28:14 +0300 Subject: [PATCH 2/3] Update pyupgrade flag in .pre-commit-config.yaml --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f8978332f..cdb940de3 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -16,7 +16,7 @@ repos: rev: v3.3.1 hooks: - id: pyupgrade - args: ["--py37-plus"] + args: ["--py38-plus"] - repo: https://github.com/pycqa/isort rev: 5.12.0 hooks: From 025eec11a5b4d1ae2efb5f8727439029ce1d61af Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 29 Apr 2023 18:35:13 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks --- django_stubs_ext/django_stubs_ext/db/models.py | 3 +-- django_stubs_ext/django_stubs_ext/types.py | 4 +--- django_stubs_ext/tests/test_monkeypatching.py | 3 +-- mypy_django_plugin/django/context.py | 3 +-- mypy_django_plugin/transformers/managers.py | 3 +-- 5 files changed, 5 insertions(+), 11 deletions(-) diff --git a/django_stubs_ext/django_stubs_ext/db/models.py b/django_stubs_ext/django_stubs_ext/db/models.py index 1efeae7af..172cd3055 100644 --- a/django_stubs_ext/django_stubs_ext/db/models.py +++ b/django_stubs_ext/django_stubs_ext/db/models.py @@ -1,10 +1,9 @@ from typing import TYPE_CHECKING if TYPE_CHECKING: - from typing import ClassVar, List, Sequence, Tuple, Union + from typing import ClassVar, List, Literal, Sequence, Tuple, Union from django.db.models import BaseConstraint, Index - from typing_extensions import Literal from django_stubs_ext import StrOrPromise diff --git a/django_stubs_ext/django_stubs_ext/types.py b/django_stubs_ext/django_stubs_ext/types.py index da321a46d..8c60066c9 100644 --- a/django_stubs_ext/django_stubs_ext/types.py +++ b/django_stubs_ext/django_stubs_ext/types.py @@ -1,6 +1,4 @@ -from typing import Any - -from typing_extensions import Protocol +from typing import Any, Protocol # Used internally by mypy_django_plugin. diff --git a/django_stubs_ext/tests/test_monkeypatching.py b/django_stubs_ext/tests/test_monkeypatching.py index 3eb1c59d8..2ba12ecf9 100644 --- a/django_stubs_ext/tests/test_monkeypatching.py +++ b/django_stubs_ext/tests/test_monkeypatching.py @@ -1,13 +1,12 @@ import builtins from contextlib import suppress -from typing import Iterable, List, Optional +from typing import Iterable, List, Optional, Protocol import pytest from _pytest.fixtures import FixtureRequest from _pytest.monkeypatch import MonkeyPatch from django.db.models import Model from django.forms.models import ModelForm -from typing_extensions import Protocol import django_stubs_ext from django_stubs_ext import patch diff --git a/mypy_django_plugin/django/context.py b/mypy_django_plugin/django/context.py index 2ddddd9c1..1cd5eb700 100644 --- a/mypy_django_plugin/django/context.py +++ b/mypy_django_plugin/django/context.py @@ -2,7 +2,7 @@ import sys from collections import defaultdict from contextlib import contextmanager -from typing import TYPE_CHECKING, Any, Dict, Iterable, Iterator, Optional, Sequence, Set, Tuple, Type, Union +from typing import TYPE_CHECKING, Any, Dict, Iterable, Iterator, Literal, Optional, Sequence, Set, Tuple, Type, Union from django.core.exceptions import FieldDoesNotExist, FieldError from django.db import models @@ -20,7 +20,6 @@ from mypy.types import AnyType, Instance from mypy.types import Type as MypyType from mypy.types import TypeOfAny, UnionType -from typing_extensions import Literal from mypy_django_plugin.lib import fullnames, helpers from mypy_django_plugin.lib.fullnames import WITH_ANNOTATIONS_FULLNAME diff --git a/mypy_django_plugin/transformers/managers.py b/mypy_django_plugin/transformers/managers.py index 19e176a25..eeca04751 100644 --- a/mypy_django_plugin/transformers/managers.py +++ b/mypy_django_plugin/transformers/managers.py @@ -1,4 +1,4 @@ -from typing import Optional, Union +from typing import Final, Optional, Union from mypy.checker import TypeChecker from mypy.nodes import ( @@ -22,7 +22,6 @@ from mypy.types import Type as MypyType from mypy.types import TypeOfAny from mypy.typevars import fill_typevars -from typing_extensions import Final from mypy_django_plugin.lib import fullnames, helpers