Skip to content

BaseModelAdmin should be a generic class #482

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

Closed
fancysnake opened this issue Oct 10, 2020 · 4 comments · Fixed by #504
Closed

BaseModelAdmin should be a generic class #482

fancysnake opened this issue Oct 10, 2020 · 4 comments · Fixed by #504
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@fancysnake
Copy link

Bug report

What's wrong

I have a class inheriting from BaseModelAdmin and a method delete_model defined as such:

def delete_model(self, request: HttpRequest, obj: AgendaItem) -> None:

AgendaItem is a Django model

However, mypy raises an error:

app/chronology/admin.py: note: In member "delete_model" of class "AgendaItemAdmin":
app/chronology/admin.py:376: error: Argument 2 of "delete_model" is
incompatible with supertype "ModelAdmin"; supertype defines the argument type as
"Model"  [override]
        def delete_model(self, request: HttpRequest, obj: AgendaItem) -> N...
        ^

because in stubs this function is defined as:

def delete_model(self, request: HttpRequest, obj: Model) -> None: ...

How is that should be

The solution is to make BaseModelAdmin a generic class with model type variable.

System information

  • OS: ArchLinux
  • python version: 3.8.6
  • django version: 3.1.2
  • mypy version: 0.782
  • django-stubs version: 1.6.0
@sobolevn
Copy link
Member

@radowit do you want to work on this?

@vgel
Copy link

vgel commented Oct 12, 2020

I can take a shot at this!

@sobolevn
Copy link
Member

@vgel awesome! Please, feel free to do so!

@vgel
Copy link

vgel commented Oct 21, 2020 via email

sobolevn pushed a commit that referenced this issue Oct 27, 2020
…els (#504)

* make BaseModelAdmin generic to properly type the `obj` argument of ModelAdmin.delete_model
closes #482

* turn BaseModelAdmin into bound generic, run black

* add test for generic ModelAdmin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Development

Successfully merging a pull request may close this issue.

3 participants