You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Recursive evaluation of ForwardRef (and PEP 563) #85513
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2020-07-26.15:35:12.210>created_at=<Date2020-07-19.11:28:27.457>labels= ['type-bug']
title='Recursive evaluation of ForwardRef (and PEP 563)'updated_at=<Date2020-07-26.15:35:12.209>user='https://github.com/wyfo'
ForwardRef of ForwardRef are not currently evaluated by get_type_hints, only the first level is, as illustrated in these examples:
fromtypingimportForwardRef, Optional, get_type_hintsdeffunc(a: "Optional[\"int\"]"):
passassertget_type_hints(func)["a"] ==Optional[ForwardRef("int")]
# one would expect get_type_hints(func)["a"] == Optional[int]
from __future__ importannotationsfromtypingimportForwardRef, Optional, get_type_hintsdeffunc(a: Optional["int"]):
passassertget_type_hints(func)["a"] ==Optional[ForwardRef("int")]
# one would expect get_type_hints(func)["a"] == Optional[int] (which is the case without the import of __future__.annotations!)
On the one hand I find this behavior quite counter-intuitive; I rather think ForwardRef as kind of internal (and wonder why there is no leading underscore, like _GenericAlias where it's used) and I don't understand the purpose of exposing it as the result of the public API get_type_hints. By the way, if ForwardRef can be obtained by retrieving annotations without get_type_hints, stringified annotations (especially since PEP-563) make get_type_hints kind of mandatory, and thus make ForwardRef disappeared (only at the first level so …)
On the other hand, the second example show that adoptions of postponed annotations can change the result of get_type_hints; several libraries relying of get_type_hints could be broken.
An other issue raised here is that if these ForwardRef are not evaluated by get_type_hints, how will be done their evaluatation by the user? It would require to retrieve some globalns/localns — too bad, it's exactly what is doing get_type_hints. And if the ForwardRef is in a class field, the class globalns/localns will have to be kept somewhere while waiting to encounter these random ForwardRef; that's feasible, but really tedious.
Agreeing with Guido Von Rossum (https://bugs.python.org/msg370232), this behavior could be easily "fixed" in get_type_hints.
Actually, there would be only one line to change in ForwardRef._evaluate:
# fromself.__forward_value__=_type_check(
eval(self.__forward_code__, globalns, localns),
"Forward references must evaluate to types.",
is_argument=self.__forward_is_argument__)
# toself.__forward_value__=_eval_type(
_type_check(
eval(
self.__forward_code__, globalns, localns),
"Forward references must evaluate to types.",
is_argument=self.__forward_is_argument__,
),
globalns,
localns,
)
Andifthisfixcouldsolvethe"double ForwardRef"issuementionnedinhttps://bugs.python.org/issue38605, itwouldalsoresolvehttps://bugs.python.org/issue35834inthesametime, raisingNameErrorincaseofunknownForwardRefwithpostponedannotation.
Ok, I admit that I did not think about recursive type when proposing this "fix".
I've tried an implementation that just stop when recursion is encountered in a PR.
Activity
wyfo commentedon Jul 19, 2020
(This issue is already broached in https://bugs.python.org/issue38605, and a in some way in https://bugs.python.org/issue35834, but only as a secondary subject, that's why I've opened a ticket on this particular issue)
ForwardRef of ForwardRef are not currently evaluated by get_type_hints, only the first level is, as illustrated in these examples:
On the one hand I find this behavior quite counter-intuitive; I rather think ForwardRef as kind of internal (and wonder why there is no leading underscore, like _GenericAlias where it's used) and I don't understand the purpose of exposing it as the result of the public API get_type_hints. By the way, if ForwardRef can be obtained by retrieving annotations without get_type_hints, stringified annotations (especially since PEP-563) make get_type_hints kind of mandatory, and thus make ForwardRef disappeared (only at the first level so …)
On the other hand, the second example show that adoptions of postponed annotations can change the result of get_type_hints; several libraries relying of get_type_hints could be broken.
An other issue raised here is that if these ForwardRef are not evaluated by get_type_hints, how will be done their evaluatation by the user? It would require to retrieve some globalns/localns — too bad, it's exactly what is doing get_type_hints. And if the ForwardRef is in a class field, the class globalns/localns will have to be kept somewhere while waiting to encounter these random ForwardRef; that's feasible, but really tedious.
Agreeing with Guido Von Rossum (https://bugs.python.org/msg370232), this behavior could be easily "fixed" in get_type_hints.
Actually, there would be only one line to change in ForwardRef._evaluate:
wyfo commentedon Jul 20, 2020
Ok, I admit that I did not think about recursive type when proposing this "fix".
I've tried an implementation that just stop when recursion is encountered in a PR.
gvanrossum commentedon Jul 22, 2020
New changeset 653f420 by wyfo in branch 'master':
bpo-41341: Recursive evaluation of ForwardRef in get_type_hints (bpo-21553)
653f420
gvanrossum commentedon Jul 22, 2020
Łukasz, can I please have a decision on whether to backport this bugfix to 3.9?
See my comment about that:
#21553 (review)
ambv commentedon Jul 26, 2020
Given the previous behavior was clearly a bug and after looking at the PR, I think it should go into 3.9.0rc1.
miss-islington commentedon Jul 26, 2020
New changeset 41d1c04 by Miss Islington (bot) in branch '3.9':
bpo-41341: Recursive evaluation of ForwardRef in get_type_hints (GH-21553)
41d1c04
gvanrossum commentedon Jul 26, 2020
Thank you Joseph Perez! Looking forward to more of your contributions.