Bug report
Bug description:
Usually, when a local variable is introduced after getting a ForwardRef, evaluate() picks this up and correctly evaluates it using the self.__cell__ short-circuit. However, this does not work when that local variable is a generic. It does still work for globals.
from annotationlib import Format, get_annotations def works_global(): class Demo: x: sequence_a[int] fwdref = get_annotations(Demo, format=Format.FORWARDREF)['x'] print(f"Global {fwdref!r}") global sequence_a sequence_a = list evaluated = fwdref.evaluate() print(f"Evaluated {evaluated!r}") def works_nongeneric(): class Demo: nonlocal alias # Optional x: alias fwdref = get_annotations(Demo, format=Format.FORWARDREF)['x'] print(f"Global {fwdref!r}") alias = int evaluated = fwdref.evaluate() print(f"Evaluated {evaluated!r}") def fails(): class Demo: nonlocal sequence_b x: sequence_b[int] fwdref = get_annotations(Demo, format=Format.FORWARDREF)['x'] print(f"Local {fwdref!r}") sequence_b = list evaluated = fwdref.evaluate() # NameError print(f"Evaluated {evaluated!r}") works_global() works_nongeneric() fails()
CPython versions tested on:
CPython main branch, 3.14
Operating systems tested on:
Linux