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

Return from get_deleted_objects-method of ModelAdmin #1824

Closed
golgor opened this issue Nov 7, 2023 · 0 comments · Fixed by #1825
Closed

Return from get_deleted_objects-method of ModelAdmin #1824

golgor opened this issue Nov 7, 2023 · 0 comments · Fixed by #1825
Labels
bug Something isn't working stubs Issues in stubs files (.pyi)

Comments

@golgor
Copy link
Contributor

golgor commented Nov 7, 2023

Bug report

What's wrong

When we delete a certain object, we also want to delete some related objects and we want this to be visible in the Delete Confirmation View.

I got some typing issues regarding an override I did for get_deleted_objects(self, objs, request) in a subclass to admin.ModelAdmin.

Looking at the Django documentation for this method it is said to return tuple[list[str], dict[str, int], set[str], list[str]].

I also verified this in a debug session where the deleted_objects value was: 'Parameter set: <a href="/admin/onboard/parameterset/341/change/">341</a>'.

However, in django_stubs.contrib.admin.options.ModelAdmin.get_deleted_objects I see the the deleted_objects is typed as list[Model]. The full return type is specified as tuple[list[Model], dict[str, int], set[str], list[str]].

I rely on this being a list[str] as I append strings to this list using django.utils.html.format_html, which fails with the current definition using a list[Model].

Just some example code to easier understand the issue (Not verified if this exact code raises the issue):

class TestAdmin(admin.ModelAdmin):
    def get_deleted_objects(self, objs, request) -> tuple[list[str], dict[str, int], set[str], list[str]]:
        deleted_objects, model_count, perms_needed, protected = super().get_deleted_objects(objs, request)
        for obj in objs:
            deleted_objects.append(
                    format_html(
                        HTML_TEMPLATE,
                        obj.titleid,
                        obj.titleid,
                    )
            )
    return deleted_objects, model_count, perms_needed, protected

This would return a mypy-error:

Return type "tuple[list[str], dict[str, int], set[str], list[str]]" of "get_deleted_objects" incompatible with return type "tuple[list[Model], dict[str, int], set[str], list[str]]" in supertype "ModelAdmin"

Just changing it to list[Model] doesn't work in this case, as the I get:

Argument 1 to "append" of "list" has incompatible type "SafeString"; expected "Model"

How is that should be

The return type of admin.ModelAdmin.get_deleted_objects should be changed from:

def get_deleted_objects(
    self, objs: Sequence[_ModelT] | QuerySet[_ModelT], request: HttpRequest
) -> tuple[list[Model], dict[str, int], set[str], list[str]]: ...
def get_deleted_objects(
    self, objs: Sequence[_ModelT] | QuerySet[_ModelT], request: HttpRequest
) -> tuple[list[str], dict[str, int], set[str], list[str]]: ...

System information

  • OS: Fedora Linux 38 (KDE Plasma) @ 6.5.9-200.fc38.x86_64 (64-bit)
  • python version: 3.11.6
  • django version: 4.2.7
  • mypy version: 1.6.1
  • django-stubs version: 4.2.6
  • django-stubs-ext version: 4.2.5
@golgor golgor added the bug Something isn't working label Nov 7, 2023
@intgr intgr added the stubs Issues in stubs files (.pyi) label Nov 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working stubs Issues in stubs files (.pyi)
Development

Successfully merging a pull request may close this issue.

2 participants