Skip to content

ForwardRef: do not cache evaluated value #129464

@JelleZijlstra

Description

@JelleZijlstra

Currently, when a ForwardRef is successfully evaluated, we store the resulting value and on subsequent calls return it immediately, regardless of the arguments passed to ForwardRef.evaluate().

This is currently implemented here:

if self.__forward_evaluated__:

It matches previous behavior in 3.13:

if not self.__forward_evaluated__ or localns is not globalns:

I think this is confusing: if you call evaluate() on a ForwardRef that was already evaluated, we ignore whatever globals/locals/type_params you pass in and return whatever the result of the previous evaluation was. It would make more sense to me for evaluate() to re-evaluate the ForwardRef every time it is called. Callers can add caching if they want to in a more intelligent way.

As for #129463, cc @agronholm @Viicos @leycec.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions