🐛 Fix evaluating stringified annotations in Python 3.8 & 3.9 (also from __future__ import annotations)#1264
Open
sisp wants to merge 2 commits intofastapi:masterfrom
Open
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
cc4449a to
07195af
Compare
from __future__ import annotations)from __future__ import annotations)
This comment was marked as outdated.
This comment was marked as outdated.
3121aa8 to
7e483b8
Compare
This comment was marked as outdated.
This comment was marked as outdated.
7e483b8 to
33a2075
Compare
Contributor
|
📝 Docs preview for commit 33a2075 at: https://f14cfab7.typertiangolo.pages.dev |
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
…rom __future__ import annotations`)
33a2075 to
9fea46f
Compare
Contributor
📝 Docs previewLast commit 9fea46f at: https://a7cf0ef0.typertiangolo.pages.dev |
Member
|
Support for Python 3.8 has now also been dropped, so I think this PR will need to be thoroughly reviewed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I've fixed evaluating stringified annotations in Python 3.9 and less when using the
Annotated[<type>, typer.{Argument,Option}(...)]syntax. When removing the@needs_py310decorator from thetest_annotated_argument_in_string_type_with_defaulttest, it fails with the following error:This problem typically occurs when
from __future__ import annotationsis used with Python prior to 3.10.This PR fixes the problem for Python 3.8 & 3.9 by evaluating string annotations with
typing_extensions.get_annotations(...)(added in v4.13.0) instead ofinspect.signature(..., eval_str=True), which is only available for Python 3.10+. I can't get this to work for Python 3.7, which is still declared as supported despite having reached EOL more than 2 years ago, as typing-extensions v4.13.0 requires Python 3.8+. Nevertheless, this PR fixes the above problem for all non-EOL Python versions and even Python 3.8.