Skip to content

Static typing: utilities.parameters.get_parameter #3507

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

Closed
troyswanson opened this issue Dec 14, 2023 · 5 comments · Fixed by #3534
Closed

Static typing: utilities.parameters.get_parameter #3507

troyswanson opened this issue Dec 14, 2023 · 5 comments · Fixed by #3534
Assignees
Labels
parameters Parameters utility typing Static typing definition related issues (mypy, pyright, etc.)

Comments

@troyswanson
Copy link
Contributor

troyswanson commented Dec 14, 2023

Static type checker used

pyright/pylance

AWS Lambda function runtime

3.10

Powertools for AWS Lambda (Python) version

2.29.1

Static type checker info

Argument of type "str | dict[Unknown, Unknown] | bytes" cannot be assigned to parameter "__x" of type "ConvertibleToInt" in function "new"

Code snippet

from aws_lambda_powertools.utilities.parameters import get_parameter


param_value = get_parameter("/My/Parameter")
param_as_int = int(param_value)

Possible Solution

I believe this is happening because the get_parameter function can be configured via the transform argument to convert the param into a dictionary (if the response if a JSON string) or bytes (if the response is a base64 encoded string).

The type annotation for the function return can be overloaded depending on what value is used for that argument.

@troyswanson troyswanson added triage Pending triage from maintainers typing Static typing definition related issues (mypy, pyright, etc.) labels Dec 14, 2023
@rubenfonseca
Copy link
Contributor

Looking at this now

@rubenfonseca
Copy link
Contributor

@troyswanson I need a little more help here. Is this on VSCode? I pasted your code into a file but I can't generate an error message. Can you please give me more information about the setup?

@rubenfonseca rubenfonseca removed the triage Pending triage from maintainers label Dec 15, 2023
@rubenfonseca rubenfonseca moved this from Triage to Working on it in Powertools for AWS Lambda (Python) Dec 15, 2023
@rubenfonseca rubenfonseca self-assigned this Dec 15, 2023
@troyswanson
Copy link
Contributor Author

troyswanson commented Dec 15, 2023

Yes, I'm using VSCode. Here's a screenshot from my computer while hovering over the param_value variable.

Screenshot 2023-12-15 at 9 53 31 AM

The return type for get_parameter is str | dict[Unknown, Unknown] | bytes because the function has the ability to transform the data from SSM into a dict or bytes. However, if you don't specify a transformation, it will just return as a str.

Using the int function on a dict type will always fail, which is why the type checker is throwing the warning.

A fix that I can make in my code would be to use typing.cast to force the type checker to assume the value is a str type, but I believe this can be solved in the Powertools library by overloading the type hints for the get_parameter function depending on what kind of arguments are being passed to it.

@rubenfonseca
Copy link
Contributor

Thank you for the update and for testing the issue locally! It sounds like you've pinpointed the challenge with the SSM provider. However I'm not seeing a simple way of implementing the override, mainly due to it being very specific to the SSM provider.

If you have a clear direction in mind for a solution, we'd greatly appreciate your contribution. Please feel free to open a pull request, and we can collaborate to refine the implementation and get it ready for merging. Looking forward to seeing your approach and working together on this!

@rubenfonseca rubenfonseca moved this from Working on it to Backlog in Powertools for AWS Lambda (Python) Dec 18, 2023
@rubenfonseca rubenfonseca added the need-customer-feedback Requires more customers feedback before making or revisiting a decision label Dec 18, 2023
@rubenfonseca rubenfonseca added parameters Parameters utility and removed need-customer-feedback Requires more customers feedback before making or revisiting a decision labels Dec 19, 2023
@rubenfonseca rubenfonseca moved this from Backlog to Working on it in Powertools for AWS Lambda (Python) Dec 19, 2023
@github-project-automation github-project-automation bot moved this from Working on it to Coming soon in Powertools for AWS Lambda (Python) Dec 20, 2023
Copy link
Contributor

⚠️COMMENT VISIBILITY WARNING⚠️

This issue is now closed. Please be mindful that future comments are hard for our team to see.

If you need more assistance, please either tag a team member or open a new issue that references this one.

If you wish to keep having a conversation with other community members under this issue feel free to do so.

@leandrodamascena leandrodamascena moved this from Coming soon to Shipped in Powertools for AWS Lambda (Python) Jan 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
parameters Parameters utility typing Static typing definition related issues (mypy, pyright, etc.)
Projects
Status: Shipped
2 participants