Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.3k
Closed
Description
Bug report
Bug description:
Usually, when a local variable is introduced after getting a ForwardRef, evaluate() picks this up and correctly evaluates it using theself.__cell__ short-circuit. However, this does not work when that local variable is a generic. It does still work for globals.
fromannotationlibimportFormat,get_annotationsdefworks_global():classDemo:x:sequence_a[int]fwdref=get_annotations(Demo,format=Format.FORWARDREF)['x']print(f"Global{fwdref!r}")globalsequence_asequence_a=listevaluated=fwdref.evaluate()print(f"Evaluated{evaluated!r}")defworks_nongeneric():classDemo:nonlocalalias# Optionalx:aliasfwdref=get_annotations(Demo,format=Format.FORWARDREF)['x']print(f"Global{fwdref!r}")alias=intevaluated=fwdref.evaluate()print(f"Evaluated{evaluated!r}")deffails():classDemo:nonlocalsequence_bx:sequence_b[int]fwdref=get_annotations(Demo,format=Format.FORWARDREF)['x']print(f"Local{fwdref!r}")sequence_b=listevaluated=fwdref.evaluate()# NameErrorprint(f"Evaluated{evaluated!r}")works_global()works_nongeneric()fails()
CPython versions tested on:
CPython main branch, 3.14
Operating systems tested on:
Linux