-
-
Notifications
You must be signed in to change notification settings - Fork 553
Open
Labels
bugSomething isn't workingSomething isn't working
Description
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
objargument passed toInlineModelAdminmethods is the parent object being edited orNonewhen 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
pythonversion: 3.13.5djangoversion: 6.0.3mypyversion: 1.19.1django-stubsversion: 6.0.1django-stubs-extversion: 6.0.1
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working