Skip to content

Signature of InlineModelAdmin.get_readonly_fields is incompatible with supertype BaseModelAdmin #3224

@pai-danielkleinridder

Description

@pai-danielkleinridder

Bug report

What's wrong

The signature of the method get_readonly_fields for InlineModelAdmin has the wrong type for its second parameter obj.

The django documentation also has the following note

The obj argument passed to InlineModelAdmin methods is the parent object being edited or None when adding a new parent.

In this example I set the correct type for the parameter obj:

class BookInline(admin.StackedInline[Book, Author]):
    model = Book

    def get_readonly_fields(self, request: HttpRequest, obj: Author | None = None):
        return super().get_readonly_fields(request, obj)

Running mypy give this error:

app/admin.py:10: error: Argument 2 of "get_readonly_fields" is incompatible with supertype "django.contrib.admin.options.BaseModelAdmin"; supertype defines the argument type as "Book | None"  [override]
app/admin.py:10: note: This violates the Liskov substitution principle
app/admin.py:10: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
app/admin.py:12: error: Argument 2 to "get_readonly_fields" of "BaseModelAdmin" has incompatible type "Author | None"; expected "Book | None"  [arg-type]

This is also related to #469

How is that should be

The obj parameter of methods for InlineModelAdmin should have the type of the parent object instead of the child object.

System information

  • OS: MacOS 26.4
  • python version: 3.13.5
  • django version: 6.0.3
  • mypy version: 1.19.1
  • django-stubs version: 6.0.1
  • django-stubs-ext version: 6.0.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions