Fix non-code string annotations during instrumentation - #568
Open
deepakganesh78 wants to merge 1 commit into
Open
Fix non-code string annotations during instrumentation#568deepakganesh78 wants to merge 1 commit into
deepakganesh78 wants to merge 1 commit into
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Owner
|
What about non-code annotations that DO parse as valid Python? |
Owner
|
What is the use case even here? |
Owner
|
You saw the last comment on the related issue? Assuming there's somebody home there @deepakganesh78 |
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.
Fixes #513
Reproduction
On current
master, this minimal example fails while applying@typechecked:It raises
SyntaxError: invalid syntaxbecause the instrumenter tries to parse the string subscript value"a b"as a Python expression.Root cause
AnnotationTransformer.visit_Constant()treated every string literal inside an annotation as a potential forward reference and parsed it withast.parse(..., mode="eval"). Custom annotation expressions may legitimately contain string subscript values or metadata strings that are not valid Python expressions.Fix
If parsing a string literal as a forward reference raises
SyntaxError, the transformer now leaves the original string constant in place. Valid string forward references still follow the existing path, while non-code strings used by custom annotations remain valid runtime annotation values.Compatibility notes
This preserves existing handling for parseable string forward references and
Literalannotations. The behavioral change is limited to non-parseable string values in annotations, which now survive instrumentation instead of aborting decoration/import.Validation
masterwithSyntaxError, then printed10after the fix.python -m pytest tests\test_instrumentation.py::test_annotation_with_non_code_string -q=>1 passed, 1 error(SyntaxError).2 passed in 0.06s.python -m pytest=>531 passed, 9 skipped, 9 xfailed in 2.66s.python -m pre_commit run --files src\typeguard\_transformer.py tests\issue513.py tests\test_instrumentation.py docs\versionhistory.rst=> all hooks passed.