Skip to content

Use of TypedDict in ListFilter.choices makes it hard to subclass #2508

Open
@strokirk

Description

@strokirk

Hello!

In #2004, the return type of ListFilter.choices was changed to use a private TypedDict. However, since TypedDict is not a subclass of dict, this introduces extra friction when implementing a new SimpleListFilter. For example, this example will fail since dict cannot be used in place of _ListFilterChoices:

class ExampleFilter(SimpleListFilter):
    ...

    def choices(self, changelist: ChangeList) -> Iterator[dict]:
        for lookup, title in self.lookup_choices:
            yield {
                "selected": self.value() == lookup,
                "query_string": changelist.get_query_string(
                    {self.parameter_name: lookup}
                ),
                "display": title.upper(),
            }

As a workaround, the application developer can of course either duplicate _ListFilterChoices locally, or use Any, but either isn't very ergonomic to my eyes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions