aws-powertools / powertools-lambda-python

A developer toolkit to implement Serverless best practices and increase developer velocity.
https://docs.powertools.aws.dev/lambda/python/latest/
MIT No Attribution
2.71k stars 376 forks source link

Static typing: overloads for SecretsProvider.get() #4622

Open rtandy opened 5 days ago

rtandy commented 5 days ago

Static type checker used

mypy (project's standard)

AWS Lambda function runtime

3.11

Powertools for AWS Lambda (Python) version

latest (2.39.1)

Static type checker info

parameters.get_secret() has overloads to vary its return type based on the transform parameter. Would it be possible for BaseProvider.get() to have the same overloads? For context, I am using SecretsProvider so that I can bring my own boto3 client.

Also, looking at those existing overloads, are the binary and auto cases backwards? It has:

# ...
    transform: Literal["binary"],
# ...
) -> Union[str, dict, bytes]: ...

# ...
    transform: Literal["auto"],
# ...
) -> bytes: ...

I would have expected binary would return bytes, and auto would return str | dict | bytes?

Thank you for your work on Powertools!

Code snippet

from aws_lambda_powertools.utilities import parameters
import boto3

secret1 = parameters.get_secret('test_secret', transform='json')
reveal_type(secret1) # dict

client = boto3.client('secretsmanager')
provider = parameters.SecretsProvider(boto3_client=client)
secret2 = provider.get('test_secret', transform='json')
reveal_type(secret2) # str | dict | bytes | None; would like dict

reveal_type(parameters.get_secret('test_secret')) # str; OK
reveal_type(parameters.get_secret('test_secret', transform='json')) # dict; OK
reveal_type(parameters.get_secret('test_secret', transform='binary')) # str | dict | bytes; should be bytes?
reveal_type(parameters.get_secret('test_secret', transform='auto')) # bytes; should be str | dict | bytes?

Possible Solution

No response

boring-cyborg[bot] commented 5 days ago

Thanks for opening your first issue here! We'll come back to you as soon as we can. In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

leandrodamascena commented 4 days ago

Hello @rtandy! Thanks for opening this issue, although we don't fully support typing checking and some utilities are very hard to resolve without major refactoring, this topic is something we are always looking to improve here at Powertools.

Do you want to work in a PR to fix this? I can help with that if you need any help.

Thanks