Using multiple dependencies that have the same function arguments, like in litestar-fullstack's provide_created_filter and provide_updated_filter filter dependencies, and passing in one of those arguments as query params causes overrides in seemingly unexpected ways.
For example, if you have the following dependencies:
from dataclasses import dataclass
@dataclass
class FilterA:
field: str | None
value: str | None
@dataclass
class FilterB:
field: str | None
value: str | None
def provide_filter_a_filter(
field: str | None = Parameter(query='fieldA', default='a', required=False),
value: str | None = Parameter(query='valueA', default=None, required=False),
) -> FilterA:
return FilterA(field=field, value=value)
def provide_filter_b_filter(
field: str | None = Parameter(query='fieldB', default='b', required=False),
value: str | None = Parameter(query='valueB', default=None, required=False),
) -> FilterB:
return FilterB(field=field, value=value)
def provide_filter_dependencies(filter_a: FilterA, filter_b: FilterB) -> list[FilterA | FilterB]:
return [filter_a, filter_b]
FilterTypes = FilterA | FilterB
and use them in a route handler like
@get('')
async def r(filters: list[FilterA | FilterB] = Dependency(skip_validation=True)) -> list[dict]:
return [asdict(f) for f in filters]
then pass in the query parameters {'valueA': 'a', 'valueB': 'b'}
, the filters will be set to FilterA(field='b', value='b')
and FilterB(field='b', value='b')
. The values here could change, as noted in the MCVE test code. The field
and value
values in the filters can be mix and matched, but always duplicated in some way between them (so field=a field=a, value=b, value=b
, field=b field=b value=a value=a
, etc.).
Using the query
argument in Parameter
in dependency function arguments doesn't seem to help, though it seems like maybe it should, as this requires you to pass in the query param via the query
value used, so it should make them "unique". This would allow you to keep the function arguments duplicated across dependencies (which seems to be behavior that makes sense, since the dependency functions shouldn't have to care/know what other function arguments are set to).
Default values when query params aren't passed in also have this behavior (as you can see in the tests). For example, in the test_duplicated_dependency_arguments__only_passing_values__default_field_values
test, field
isn't passed in as a query param, and both field
values have a default value of 'a'
and 'b'
, respectively. When the test is run, you'll see that both field
values will be set to the same value ('a'
or 'b'
, but never separate, as defined in their function). If you run the tests multiple times, you'll see that the order isn't deterministic. In some instances, for example, the field
value will be 'a'
for both, and others it'll be 'b'
.
I haven't tested this, but I assume this only happens when you're using dependencies in the way that's being done here, where like filters (e.g. BeforeAfter
) are grouped in a list. I'm not sure if a bug ticket is correct here, or if it should be an enhancement request for Litestar to warn you when you don't have unique names for dependency arguments (maybe only when grouped in a list, if that's possible and actually the root cause here).
https://github.com/rseeley/litestar-mcves/blob/mcve/filter-names/tests/test_filters.py
No response
1. Install dependencies (including the `test` dev dep)
2. Run `pytest` to see which tests fail
No response
No response
2.2.1
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too