Skip to content

admin.TabularInline: Signature of "has_add_permission" incompatible with supertype "BaseModelAdmin" #469

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

Open
JoaRiski opened this issue Sep 23, 2020 · 5 comments · May be fixed by #470
Open
Labels
bug Something isn't working

Comments

@JoaRiski
Copy link

JoaRiski commented Sep 23, 2020

What's wrong

The signature for the admin.TabularInline.has_add_permission method seems to be wrongly typed, resulting in the following error when a subclass declares a type according to the Django documentation:

admin.TabularInline: Signature of "has_add_permission" incompatible with supertype "BaseModelAdmin"

Example signature resulting in this error:

    def has_add_permission(self, request: HttpRequest, obj: Model) -> bool:
        return False

The problem seems to be caused by the TabluarInline directly inheriting the types from here

def has_add_permission(self, request: HttpRequest) -> bool: ...

When in reality the InlineModelAdmin has a different signature for the has_add_permission function:
https://docs.djangoproject.com/en/3.1/ref/contrib/admin/#django.contrib.admin.InlineModelAdmin.has_add_permission

How is that should be

The has_add_permission should be typed specifically for the InlineModelAdmin class rather than inheriting from BaseModelAdmin.

It would be a good idea to verify other has_x_permission methods at the same time, as the inline admin functions might differ from the base model admin

System information

  • OS: Linux
  • python version: 3.8.5
  • django version: 3.1.1
  • mypy version: 0.770
  • django-stubs version: 1.5.0
@JoaRiski JoaRiski added the bug Something isn't working label Sep 23, 2020
@JoaRiski
Copy link
Author

Loosely related to #203

@sobolevn
Copy link
Member

@JoaRiski PRs are appreciated!

JoaRiski added a commit to JoaRiski/django-stubs that referenced this issue Sep 23, 2020
Add a proper definition for the InlineModelAdmin's has_add_permission method.

Fixes typeddjango#469
@JoaRiski JoaRiski linked a pull request Sep 23, 2020 that will close this issue
@JoaRiski
Copy link
Author

@sobolevn I don't have the time to properly test it, but opened #470

JoaRiski added a commit to JoaRiski/django-stubs that referenced this issue Nov 28, 2021
Improve how `has_add_permission` is declared by removing it from `BaseModelAdmin` and adding it directly to `ModelAdmin`. This is required as `InlineModelAdmin` has a different signature, see typeddjango#469
@lachlancannon
Copy link

I think the types are wrong in addition to this. If we look at the Django docs https://docs.djangoproject.com/en/4.0/ref/contrib/admin/#django.contrib.admin.InlineModelAdmin.has_add_permission the inlines are defined as being passed the parent object, not the current object, but the types generated by django-stubs complain if I don't specify the type as InlineType | None

e.g.

class ScheduledChangeLogFieldInline(admin.TabularInline[ScheduledChangeLogField]):      
    model = ScheduledChangeLogField                                                   
    fields = ["field_name", "old_value", "new_value"]                                 
                                                                                      
    def has_change_permission(                                                           
        self, request: HttpRequest, parent: ScheduledChangeLogField | None = None      
    ) -> bool:                                                                                     
        return False

If I change parent to ScheduledChangeLog, which it should be, I get:

base/admin.py:106: error: Argument 2 of "has_change_permission" is incompatible with supertype "BaseModelAdmin"; supertype defines the argument type as "Optional[ScheduledChangeLogField]"  [override]

The same applies to has_delete_permission incidentally.

@bobwhitelock
Copy link
Contributor

Is this the same issue as #874, and if so was this also fixed by #909?

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

Successfully merging a pull request may close this issue.

4 participants