Description
Bug report
Bug description:
I'm in the process of adding 3.14 support to Pydantic. To evaluate type annotations, we make use of the private typing._eval_type()
function. In an ideal we shouldn't, but this is very hard to change now (and still, we need evaluate "standalone" type expressions for several reasons, e.g. to be able to have TypeAdapter(list['ForwardReference'])
working).
In 3.13, the following works:
from typing import ForwardRef, _eval_type
MyList = list[int]
MyDict = dict[str, 'MyList']
fw = ForwardRef('MyDict', module=__name__)
print(_eval_type(fw, globalns=None, localns=None, type_params=()))
#> dict[str, list[int]]
However, starting with 3.14, this raises:
from annotationlib import ForwardRef
from typing import _eval_type
MyList = list[int]
MyDict = dict[str, 'MyList']
fw = ForwardRef('MyDict', module=__name__)
print(_eval_type(fw, globalns=None, localns=None, type_params=()))
# NameError: name 'MyList' is not defined
Also explicitly passing globalns=globals()
in 3.14 doesn't work, as typing._eval_type()
is setting the globals to None
if a __forward_module__
is present on the ForwardRef
:
Lines 432 to 447 in 5de7e3f
Which will affect all recursive calls to _eval_type()
from evaluate_forward_ref()
.
Sorry I couldn't find a repro not relying on this private function (nor I could get rid of the explicit ForwardRef
usage). Feel free to close as not planned, we can live without this in Pydantic (I hope).
CPython versions tested on:
3.14
Operating systems tested on:
No response