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

Add django-stubs v1.10.0 compatibility #203

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.6', '3.7', '3.8', '3.9']
python-version: ['3.7', '3.8', '3.9']
steps:
- uses: actions/checkout@v2
- name: Setup system dependencies
Expand All @@ -52,7 +52,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.6', '3.7', '3.8', '3.9']
python-version: ['3.7', '3.8', '3.9']
steps:
- uses: actions/checkout@v2
- name: Setup system dependencies
Expand Down
2 changes: 2 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ repos:
rev: 21.12b0
henribru marked this conversation as resolved.
Show resolved Hide resolved
hooks:
- id: black
# https://github.com/psf/black/issues/2964#issuecomment-1080974737
additional_dependencies: ['click==8.0.4']
- repo: https://gitlab.com/pycqa/flake8
rev: 3.9.2
hooks:
Expand Down
4 changes: 2 additions & 2 deletions mypy_drf_plugin/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from mypy.nodes import TypeInfo
from mypy.options import Options
from mypy.plugin import ClassDefContext, Plugin
from mypy_django_plugin import main as mypy_django_main
from mypy_django_plugin.config import DjangoPluginConfig
from mypy_django_plugin.django.context import DjangoContext

from mypy_drf_plugin.lib import fullnames, helpers
Expand All @@ -22,7 +22,7 @@ class NewSemanalDRFPlugin(Plugin):
def __init__(self, options: Options) -> None:
super().__init__(options)

django_settings_module = mypy_django_main.extract_django_settings_module(options.config_file)
django_settings_module = DjangoPluginConfig(options.config_file).django_settings_module
self.django_context = DjangoContext(django_settings_module)

def _get_currently_defined_serializers(self) -> Dict[str, int]:
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
wheel
gitpython==3.1.9
pre-commit==2.7.1
pre-commit==2.9.2
pytest==6.1.1
pytest-mypy-plugins==1.9.1
djangorestframework==3.12.1
Expand Down
3 changes: 2 additions & 1 deletion rest_framework-stubs/test.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ from django.test.client import RequestFactory as DjangoRequestFactory
from rest_framework.authtoken.models import Token
from rest_framework.request import Request
from rest_framework.response import Response
import urllib3

def force_authenticate(
request: HttpRequest, user: Optional[Union[AnonymousUser, AbstractBaseUser]] = ..., token: Optional[Token] = ...
) -> None: ...

class HeaderDict(requests.packages.urllib3._collections.HTTPHeaderDict):
class HeaderDict(urllib3._collections.HTTPHeaderDict):
def get_all(self, key: Any, default: Any): ...

class MockOriginalResponse:
Expand Down
13 changes: 12 additions & 1 deletion scripts/typecheck_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
' base class "BaseTokenAuthTests" defined the type as "None"',
'"None" has no attribute "objects"',
'"BaseTokenAuthTests" has no attribute "assertNumQueries"',
'Module "django.middleware.csrf" has no attribute "_mask_cipher_secret"; maybe "_salt_cipher_secret"?',
"All conditional function variants must have identical signatures",
],
"schemas": [
'(expression has type "CharField", base class "Field" defined the type as "bool")',
Expand All @@ -78,6 +80,9 @@
],
"conftest.py": ["Unsupported operand types for"],
"models.py": ['"ForeignKeyTarget" has no attribute "sources"'],
"serializers.pyi": [
'note: "IntegerSerializer" defined here',
],
"test_authtoken.py": [
'Item "None" of "Optional[Token]" has no attribute "key"',
'Argument 1 to "get_fields" of "BaseModelAdmin" has incompatible type "object"; expected "HttpRequest"',
Expand Down Expand Up @@ -143,6 +148,7 @@
"test_permissions.py": [
'"ResolverMatch" has incompatible type "str"; expected "Callable[..., Any]"',
"_SupportsHasPermission",
"Invalid type alias: expression is not a valid type",
],
"test_relations.py": [
'Invalid index type "int" for "Union[str, List[Any], Dict[str, Any]]"; expected type "str"',
Expand Down Expand Up @@ -175,6 +181,8 @@
'"CharField" has incompatible type "Collection[Any]"',
'Name "foo" is not defined',
'Argument "data" has incompatible type "None"',
'Unsupported left operand type for | ("ReturnDict")',
'Unsupported left operand type for | ("Dict[str, str]")',
],
"test_serializer_bulk_update.py": [
'Argument "data" has incompatible type "int"',
Expand All @@ -184,6 +192,8 @@
"test_serializer_lists.py": [
'The type "Type[ListSerializer]" is not generic and not indexable',
'Name "foo" is not defined',
'Unexpected keyword argument "max_length" for "IntegerSerializer"',
'Unexpected keyword argument "min_length" for "IntegerSerializer"',
],
"test_serializer_nested.py": [
'(expression has type "NestedSerializer", base class "Field" defined the type as "bool")',
Expand Down Expand Up @@ -214,7 +224,8 @@
'Argument "detail" to "ValidationError" has incompatible type "Tuple[str, str]"; expected "Union[str, List[Any], Dict[str, Any], None]"', # noqa: E501
],
"test_validators.py": [
'has incompatible type "object"; expected "QuerySet[Any]"',
'Argument "queryset" to "BaseUniqueForValidator" has incompatible type "object";'
' expected "_QuerySet[Any, Any]"',
'to "filter_queryset" of "BaseUniqueForValidator" has incompatible type "None"',
],
"test_versioning.py": [
Expand Down
7 changes: 3 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ def find_stub_files(name):
readme = f.read()

dependencies = [
"mypy>=0.790",
"django-stubs>=1.7.0",
"mypy>=0.930,<0.950",
Copy link
Contributor

Choose a reason for hiding this comment

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

is this upper bound on mypy necessary? (and does it relate to the purpose of this MR?)

This project releases rather infrequently. So as a user I'd prefer that it did not pin me to a mypy version that will soon become stale.

If and when mypy release a breaking change, I can always pin it myself - but the recent evidence (per the current requirement without an upper bound) is that that's probably not needed.

Copy link
Member

Choose a reason for hiding this comment

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

Mypy can break its plugin API in new releases. We do the same in django-stubs

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, well if I'd known that I'd have asked you not to do it there too!

Perhaps you know something that I don't about mypy versioning, it seems mysterious to me. Is there any reason to suspect that 0.950 would be breaking but 0.949 would not?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

is this upper bound on mypy necessary? (and does it relate to the purpose of this MR?

I just set it to the same one as django-stubs as suggested in #203 (comment).

"django-stubs>=1.10.1",
"typing-extensions>=3.7.2",
"requests>=2.0.0",
"coreapi>=2.0.0",
Expand All @@ -43,11 +43,10 @@ def find_stub_files(name):
install_requires=dependencies,
packages=["rest_framework-stubs", *find_packages(exclude=["scripts"])],
package_data={"rest_framework-stubs": find_stub_files("rest_framework-stubs")},
python_requires=">=3.6",
python_requires=">=3.7",
classifiers=[
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
Expand Down
2 changes: 1 addition & 1 deletion tests/typecheck/test_serializers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from rest_framework import serializers

reveal_type(serializers.ModelSerializer.Meta.model) # N: Revealed type is "Type[_MT?]"
reveal_type(serializers.ModelSerializer.Meta.fields) # N: Revealed type is "typing.Sequence[builtins.str]"
reveal_type(serializers.ModelSerializer.Meta.fields) # N: Revealed type is "Union[typing.Sequence[builtins.str], Literal['__all__']]"
reveal_type(serializers.ModelSerializer.Meta.read_only_fields) # N: Revealed type is "Union[typing.Sequence[builtins.str], None]"
reveal_type(serializers.ModelSerializer.Meta.exclude) # N: Revealed type is "Union[typing.Sequence[builtins.str], None]"
reveal_type(serializers.ModelSerializer.Meta.depth) # N: Revealed type is "Union[builtins.int, None]"
Expand Down