Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

3.14 regression withtyping._eval_type() #136316

Closed
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
@Viicos

Description

@Viicos

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 privatetyping._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 haveTypeAdapter(list['ForwardReference']) working).

In 3.13, the following works:

fromtypingimportForwardRef,_eval_typeMyList=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:

fromannotationlibimportForwardReffromtypingimport_eval_typeMyList=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 passingglobalns=globals() in 3.14 doesn't work, astyping._eval_type() is setting the globals toNone if a__forward_module__ is present on theForwardRef:

cpython/Lib/typing.py

Lines 432 to 447 in5de7e3f

def_eval_type(t,globalns,localns,type_params=_sentinel,*,recursive_guard=frozenset(),
format=None,owner=None):
"""Evaluate all forward references in the given type t.
For use of globalns and localns see the docstring for get_type_hints().
recursive_guard is used to prevent infinite recursion with a recursive
ForwardRef.
"""
iftype_paramsis_sentinel:
_deprecation_warning_for_no_type_params_passed("typing._eval_type")
type_params= ()
ifisinstance(t,_lazy_annotationlib.ForwardRef):
# If the forward_ref has __forward_module__ set, evaluate() infers the globals
# from the module, and it will probably pick better than the globals we have here.
ift.__forward_module__isnotNone:
globalns=None

Which will affectall recursive calls to_eval_type() fromevaluate_forward_ref().


Sorry I couldn't find a repro not relying on this private function (nor I could get rid of the explicitForwardRef usage). Feel free to close as not planned, we can live without this in Pydantic (I hope).

cc@JelleZijlstra

CPython versions tested on:

3.14

Operating systems tested on:

No response

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


      [8]ページ先頭

      ©2009-2025 Movatter.jp