-
Notifications
You must be signed in to change notification settings - Fork 421
refactor(typing): enable boto3 implicit type annotations #4692
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
Conversation
Add boto3-stubs and botocore-stubs to enable implicit type annotations with mypy_boto3_* packages. See https://youtype.github.io/boto3_stubs_docs/mypy_boto3_s3/usage/#implicit-type-annotations This enables removing very few unneeded explicit type annotations and helps detect some new errors automatically. No need to do boto3_config = boto3_config or Config() before passing the boto3_config to the boto3_session client or resource methods, as None is an accepted value. Refactor the aws_lambda_powertools.utilities.parameters.base.BaseProvider class: move the instantiation of clients in _build_boto3_client and _build_boto3_resource_client to the subclasses and the user_agent method calls to the constructor. Keeping correct typing information with the original implementation was going to be too complex and hopefully the refactor even made the code tidier. Keep imports consistent: use boto3.session.Session, mypy_boto3_*.client, mypy_boto3_*.service_resource and mypy_boto3_*.type_defs as documented at https://boto3.amazonaws.com/v1/documentation/api/latest/reference/core/session.html and https://youtype.github.io/boto3_stubs_docs/mypy_boto3_s3/usage/#explicit-type-annotations
I will start the review tomorrow morning. 🚀 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @ericbn! I've started looking into this PR. Everything looks great, but I see that you refactored an important part of the code where we register the UserAgent
in the session. This code is critical and I'll need a few days to do some exploratory testing to see if we won't have any side effects.
As always, you have great attention to detail and your code is elegant and clean, but I need to be 100% sure that refactoring this part of the code won't have any issues that our tests haven't caught.
aws_lambda_powertools/utilities/idempotency/persistence/dynamodb.py
Outdated
Show resolved
Hide resolved
mypy is complaining about something, but it seems easy to fix. Can you take a look, please @ericbn? |
Cool! @leandrodamascena, I've updated the code to ignore that mypy error, since this was a case for a DynamoDB request with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @ericbn! The only thing missing is to review the Idempotency change in the put_record
method, but the other changes are fantastic! Thank you for improving the quality of the project in development environments, making the project more compatible with python styles and removing unnecessary code.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall, I think this PR is a win.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another amazing PR @ericbn! I'm more than happy to say APPROVED!!!
Issue number: #4607
Summary
Changes
Add boto3-stubs and botocore-stubs to enable implicit type annotations with
mypy_boto3_*
packages. See https://youtype.github.io/boto3_stubs_docs/mypy_boto3_s3/usage/#implicit-type-annotationsThis enables removing very few unneeded explicit type annotations and helps detect some new errors automatically.
No need to do
before passing the boto3_config to the boto3_session client or resource methods, as None is an accepted value.
Refactor the aws_lambda_powertools.utilities.parameters.base.BaseProvider class: move the instantiation of clients in _build_boto3_client and _build_boto3_resource_client to the subclasses and the user_agent method calls to the constructor. Keeping correct typing information with the original implementation was going to be too complex and hopefully the refactor even made the code tidier.
Keep imports consistent: use boto3.session.Session,
mypy_boto3_*.client
,mypy_boto3_*.service_resource
andmypy_boto3_*.type_defs
as documented at https://boto3.amazonaws.com/v1/documentation/api/latest/reference/core/session.html and https://youtype.github.io/boto3_stubs_docs/mypy_boto3_s3/usage/#explicit-type-annotationsUser experience
The initial motivation of this PR is what we agreed as step 1 of #4607:
In the process of reviewing the respective code, I've noticed that implicit type annotations for
mypy_boto3_*
packages was not enabled, due to missing dev dependencies as mentioned above. Adding this allows both reducing the need to explicitly annotate these types -- which we were not doing much anyway -- and letting types be discovered automatically -- which we were missing, given some errors were discovered. All new errors reported by mypy were fixed, unless for one. I'm not sure if this is intentional in the code:Checklist
If your change doesn't seem to apply, please leave them unchecked.
Is this a breaking change?
RFC issue number:
Checklist:
Acknowledgment
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.