Skip to content

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

Merged
merged 4 commits into from
Aug 6, 2024

Conversation

ericbn
Copy link
Contributor

@ericbn ericbn commented Jul 4, 2024

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-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

User experience

The initial motivation of this PR is what we agreed as step 1 of #4607:

  1. Review the use of mypy_boto3_* types, and try to keep their imports to a minimum, to only when those types cannot be discovered automatically by mypy.

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:

aws_lambda_powertools/utilities/idempotency/persistence/dynamodb.py: note: In member "_put_record" of class "DynamoDBPersistenceLayer":
aws_lambda_powertools/utilities/idempotency/persistence/dynamodb.py:251:74: error: TypedDict "_ClientErrorResponseTypeDef" has no key "Item"  [typeddict-item]
                    old_data_record = self._item_to_data_record(exc.response["Item"]) if "Item" in exc.response else None
                                                                             ^~~~~~
Found 1 error in 1 file (checked 616 source files)
make: *** [mypy] Error 1

Checklist

If your change doesn't seem to apply, please leave them unchecked.

Is this a breaking change?

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

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.

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
@ericbn ericbn requested a review from a team July 4, 2024 20:15
@boring-cyborg boring-cyborg bot added dependencies Pull requests that update a dependency file documentation Improvements or additions to documentation streaming tests typing Static typing definition related issues (mypy, pyright, etc.) labels Jul 4, 2024
@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jul 4, 2024
@github-actions github-actions bot added internal Maintenance changes and removed documentation Improvements or additions to documentation labels Jul 5, 2024
@leandrodamascena
Copy link
Contributor

Hello @ericbn! I'm planning to review this PR right after we finish #4606!

@leandrodamascena leandrodamascena self-assigned this Jul 22, 2024
@leandrodamascena leandrodamascena linked an issue Jul 22, 2024 that may be closed by this pull request
2 tasks
@leandrodamascena
Copy link
Contributor

I will start the review tomorrow morning. 🚀

@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Jul 31, 2024
Copy link
Contributor

@leandrodamascena leandrodamascena left a 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.

@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Aug 1, 2024
@leandrodamascena
Copy link
Contributor

mypy is complaining about something, but it seems easy to fix. Can you take a look, please @ericbn?

@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Aug 2, 2024
@ericbn
Copy link
Contributor Author

ericbn commented Aug 2, 2024

Cool! @leandrodamascena, I've updated the code to ignore that mypy error, since this was a case for a DynamoDB request with ReturnValuesOnConditionCheckFailure, for which the response type was not covered by mypy_boto3_dynamodb.

@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Aug 5, 2024
Copy link
Contributor

@leandrodamascena leandrodamascena left a 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.

@leandrodamascena leandrodamascena changed the title chore(deps-dev): enable mypy-boto3 implicit type annotations refactor(typing): enable boto3 implicit type annotations Aug 6, 2024
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Aug 6, 2024
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Aug 6, 2024
@github-actions github-actions bot added enhancement and removed internal Maintenance changes labels Aug 6, 2024
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Aug 6, 2024
Copy link

sonarqubecloud bot commented Aug 6, 2024

@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Aug 6, 2024
Copy link
Contributor

@sthulb sthulb left a 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.

@leandrodamascena leandrodamascena self-requested a review August 6, 2024 15:32
Copy link
Contributor

@leandrodamascena leandrodamascena left a 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!!!

@leandrodamascena leandrodamascena merged commit 26cfe7f into aws-powertools:v3 Aug 6, 2024
11 of 12 checks passed
@ericbn ericbn deleted the add-boto3-stubs branch August 6, 2024 15:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependencies Pull requests that update a dependency file enhancement size/L Denotes a PR that changes 100-499 lines, ignoring generated files. streaming tests typing Static typing definition related issues (mypy, pyright, etc.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tech debt: Inconsistent use of from __future__ import annotations
3 participants