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

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

Open
strokirk opened this issue Feb 11, 2025 · 0 comments
Open

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

strokirk opened this issue Feb 11, 2025 · 0 comments

Comments

@strokirk
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant