Fix FilterSchema list parsing for PEP 695 type aliases (#1720)#1734
Open
Vansh-Sharma27 wants to merge 1 commit into
Open
Fix FilterSchema list parsing for PEP 695 type aliases (#1720)#1734Vansh-Sharma27 wants to merge 1 commit into
Vansh-Sharma27 wants to merge 1 commit into
Conversation
A `type X = list[...]` alias used in a FilterSchema field was not unwrapped, so collection detection treated the field as a scalar and query parsing failed with "Input should be a valid list". A FilterLookup defined inside the alias was also dropped because pydantic does not surface the alias metadata. Unwrap TypeAliasType (and the Annotated it may contain) in is_collection_type, and read FilterLookup from the alias annotation directly in _get_filter_lookup. Fixes vitalik#1720
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
Fixes #1720.
When a PEP 695
typealias for alist[...]type is used in aFilterSchemafield, the query parameter fails to parse:{"detail": [{"type": "list_type", "loc": ["query", "name"], "msg": "Input should be a valid list"}]}Minimal repro from the issue:
Root cause
type X = ...produces atyping.TypeAliasTypewhose real annotation lives in__value__, andget_origin()on it returnsNone. Pydantic stores the alias verbatim as the field annotation, so:is_collection_type()inninja/signature/details.pynever sees the underlyinglist, so the field is not registered as a collection and repeated query params are not collected into a list.FilterSchema._get_filter_lookup()readsfield_info.metadata, which is empty for an aliased field, so aFilterLookupdefined inside the alias was silently dropped.Point 2 matters: fixing only the parsing turns the loud 422 into a silently wrong lookup (
nameinstead ofname__in), so both paths needed handling.Changes
TYPE_ALIAS_TYPEStoninja/compatibility/util.py(covers both the stdlib 3.12+ class and thetyping_extensionsbackport).is_collection_type()now unwrapsTypeAliasType(and a containedAnnotated) before checking the origin._get_filter_lookup()now also readsFilterLookupmetadata from inside the alias.Non-list aliases and plain
Annotatedfields are unchanged.Tests
Added three tests to
tests/test_filter_schema.py:test_filter_lookup_from_list_type_alias— the innerFilterLookupis applied.test_list_type_alias_query_parsing— repeated query params parse into a list (this is the exact 422 from the issue).test_non_list_type_alias_still_works— regression guard for scalar aliases.The tests build the alias via
TypeAliasType(...)(the same runtime object thetypestatement creates) so the file still imports on Python < 3.12. I confirmed the two bug-specific tests fail onmasterand pass with the fix.pytest,ruff format --check,ruff check, andmypy ninjaall pass.