-
Notifications
You must be signed in to change notification settings - Fork 403
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
Feature request: query_string_parameters should never be None #1359
Comments
Thanks for opening your first issue here! We'll come back to you as soon as we can. |
Hi @a3kov, thank you for raising this. When you say it's "unexpected", could you expand on that please? I've just tried on both PyCharm and VSCode including Mypy and they flag that That said, I agree that we could provide a better experience by supporting a sentinel value like we do in other cases like Thank you!! |
Cheers for the ping @heitorlessa! Is the parallel you are drawing to On the overall issue I've got an example event log in front of me and it has If I was OP I'd probably just create a local reference with |
Yes @peterschutt. Like a params = app.curent.event.get_query_string_parameters("message", default={}) # dict return
my_param = app.curent.event.get_query_string_parameters("message", default="") # str in this case Challenge here is that we can't control what the sentinel value would be, so I suspect we could solve with an In your experience, is that the best way to handle it? If so, I'd be open to have a PR to add |
And yes, I also agree that defining a local The only added benefit of having a |
@heitorlessa I meant unexpected coming from web frameworks. It's just not practical to have it as |
As @a3kov is thinking about it from a web framework perspective, an analogous solution could be a method on the resolver that always returns a
Always harder to remove code than to add it:) |
I understand it would be a nicer experience if you could always do
However, I see the
|
@a3kov hi! I think it's a good scenario for monads (you should be scared here lol). In this particular case I'd use Maybe monad with function binding/piping. Take a look at this project that implements and, what is more important, describes monads in a very "gentle" way. So let's suppose we use this library in our project/lambda. How does our code can look like: import json
import os
from typings import Optional
import boto3
from returns.maybe import Maybe
def dynamodb_query(qs: dict) -> dict:
dynamo = boto3.resource("dynamodb")
table = dynamo.Table(os.getenv("TABLE_NAME", "table_name"))
return table.query(**qs)
@app.get('/')
def list() -> Response:
qs: Optional[dict] = app.current_event.query_string_parameters
qs: Maybe[dict] = Maybe.from_optional(qs) # Some(dict) or Nothing
# magic starts here
# it will execute `dynamodb_query` only if `qs` != `Nothing`
query_result: Maybe[dict] = qs.bind_optional(dynamodb_query)
response_body = {
"items": query_result.value_or([])
}
return Response(200, "application/json", json.dumps(response_body)) |
@ppavlovdev Thank you for introducing me to returns. Seems to be a very interesting project. However, for powertools, I feel the use case to be too specific to justify adding a dependency for it. Also, as there are have been other options provided before, I would refrain from adding something like |
@sthuber90 I think I described my idea not very well sorry. I didn't mean to integrate |
The point is not whether or not its easy to check manually (it is). The point is if you want to have better experience for developers. I understand if somebody was checking for |
Since it's a potential breaking change, I'm adding this issue to the Powertools v3 milestone and we can revisit it when we plan for v3. |
Closed via #4606 |
|
Use case
When there are no query string params,
app.current_event.query_string_parameters
isNone
which is unexpected.Solution/User Experience
Proper solution is to make it to behave like an empty dict or to be an actual empty dict.
Alternative solutions
No response
Acknowledgment
The text was updated successfully, but these errors were encountered: