From 00ff825b8c29099294deecdd7539b0a1276690d3 Mon Sep 17 00:00:00 2001 From: Dalton Rardin <8892319+dalrrard@users.noreply.github.com> Date: Sat, 9 Oct 2021 01:09:46 -0500 Subject: [PATCH] Add djangorestframework-stubs Add djangorestframework-stubs to local.txt requirements Modify setup.cfg to add stubs to mypy settings Fix new type error in DRF `./users/api/views.py:19: error: Incompatible type for lookup 'id': (got "Union[AutoField[Union[Combinable, int, str], int], Any]", expected "Union[str, int]")` by asserting that `self.request.user.id` is an int --- {{cookiecutter.project_slug}}/requirements/local.txt | 3 +++ {{cookiecutter.project_slug}}/setup.cfg | 2 +- .../{{cookiecutter.project_slug}}/users/api/views.py | 1 + 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/{{cookiecutter.project_slug}}/requirements/local.txt b/{{cookiecutter.project_slug}}/requirements/local.txt index 5632f5b2..f0c8424c 100644 --- a/{{cookiecutter.project_slug}}/requirements/local.txt +++ b/{{cookiecutter.project_slug}}/requirements/local.txt @@ -17,6 +17,9 @@ mypy==0.910 # https://github.com/python/mypy django-stubs==1.8.0 # https://github.com/typeddjango/django-stubs pytest==6.2.5 # https://github.com/pytest-dev/pytest pytest-sugar==0.9.4 # https://github.com/Frozenball/pytest-sugar +{%- if cookiecutter.use_drf == "y" %} +djangorestframework-stubs==1.4.0 # https://github.com/typeddjango/djangorestframework-stubs +{%- endif %} # Documentation # ------------------------------------------------------------------------------ diff --git a/{{cookiecutter.project_slug}}/setup.cfg b/{{cookiecutter.project_slug}}/setup.cfg index d0ba6387..dad64e1f 100644 --- a/{{cookiecutter.project_slug}}/setup.cfg +++ b/{{cookiecutter.project_slug}}/setup.cfg @@ -24,7 +24,7 @@ ignore_missing_imports = True warn_unused_ignores = True warn_redundant_casts = True warn_unused_configs = True -plugins = mypy_django_plugin.main +plugins = mypy_django_plugin.main{% if cookiecutter.use_drf == "y" %}, mypy_drf_plugin.main{% endif %} [mypy.plugins.django-stubs] django_settings_module = config.settings.test diff --git a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/api/views.py b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/api/views.py index 288ea7ab..3d56cf56 100644 --- a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/api/views.py +++ b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/api/views.py @@ -16,6 +16,7 @@ class UserViewSet(RetrieveModelMixin, ListModelMixin, UpdateModelMixin, GenericV lookup_field = "username" def get_queryset(self, *args, **kwargs): + assert isinstance(self.request.user.id, int) return self.queryset.filter(id=self.request.user.id) @action(detail=False, methods=["GET"])