Fix used_parameters and params types in admin filter stubs#3187
Open
alessio-b2c2 wants to merge 1 commit intotypeddjango:masterfrom
Open
Fix used_parameters and params types in admin filter stubs#3187alessio-b2c2 wants to merge 1 commit intotypeddjango:masterfrom
alessio-b2c2 wants to merge 1 commit intotypeddjango:masterfrom
Conversation
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>
036a32c to
1cf58e7
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The
paramsargument passed toListFilter.__init__andFieldListFilter.__init__was typed asdict[str, str], but at runtimeChangeList.get_filtersbuilds it fromrequest.GET.lists(), so values are alwayslist[str].This also means
used_parameters— which is populated viaprepare_lookup_value()— was incorrectly typed asdict[str, bool | datetime | str]. Sinceprepare_lookup_valueprocesses all elements in a list through the same key-based branch (__isnull→ allbool, otherwise allstr), the correct type isdict[str, list[str] | list[bool]].SimpleListFilteris a special case: it extracts a scalar withvalue[-1], so itsused_parametersisdict[str, str]. This breaks the base class contract and requires atype: ignore[assignment].While investigating, we also noticed that
BooleanFieldListFilter.__init__checksself.used_parameters[key] in ("1", "0")aftersuper().__init__(). Since values are nowlist[str](not scalarstr), this comparison is alwaysFalse— likely a Django bug introduced whenfilter_paramsswitched fromrequest.GET.items()torequest.GET.lists().Finally,
prepare_lookup_valuenow has proper overloads forlist[str]vsstrinput, and the incorrectdatetimewas removed from its signature (it never produces datetime values).Test plan
ChangeList.get_filters,FieldListFilter.__init__,SimpleListFilter.__init__,prepare_lookup_value,build_q_object_from_lookup_parameters)🤖 Generated with Claude Code