Skip to content

Fix non-code string annotations during instrumentation - #568

Open
deepakganesh78 wants to merge 1 commit into
agronholm:masterfrom
deepakganesh78:fix/issue513-non-code-string-annotations
Open

Fix non-code string annotations during instrumentation#568
deepakganesh78 wants to merge 1 commit into
agronholm:masterfrom
deepakganesh78:fix/issue513-non-code-string-annotations

Conversation

@deepakganesh78

Copy link
Copy Markdown

Fixes #513

Reproduction

On current master, this minimal example fails while applying @typechecked:

import typeguard

annotation_map = {"a b": int}

@typeguard.typechecked
def foo(x: annotation_map["a b"]) -> int:
    return x

It raises SyntaxError: invalid syntax because 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 with ast.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 Literal annotations. The behavioral change is limited to non-parseable string values in annotations, which now survive instrumentation instead of aborting decoration/import.

Validation

  • Minimal reproduction: failed on current master with SyntaxError, then printed 10 after the fix.
  • Regression test with fix reverted: python -m pytest tests\test_instrumentation.py::test_annotation_with_non_code_string -q => 1 passed, 1 error (SyntaxError).
  • Regression test with fix: same command => 2 passed in 0.06s.
  • Full suite: python -m pytest => 531 passed, 9 skipped, 9 xfailed in 2.66s.
  • Lint: python -m pre_commit run --files src\typeguard\_transformer.py tests\issue513.py tests\test_instrumentation.py docs\versionhistory.rst => all hooks passed.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 94.838% (+0.008%) from 94.83% — deepakganesh78:fix/issue513-non-code-string-annotations into agronholm:master

@agronholm

Copy link
Copy Markdown
Owner

What about non-code annotations that DO parse as valid Python?

@agronholm

Copy link
Copy Markdown
Owner

What is the use case even here?

@agronholm

Copy link
Copy Markdown
Owner

You saw the last comment on the related issue? Assuming there's somebody home there @deepakganesh78

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Syntax error for custom type annotations

3 participants