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

ApiView + custom mixing = cannot specify the type of request #232

Open
sshishov opened this issue Jun 13, 2022 · 1 comment
Open

ApiView + custom mixing = cannot specify the type of request #232

sshishov opened this issue Jun 13, 2022 · 1 comment
Labels
bug Something isn't working

Comments

@sshishov
Copy link
Contributor

Bug report

For mixin you cannot specify the type of request as it got incompatible with base View

What's wrong

from rest_framework import mixins
from rest_framework import viewsets
from rest_framework.request import Request

class MyMixin:
    request: Request

    def some_method_with_request_usage(self, *args, **kwargs):
        print(self.request)


class MyViewSet(MyMixin, mixins.ListModelMixin, viewsets.GenericViewSet):
    pass

The output is:

myfile.py:11: error: Definition of "request" in base class "MyMixin" is incompatible with definition in base class "View"  [misc]

Workaround

As a workaround, you can add the type of the request to the MyViewSet class itself.

class MyViewSet(MyMixin, mixins.ListModelMixin, viewsets.GenericViewSet):
    request: Request

But try to imagine that this mixin is used in 100 different places, why we should duplicate the type which should be inferred from the base classes:

  • Mixin has the type (Request)
  • ApiView has the type (Request)

P.S. no need to look deeper, inside View

How is that should be

No errors should be reported.

System information

  • OS: MacOS 12.3.1
  • python version: 3.9.13
  • django version: 3.2.13
  • mypy version: v0.960
  • django-stubs version: 1.11.0
@sshishov sshishov added the bug Something isn't working label Jun 13, 2022
@macieyng
Copy link

Hi @sobolevn @intgr 👋 Is this bug anywhere on your roadmap? Do you have a suggestion how to approach this issue? Are you willing to accept a PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

No branches or pull requests

2 participants