Skip to content

Commit

Permalink
Completely drop Python 3.7 compatibility (#1459)
Browse files Browse the repository at this point in the history
* 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
* Update pyupgrade flag in .pre-commit-config.yaml
* [pre-commit.ci] auto fixes from pre-commit.com hooks

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
intgr and pre-commit-ci[bot] authored Apr 29, 2023
1 parent e60f391 commit 4d10414
Show file tree
Hide file tree
Showing 10 changed files with 10 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 2 additions & 9 deletions django-stubs/core/mail/message.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 1 addition & 2 deletions django_stubs_ext/django_stubs_ext/db/models.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
4 changes: 1 addition & 3 deletions django_stubs_ext/django_stubs_ext/types.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from typing import Any

from typing_extensions import Protocol
from typing import Any, Protocol


# Used internally by mypy_django_plugin.
Expand Down
3 changes: 1 addition & 2 deletions django_stubs_ext/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@
maintainer="Marti Raudsepp",
maintainer_email="[email protected]",
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",
Expand Down
3 changes: 1 addition & 2 deletions django_stubs_ext/tests/test_monkeypatching.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 1 addition & 2 deletions mypy_django_plugin/django/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions mypy_django_plugin/transformers/managers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional, Union
from typing import Final, Optional, Union

from mypy.checker import TypeChecker
from mypy.nodes import (
Expand All @@ -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

Expand Down
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def find_stub_files(name: str) -> List[str]:
maintainer="Marti Raudsepp",
maintainer_email="[email protected]",
py_modules=[],
python_requires=">=3.7",
python_requires=">=3.8",
install_requires=dependencies,
extras_require=extras_require,
packages=["django-stubs", *find_packages(exclude=["scripts"])],
Expand All @@ -60,7 +60,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",
Expand Down

0 comments on commit 4d10414

Please sign in to comment.