-
Notifications
You must be signed in to change notification settings - Fork 406
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
Tech debt: Inconsistent use of from __future__ import annotations #4607
Comments
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 The two issues I remember clearly were:
Adding My thoughts on tackling this as of now:
|
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
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 Is your overall goal to keep the the occurrences of |
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 We can take this opportunity to improve the use of I would like to hear what do you think. |
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
Lazy type evaluation in source codeWe should use As @ericbn called out, this will help us reduce typing imports and where possible get cleaner type annotations like Lazy type evaluation in examplesSince 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 Dev type checkingFor types we control and less ambiguous type names, we should use forward references and get rid of
However, for types we do not control that have ambiguous type name, we need to test it. Dependencies like
Typing new features backportWe should use Typing extensions handle the case where a type is already supported in a given Python version and return |
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?
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. |
looks great, one minor change to avoid breaking changes.
this ^ is fine if we target Exciting!!!! |
Oh, I was planning all this for |
Absolutely!! Let’s do it :)
…On Mon, 1 Jul 2024 at 22:19, Eric Nielsen ***@***.***> wrote:
Oh, I was planning all this for v3. Should have mentioned that. I can
start working on it if you agree.
—
Reply to this email directly, view it on GitHub
<#4607 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAZPQBFDAX4DZGC3KWRRGNDZKG2ULAVCNFSM6AAAAABJXYNX6OVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMBQHE2TENBUGE>
.
You are receiving this because you were mentioned.Message ID:
***@***.***
com>
|
I've opened the PR #4692 regarding step 1 of the plan above.
This task ended up having a different outcome that I initially though -- the code didn't have too many unnecessary hardcoded mypy_boto3_* types, which I've removed in the PR, but it was not taking advantage of implicit type annotations. I've enabled that, and it allowed mypy to report a few errors in the code or examples, which I've fixed. I've left one reported error there, as I'm not sure if it's a false-positive or need to be fixed. |
Closed via #4692 |
|
@leandrodamascena, there are 5 ou 6 more steps to go related to this issue. See #4607 (comment). Last PR was step 1. |
Opz! You're right @ericbn! Reopening this issue! |
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 |
As discussed in aws-powertools#4607. This simplifies linting and refactoring so we can introduce from __future__ import annotations to all files, which is the plan as the next step.
Closing as completed. |
|
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: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)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:
if typing.TYPE_CHECKING:
.And here are suggestions (see https://stackoverflow.com/a/53455562/2654518):
from __future__ import annotations
instead of quoting types imported insideif typing.TYPE_CHECKING:
.from __future__ import annotations
andif 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:
from __future__ import annotations
enables the use of PEP 585 and PEP 604 in Python < 3.10.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:
from __future__ import annotations
instead 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.from __future__ import annotations
andif typing.TYPE_CHECKING:
in all files?from __future__ import annotations
? I'd say definitely YES if we choose YES for question 2.Acknowledgment
The text was updated successfully, but these errors were encountered: