Skip to content

Fix used_parameters and params types in admin filter stubs#3187

Open
alessio-b2c2 wants to merge 1 commit intotypeddjango:masterfrom
alessio-b2c2:fix/field-list-filter-used-parameters-type
Open

Fix used_parameters and params types in admin filter stubs#3187
alessio-b2c2 wants to merge 1 commit intotypeddjango:masterfrom
alessio-b2c2:fix/field-list-filter-used-parameters-type

Conversation

@alessio-b2c2
Copy link

@alessio-b2c2 alessio-b2c2 commented Mar 13, 2026

Summary

The params argument passed to ListFilter.__init__ and FieldListFilter.__init__ was typed as dict[str, str], but at runtime ChangeList.get_filters builds it from request.GET.lists(), so values are always list[str].

This also means used_parameters — which is populated via prepare_lookup_value() — was incorrectly typed as dict[str, bool | datetime | str]. Since prepare_lookup_value processes all elements in a list through the same key-based branch (__isnull → all bool, otherwise all str), the correct type is dict[str, list[str] | list[bool]].

SimpleListFilter is a special case: it extracts a scalar with value[-1], so its used_parameters is dict[str, str]. This breaks the base class contract and requires a type: ignore[assignment].

While investigating, we also noticed that BooleanFieldListFilter.__init__ checks self.used_parameters[key] in ("1", "0") after super().__init__(). Since values are now list[str] (not scalar str), this comparison is always False — likely a Django bug introduced when filter_params switched from request.GET.items() to request.GET.lists().

Finally, prepare_lookup_value now has proper overloads for list[str] vs str input, and the incorrect datetime was removed from its signature (it never produces datetime values).

Test plan

  • All existing tests pass
  • Verified runtime behavior by reading Django source (ChangeList.get_filters, FieldListFilter.__init__, SimpleListFilter.__init__, prepare_lookup_value, build_q_object_from_lookup_parameters)
  • Confirmed mypy and pyright produce no new errors

🤖 Generated with Claude Code

At runtime, `ChangeList.get_filters` passes `filter_params` (from
`request.GET.lists()`) to filter constructors, so `params` values are
`list[str]`, not `str`. Similarly, `FieldListFilter.__init__` populates
`used_parameters` via `prepare_lookup_value`.

Changes:
- `ListFilter.__init__` and `FieldListFilter.__init__`/`.create()`:
  `params` type `dict[str, str]` → `dict[str, list[str]]`
- `ListFilter.used_parameters`:
  `dict[str, bool | datetime | str]` → `dict[str, list[str] | list[bool]]`
- `SimpleListFilter.used_parameters`: narrowed to `dict[str, str]` with
  type: ignore since it breaks the base class contract by storing scalars
- `prepare_lookup_value`: add overloads for `list[str]` and `str` input,
  remove incorrect `datetime` from signature
- Remove unused `import datetime` from `utils.pyi`

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@alessio-b2c2 alessio-b2c2 force-pushed the fix/field-list-filter-used-parameters-type branch from 036a32c to 1cf58e7 Compare March 13, 2026 08:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant