bpo-41249: Fix postponed annotations for TypedDict (GH-27017)

This fixes TypedDict to work with get_type_hints and postponed evaluation of annotations across modules.

This is done by adding the module name to ForwardRef at the time the object is created and using that to resolve the globals during the evaluation.

Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com>
This commit is contained in:
Germán Méndez Bravo 2021-07-16 20:49:30 -07:00 committed by GitHub
parent bf89ff96e6
commit 889036f7ef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 7 deletions

View file

@ -34,6 +34,7 @@ import weakref
import types
from test import mod_generics_cache
from test import _typed_dict_helper
class BaseTestCase(TestCase):
@ -2819,6 +2820,9 @@ class Point2D(TypedDict):
x: int
y: int
class Bar(_typed_dict_helper.Foo, total=False):
b: int
class LabelPoint2D(Point2D, Label): ...
class Options(TypedDict, total=False):
@ -3995,6 +3999,12 @@ class TypedDictTests(BaseTestCase):
# classes, not instances
assert is_typeddict(Point2D()) is False
def test_get_type_hints(self):
self.assertEqual(
get_type_hints(Bar),
{'a': typing.Optional[int], 'b': int}
)
class IOTests(BaseTestCase):