Repository navigation
Description
Why is this needed?
Source files that have from __future__ import annotations are not taking full advantage of the import. Namely, this import enables PEP 563 (Postponed Evaluation of Annotations), which allows:
- usage of
if typing.TYPE_CHECKING:for types to be used only by the type checker and no need to quote type names (unless being used in forward references) - use of PEP 585 (Type Hinting Generics In Standard Collections) in Python < 3.9
- use of PEP 604 (Allow writing union types as X | Y) in Python < 3.10
Looking at the files with this import we see that not all of them have if typing.TYPE_CHECKING:, which could have been the initial motivation to add the import, and they have mixed usages of PEP 585 and PEP 604.
Some other files do have if typing.TYPE_CHECKING: without the import and are quoting the type names instead.
Also, I see some if typing.TYPE_CHECKING: is being used for the types defined in the mypy_boto3_* packages, which are dev dependencies and in these cases this makes sense.
Which area does this relate to?
Static typing
Suggestion
We already have a constraint:
- The import of types from the mypy_boto3_* packages must be inside
if typing.TYPE_CHECKING:.
And here are suggestions (see https://stackoverflow.com/a/53455562/2654518):
- Use
from __future__ import annotationsinstead of quoting types imported insideif typing.TYPE_CHECKING:. - Using both
from __future__ import annotationsandif typing.TYPE_CHECKING:can improve the startup performance since less packages are imported during runtime. Although optimizing performance really only makes sense if we have benchmarks first.
Also:
Looks like the current code does not have a clear direction on how to follow these suggestions. I'd say it's more consistent and cleaner to have a guideline. So, here are some decisions that could be made:
- Do we use
from __future__ import annotationsinstead of quoting types that have been imported insideif typing.TYPE_CHECKING:? I'd say YES since we're already using the import in some files. - Do we use
from __future__ import annotationsandif typing.TYPE_CHECKING:in all files?- Even if we choose YES, should we at least skip the example files?
- Do we use PEP 585 and PEP 604 in files using
from __future__ import annotations? I'd say definitely YES if we choose YES for question 2.
Acknowledgment
- This request meets Powertools for AWS Lambda (Python) Tenets
- Should this be considered in other Powertools for AWS Lambda languages? i.e. Java, TypeScript, and .NET
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsShipped
Activity
heitorlessa commentedon Jun 26, 2024
THANK YOU @ericbn for taking the time, this has been a pet peeve of mine for a long while but we never had the bandwidth to kick off a discussion to tackle it. Most was added before the Python's addition of being able to use
listoverList, hence why the inconsistency -- we could enable this linting rule to have a quick plan of action.The two issues I remember clearly were:
Adding
help wantedas we're dealing with v3 now with multi-version Python Lambda Layer (compiled deps).My thoughts on tackling this as of now:
mypy_boto3- VSCode or PyCharm resolving would be enough for us.ericbn commentedon Jun 26, 2024
Oi @heitorlessa.
I've edited the description of the ticket a few times before you've posted your comment. Sorry for the confusion. I'm not sure exactly what you mean by "Go with your 1st suggestion". I'm assuming it's "Only keep
from __future__ import annotationsin files whereif typing.TYPE_CHECKING:is being used" from the original description. In the new description I've broken the decisions into 4 questions (1, 2, 2i, 3) :I see you mean "YES" for 2i.
Removing unneeded quoted type annotations can be reported by ruff's quoted-annotation (UP037), hopefully regardless of the presence of
from __future__ import annotations.Is your overall goal to keep the the occurrences of
from __future__ import annotationsto a minimum?leandrodamascena commentedon Jun 27, 2024
I have another point here that is not directly the problem described here, but is affected by it. We don't have a standard when importing types, sometimes we import from
types, sometimes fromhttps://github.com/aws-powertools/powertools-lambda-python/blob/develop/aws_lambda_powertools/shared/types.pyand because of this we need to addfrom __future__ import annotationsor we may need to check the Python version inside the files to see if the type is supported.We can take this opportunity to improve the use of
from __future__ import annotations, but also create a standard of every type must be imported fromhttps://github.com/aws-powertools/powertools-lambda-python/blob/develop/aws_lambda_powertools/shared/types.py.I would like to hear what do you think.
heitorlessa commentedon Jul 1, 2024
hey @ericbn @leandrodamascena thanks for patience!! I think we've got several subjects here and the formatting isn't helping, causing confusion -- I broke into categories so it's easier to see if we're missing or if we vehemently disagree on one of them.
Let me know if I missed anything, and whether you agree with the direction (then any implementation help is appreciated!).
Summary
from __future__ import annotationsfrom __future__ import annotations"BaseModel"Clientmay not be resolved properlyfrom typing_extensions import ParamSpectyping_extensionsoverif py version.Lazy type evaluation in source code
We should use
from __future__ import annotationsuntil Python 3.9 is deprecated (2025 Oct).As @ericbn called out, this will help us reduce typing imports and where possible get cleaner type annotations like
str | int | NoneoverOptional[Union[str, int]].Lazy type evaluation in examples
Since we don't know what Python versions our customers will use (there's plenty still on Py3.7/3.8), it's easier to add
from __future__ import annotationsconsistently across all examples. It helps everyone.Dev type checking
For types we control and less ambiguous type names, we should use forward references and get rid of
if TYPE_CHECKING. This is only possible withfrom __future__ import annotations.However, for types we do not control that have ambiguous type name, we need to test it. Dependencies like
mypy_boto3might have more than oneClient, orTablemight get resolved to another dev dependency installed. If we can't confidently test, I'd suggest we keepmypy_boto3as the first exception to the rule in keeping if TYPE_CHECKING`.Typing new features backport
We should use
from typing_extensions import <Type>for any type that might start by Python version X. Since we addtyping_extensionsas a required dependency, we can get rid oftypes.pyand any branching logic to import a given type to an older Python version.Typing extensions handle the case where a type is already supported in a given Python version and return
from typing import <X>instead, so we don't have to -- ParamSpec example in 3.10+.ericbn commentedon Jul 1, 2024
Hey @heitorlessa.
Makes perfect sense and good callout on using typing_extensions instead of a custom solution.
What do you think about the following plan to tackle this?
aws_lambda_powertools.shared.typesimports and replace them by either types from the standard library that are already compatible with Python >= 3.8 or types from typing_extensions. Remove theaws_lambda_powertools.shared.typespackage too.from __future__ import annotationsto all files, including examples.TCHrules and fix all reported issues. These will detect all typing-only imports that can be moved to aif TYPE_CHECKINGblocks. Expect a significant number of imports to fall into this category specially after addingfrom __future__ import annotationsto the files.UPrules and fix all reported issues. We're particularly interested inUP006 (non-pep585-annotation, UP007 (non-pep604-annotation) and UP037 (quoted-annotation), but I see there are other small changes other rules are reporting too that we can fix, like UP004 (useless-object-inheritance), UP008 (super-call-with-parameters) and UP009 (utf8-encoding-declaration).
I think 1 and 2 can be done in one PR each first. Then the remaining steps can be done after those and #4606 are merged, with separate PRs per feature as proposed before.
heitorlessa commentedon Jul 1, 2024
looks great, one minor change to avoid breaking changes.
this ^ is fine if we target
v3branch that @leandrodamascena and @sthulb are working on. Otherwise, we should cautiously keepaws_lambda_powertools.shared.typesfile, and replace all types withtyping_extensions. Often times we don't know what customers took a dependency on, despite the warnings -- a major version gives us more flexibility.Exciting!!!!
ericbn commentedon Jul 1, 2024
Oh, I was planning all this for
v3. Should have mentioned that. I can start working on it if you agree.10 remaining items
leandrodamascena commentedon Aug 6, 2024
Will you be taking care of these PRs? If any of them include a breaking change, it would be interesting to include them in v3. I think we have another 3 or 4 weeks until v3 is officially released and we can divide and conquer to do this.
cc @ericbn
ericbn commentedon Aug 6, 2024
I can take care of all PRs and I'm aiming all of them for v3 since each step roughly depends on the previous ones. Just opened a PR for step 2.
leandrodamascena commentedon Aug 6, 2024
List of taks to complete this issue:
aws_lambda_powertools.shared.typesfrom __future__ import annotationsto all files, including examplesTCHrulesUPrulesrefactor(typing): reduce aws_lambda_powertools.shared.types usage (#4896
ericbn commentedon Aug 6, 2024
I'm thinking in doing all these in the next PR together:
from __future__ import annotationsto all files, including examplesTCHrulesSounds good? We can enable the remaining
UPrules in a separate PR since those changes won't be related to the main topic discussed in this issue.leandrodamascena commentedon Aug 6, 2024
I think it's ok @ericbn! Enabling Ruff rules is literally only a few new lines in the
ruff.tomlfile. Go aheadericbn commentedon Aug 13, 2024
@leandrodamascena, I'd say I'm roughly halfway done with opening the PRs. Let me know if we're in the right path here and I can continue, or if there's any second thoughts given the amount of changes, which is totally fine.
leandrodamascena commentedon Aug 19, 2024
Closing as completed.
github-actions commentedon Aug 19, 2024
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.