Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completely drop Python 3.7 compatibility #1459

Merged
merged 4 commits into from
Apr 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]
Copy link
Collaborator Author

@intgr intgr Apr 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hrm? These comments were referencing Python 3.7, but I'm not sure whether this has anything to do with Python 3.7.

str | None or tuple[str] don't work on Python 3.8 runtime either.

But AFAIK it doesn't matter since .pyi files are only evaluated by mypy, not Python itself. Maybe I'm missing something.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These files are parsed by python, but not executed. I don't remember the origin of this comment, but I am happy that it is removed now :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These files are parsed by python, but not executed.

Genuinely interested, when does Python parse .pyi files?

Is this somehow related to the ast module (which used to be typed_ast but got merged into Python 3.8's ast)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, exactly. mypy uses ast.parse to get the Python's AST and then mypy builds its own AST around it.

)

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