Skip to content

Indentation error while getting annotation of function with an template-string as return type #138558

@15r10nk

Description

@15r10nk

Bug report

Bug description:

I found the following bug while fuzzing with pysource-codegen

from annotationlib import get_annotations, Format

def name_3() -> t'{ {}}':
    pass
get_annotations(name_3, format=Format.STRING)

output (Python 3.14.0rc2+):

Traceback (most recent call last):
  File "/home/frank/projects/pysource-playground/crash.py", line 5, in <module>
    get_annotations(name_3, format=Format.STRING)
    ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/frank/projects/cpython/install/lib/python3.14/annotationlib.py", line 914, in get_annotations
    ann = _get_and_call_annotate(obj, format)
  File "/home/frank/projects/cpython/install/lib/python3.14/annotationlib.py", line 1034, in _get_and_call_annotate
    ann = call_annotate_function(annotate, format, owner=obj)
  File "/home/frank/projects/cpython/install/lib/python3.14/annotationlib.py", line 683, in call_annotate_function
    key: _stringify_single(val)
         ~~~~~~~~~~~~~~~~~^^^^^
  File "/home/frank/projects/cpython/install/lib/python3.14/annotationlib.py", line 814, in _stringify_single
    return ast.unparse(_template_to_ast(anno))
                       ~~~~~~~~~~~~~~~~^^^^^^
  File "/home/frank/projects/cpython/install/lib/python3.14/annotationlib.py", line 573, in _template_to_ast
    value=ast.parse(part.expression),
          ~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/home/frank/projects/cpython/install/lib/python3.14/ast.py", line 46, in parse
    return compile(source, filename, mode, flags,
                   _feature_version=feature_version, optimize=optimize)
  File "<unknown>", line 1
    {}
IndentationError: unexpected indent

I think the problem is the space before {} but this space is required to distinguish it from an escaped {.

CPython versions tested on:

3.14

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.14bugs and security fixes3.15new features, bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)topic-typingtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions