mirror of
https://github.com/python/cpython.git
synced 2025-11-01 02:38:53 +00:00
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:
parent
bf89ff96e6
commit
889036f7ef
4 changed files with 43 additions and 7 deletions
|
|
@ -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):
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue