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

Enhancement: Allow parameter of type Iterable to be processed as list #3631

Open
1 of 4 tasks
rafalkrupinski opened this issue Jul 16, 2024 · 3 comments · May be fixed by #3632
Open
1 of 4 tasks

Enhancement: Allow parameter of type Iterable to be processed as list #3631

rafalkrupinski opened this issue Jul 16, 2024 · 3 comments · May be fixed by #3632
Labels
Enhancement This is a new feature or request

Comments

@rafalkrupinski
Copy link
Contributor

rafalkrupinski commented Jul 16, 2024

Description

Litestar doesn't think it should process Iterable parameter as an array.

The original discussion was about default parameter value, but the problem is actually with how litestar handles Iterable itself

URL to code causing the issue

https://github.com/litestar-org/litestar/blob/8c4c15bb501879dabaecfbf0af541ac571c08cf3/litestar/_kwargs/parameter_definition.py#L67C9-L67C60

MCVE

@get('/sequence')
async def sequence(foo: Sequence[str]) -> Sequence[str]:
    return foo

@get('/iterable')
async def iterable(foo: Iterable[str]) -> Iterable[str]:
    return foo

app = Litestar(
    route_handlers=(
        sequence,
        iterable,
    )
)

Steps to reproduce

$ curl  'localhost:8000/sequence?foo=s1&foo=s2'
["s1","s2"]

$ curl 'localhost:8000/iterable?foo=s1&foo=s2'
s2

Screenshots

No response

Logs

No response

Litestar Version

2.9.1

Platform

  • Linux
  • Mac
  • Windows
  • Other (Please specify in the description above)

Note

While we are open for sponsoring on GitHub Sponsors and
OpenCollective, we also utilize Polar.sh to engage in pledge-based sponsorship.

Check out all issues funded or available for funding on our Polar.sh dashboard

  • If you would like to see an issue prioritized, make a pledge towards it!
  • We receive the pledge once the issue is completed & verified
  • This, along with engagement in the community, helps us know which features are a priority to our users.
Fund with Polar
@rafalkrupinski rafalkrupinski added the Bug 🐛 This is something that is not working as expected label Jul 16, 2024
rafalkrupinski added a commit to rafalkrupinski/litestar that referenced this issue Jul 16, 2024
@rafalkrupinski rafalkrupinski linked a pull request Jul 16, 2024 that will close this issue
@provinzkraut
Copy link
Member

Other than a theoretical, can you explain what use case this allows that's currently not possible?

Litestar will only ever provide parameters as concrete types, i.e. will always pass a list[str], never a Sequence[str], so I'm not sure what the benefit of allowing such annotations is, aside from catering to individual style preferences.

I'm asking because it is very much intentional to be restrictive about the typing, so as long as there's no functionality to be gained here, I'm wary of accepting such a feature request.

@provinzkraut provinzkraut changed the title Bug: Parameter of type Iterable not processed as list Enhancement: Parameter of type Iterable not processed as list Jul 16, 2024
@provinzkraut provinzkraut added Enhancement This is a new feature or request and removed Bug 🐛 This is something that is not working as expected labels Jul 16, 2024
@provinzkraut provinzkraut changed the title Enhancement: Parameter of type Iterable not processed as list Enhancement: Alow parameter of type Iterable to be processed as list Jul 16, 2024
@rafalkrupinski
Copy link
Contributor Author

rafalkrupinski commented Jul 16, 2024

Currently Set, Sequence and possibly other types are accepted as parameter types, so it's unintuitive for me as a user, that litestar doesn'tt accept Iterable. The change is for ergonomics.

For example you don't want to do this. Even if it worked with litestar, it's a mutable default parameter value.

param: list[str] = []

This works but has different semantics and generates different OpenAPI schema, and you need to handle None value manually, and you need another variable or assert to please mypy.

param: list[str]|None=None

you can do

param: Sequence[str]=()

but currently this fails

param: Iterable[str]=()

It's unintuitive that one abstract type works and another doesn't.

@provinzkraut
Copy link
Member

Thanks for the explanation @rafalkrupinski, that seems reasonable!

@JacobCoffee JacobCoffee changed the title Enhancement: Alow parameter of type Iterable to be processed as list Enhancement: Allow parameter of type Iterable to be processed as list Aug 12, 2024
provinzkraut pushed a commit to rafalkrupinski/litestar that referenced this issue Sep 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement This is a new feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants