Skip to content

annotationlib: ref.evaluate(format=Format.FORWARDREF) not evaluating global generics with undefined params #138425

@dr-carlos

Description

@dr-carlos

Bug report

Bug description:

from typing import Sequence
from annotationlib import Format, get_annotations

class A:
    x: fails[Sequence, undef]
    y: fails[list, undef]
    z: works[list, list]

x_anno = get_annotations(A, format=Format.FORWARDREF)["x"]
y_anno = get_annotations(A, format=Format.FORWARDREF)["y"]
z_anno = get_annotations(A, format=Format.FORWARDREF)["z"]

fails = dict
works = dict

print(x_anno.evaluate(format=Format.FORWARDREF))
print(y_anno.evaluate(format=Format.FORWARDREF))
print(z_anno.evaluate(format=Format.FORWARDREF))

This should print:

dict[Sequence, undef]
dict[list, undef]
dict[list, list]

Instead, the following is outputted:

ForwardRef('fails[__annotationlib_name_1__, undef]', is_class=True, owner=<class '__main__.A'>)
ForwardRef('fails[__annotationlib_name_1__, undef]', is_class=True, owner=<class '__main__.A'>)
dict[list, list]

The issue seems to be that the globals are not considered when creating the _StringifierDict in this code path (whereas builtins and locals are):
https://github.com/python/cpython/blob/572df47840d910b9fc9cd951074232ae89442be1/Lib/annotationlib.py#L190C13-L196C14n

CPython versions tested on:

CPython main branch, 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 fixesstdlibStandard Library Python modules in the Lib/ directorytopic-typingtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions