Skip to content

typing - evaluating forward references in STRING format can 'leak' internal names #150641

@cossor

Description

@cossor

Bug report

Bug description:

Resolution of gh-148680 addressed the underlying annotationlib issue without changing the behavior of evaluate_forward_ref.

On 3.14.5:

from annotationlib import Format, get_annotations
from typing import TYPE_CHECKING, evaluate_forward_ref


def test(arg: Iterable[int]) -> None:
    pass


forward_ref = get_annotations(test, format=Format.FORWARDREF)['arg']
assert forward_ref.evaluate(format=Format.STRING) == 'Iterable[int]'

bad_string = evaluate_forward_ref(forward_ref, format=Format.STRING)
assert bad_string == 'Iterable[__annotationlib_name_1__]'
assert bad_string is forward_ref.__forward_arg__

if TYPE_CHECKING:
    from collections.abc import Iterable

CPython versions tested on:

3.14

Operating systems tested on:

macOS

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions