From f5b357044033ddf168c2b4ab68e480da557270b6 Mon Sep 17 00:00:00 2001 From: Yurii Karabas <1998uriyyo@gmail.com> Date: Tue, 6 Jul 2021 22:24:40 +0300 Subject: [PATCH 01/10] bpo-44490: Fix various issues with types.Union --- Lib/test/test_types.py | 10 ++++ Lib/test/test_typing.py | 25 ++++++++++ Lib/typing.py | 18 +++++-- .../2021-07-06-22-22-15.bpo-44490.BJxPbZ.rst | 3 ++ Objects/unionobject.c | 47 ++++++++++++++++++- 5 files changed, 97 insertions(+), 6 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2021-07-06-22-22-15.bpo-44490.BJxPbZ.rst diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py index 7f7ce86ff08ef3..aa635bf012aecc 100644 --- a/Lib/test/test_types.py +++ b/Lib/test/test_types.py @@ -13,6 +13,9 @@ import weakref import typing + +T = typing.TypeVar('T') + class Example: pass @@ -676,6 +679,13 @@ def test_union_parameter_chaining(self): self.assertEqual((list[T] | list[S]).__parameters__, (T, S)) self.assertEqual((list[T] | list[S])[int, T], list[int] | list[T]) + def test_union_pickle(self): + alias = list[T] | int + s = pickle.dumps(alias) + loaded = pickle.loads(s) + self.assertEqual(alias.__args__, loaded.__args__) + self.assertEqual(alias.__parameters__, loaded.__parameters__) + def test_or_type_operator_with_forward(self): T = typing.TypeVar('T') ForwardAfter = T | 'Forward' diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index e693883094d5c9..6930a438705ac5 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -315,6 +315,8 @@ def test_repr(self): self.assertEqual(repr(u), 'typing.Union[typing.List[int], int]') u = Union[list[int], dict[str, float]] self.assertEqual(repr(u), 'typing.Union[list[int], dict[str, float]]') + u = Union[int | float] + self.assertEqual(repr(u), 'typing.Union[int | float]') def test_cannot_subclass(self): with self.assertRaises(TypeError): @@ -1449,6 +1451,8 @@ def test_basics(self): with self.assertRaises(TypeError): issubclass(SM1, SimpleMapping) self.assertIsInstance(SM1(), SimpleMapping) + T = TypeVar("T") + self.assertEqual(List[list[T] | float].__parameters__, (T,)) def test_generic_errors(self): T = TypeVar('T') @@ -1785,6 +1789,7 @@ def test_extended_generic_rules_repr(self): def test_generic_forward_ref(self): def foobar(x: List[List['CC']]): ... def foobar2(x: list[list[ForwardRef('CC')]]): ... + def foobar3(x: list[ForwardRef('CC')] | int): ... class CC: ... self.assertEqual( get_type_hints(foobar, globals(), locals()), @@ -1794,6 +1799,10 @@ class CC: ... get_type_hints(foobar2, globals(), locals()), {'x': list[list[CC]]} ) + self.assertEqual( + get_type_hints(foobar3, globals(), locals()), + {'x': list[CC] | int} + ) T = TypeVar('T') AT = Tuple[T, ...] @@ -2992,6 +3001,17 @@ def barfoo3(x: BA2): ... BA2 ) + BA3 = typing.Annotated[int | float, "const"] + def barfoo4(x: BA3): ... + self.assertEqual( + get_type_hints(barfoo4, globals(), locals()), + {"x": int | float} + ) + self.assertEqual( + get_type_hints(barfoo4, globals(), locals(), include_extras=True), + {"x": typing.Annotated[int | float, "const"]} + ) + def test_get_type_hints_annotated_refs(self): Const = Annotated[T, "Const"] @@ -4390,6 +4410,9 @@ def test_no_paramspec_in__parameters__(self): self.assertNotIn(P, list[P].__parameters__) self.assertIn(T, tuple[T, P].__parameters__) + self.assertNotIn(P, (list[P] | int).__parameters__) + self.assertIn(T, (tuple[T, P] | int).__parameters__) + def test_paramspec_in_nested_generics(self): # Although ParamSpec should not be found in __parameters__ of most # generics, they probably should be found when nested in @@ -4399,8 +4422,10 @@ def test_paramspec_in_nested_generics(self): C1 = Callable[P, T] G1 = List[C1] G2 = list[C1] + G3 = list[C1] | int self.assertEqual(G1.__parameters__, (P, T)) self.assertEqual(G2.__parameters__, (P, T)) + self.assertEqual(G3.__parameters__, (P, T)) class ConcatenateTests(BaseTestCase): diff --git a/Lib/typing.py b/Lib/typing.py index ca05fb54bf428f..e5892a6f4415f8 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -196,7 +196,7 @@ def _type_repr(obj): return repr(obj) -def _collect_type_vars(types, typevar_types=None): +def _collect_type_vars(types_, typevar_types=None): """Collect all type variable contained in types in order of first appearance (lexicographic order). For example:: @@ -205,10 +205,10 @@ def _collect_type_vars(types, typevar_types=None): if typevar_types is None: typevar_types = TypeVar tvars = [] - for t in types: + for t in types_: if isinstance(t, typevar_types) and t not in tvars: tvars.append(t) - if isinstance(t, (_GenericAlias, GenericAlias)): + if isinstance(t, (_GenericAlias, GenericAlias, types.Union)): tvars.extend([t for t in t.__parameters__ if t not in tvars]) return tuple(tvars) @@ -315,12 +315,14 @@ def _eval_type(t, globalns, localns, recursive_guard=frozenset()): """ if isinstance(t, ForwardRef): return t._evaluate(globalns, localns, recursive_guard) - if isinstance(t, (_GenericAlias, GenericAlias)): + if isinstance(t, (_GenericAlias, GenericAlias, types.Union)): ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__) if ev_args == t.__args__: return t if isinstance(t, GenericAlias): return GenericAlias(t.__origin__, ev_args) + if isinstance(t, types.Union): + return types.Union(ev_args) else: return t.copy_with(ev_args) return t @@ -1009,7 +1011,7 @@ def __getitem__(self, params): for arg in self.__args__: if isinstance(arg, self._typevar_types): arg = subst[arg] - elif isinstance(arg, (_GenericAlias, GenericAlias)): + elif isinstance(arg, (_GenericAlias, GenericAlias, types.Union)): subparams = arg.__parameters__ if subparams: subargs = tuple(subst[x] for x in subparams) @@ -1775,6 +1777,12 @@ def _strip_annotations(t): if stripped_args == t.__args__: return t return GenericAlias(t.__origin__, stripped_args) + if isinstance(t, types.Union): + stripped_args = tuple(_strip_annotations(a) for a in t.__args__) + if stripped_args == t.__args__: + return t + return types.Union(stripped_args) + return t diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-07-06-22-22-15.bpo-44490.BJxPbZ.rst b/Misc/NEWS.d/next/Core and Builtins/2021-07-06-22-22-15.bpo-44490.BJxPbZ.rst new file mode 100644 index 00000000000000..4c66c813ceacb4 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2021-07-06-22-22-15.bpo-44490.BJxPbZ.rst @@ -0,0 +1,3 @@ +Add ability to serialize ``types.Union``. Fix ``typing`` module issues with +``types.Union`` parameters and arguments resolving. Patch provided by Yurii +Karabas. diff --git a/Objects/unionobject.c b/Objects/unionobject.c index d2a10dfec858ea..31e2ec72fdf99b 100644 --- a/Objects/unionobject.c +++ b/Objects/unionobject.c @@ -428,6 +428,13 @@ union_repr(PyObject *self) return NULL; } +static PyObject * +union_reduce(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + unionobject *alias = (unionobject *)self; + return Py_BuildValue("O(O)", Py_TYPE(alias), alias->args); +} + static PyMemberDef union_members[] = { {"__args__", T_OBJECT, offsetof(unionobject, args), READONLY}, {0} @@ -436,6 +443,7 @@ static PyMemberDef union_members[] = { static PyMethodDef union_methods[] = { {"__instancecheck__", union_instancecheck, METH_O}, {"__subclasscheck__", union_subclasscheck, METH_O}, + {"__reduce__", union_reduce, METH_NOARGS}, {0}}; @@ -489,6 +497,42 @@ static PyNumberMethods union_as_number = { .nb_or = _Py_union_type_or, // Add __or__ function }; +static PyObject * +union_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +{ + if (!_PyArg_NoKeywords("Union", kwds)) { + return NULL; + } + if (!_PyArg_CheckPositional("Union", PyTuple_GET_SIZE(args), 1, 1)) { + return NULL; + } + + return _Py_Union(PyTuple_GET_ITEM(args, 0)); +} + +static const char* const cls_attrs[] = { + "__name__", + "__qualname__", + "__module__", + NULL, +}; + +static PyObject * +union_getattro(PyObject *self, PyObject *name) +{ + unionobject *alias = (unionobject *)self; + + for (const char * const *p = cls_attrs; ; p++) { + if (*p == NULL) { + break; + } + if (_PyUnicode_EqualToASCIIString(name, *p)) { + return PyObject_GetAttr((PyObject *) Py_TYPE(alias), name); + } + } + return PyObject_GenericGetAttr(self, name); +} + PyTypeObject _Py_UnionType = { PyVarObject_HEAD_INIT(&PyType_Type, 0) .tp_name = "types.Union", @@ -502,7 +546,7 @@ PyTypeObject _Py_UnionType = { .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, .tp_traverse = union_traverse, .tp_hash = union_hash, - .tp_getattro = PyObject_GenericGetAttr, + .tp_getattro = union_getattro, .tp_members = union_members, .tp_methods = union_methods, .tp_richcompare = union_richcompare, @@ -510,6 +554,7 @@ PyTypeObject _Py_UnionType = { .tp_as_number = &union_as_number, .tp_repr = union_repr, .tp_getset = union_properties, + .tp_new = union_new, }; PyObject * From 0bc18006834850e2f61c9157a71a84b056839699 Mon Sep 17 00:00:00 2001 From: Yurii Karabas <1998uriyyo@gmail.com> Date: Tue, 6 Jul 2021 23:38:10 +0300 Subject: [PATCH 02/10] Fix repr test --- Lib/test/test_typing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 6930a438705ac5..6d9d028e425903 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -316,7 +316,7 @@ def test_repr(self): u = Union[list[int], dict[str, float]] self.assertEqual(repr(u), 'typing.Union[list[int], dict[str, float]]') u = Union[int | float] - self.assertEqual(repr(u), 'typing.Union[int | float]') + self.assertEqual(repr(u), 'typing.Union[int, float]') def test_cannot_subclass(self): with self.assertRaises(TypeError): From d699211892947f6b769681a94790bf984d913bf3 Mon Sep 17 00:00:00 2001 From: Yurii Karabas <1998uriyyo@gmail.com> Date: Wed, 7 Jul 2021 20:53:51 +0300 Subject: [PATCH 03/10] Remove types.Union serialezation --- Lib/test/test_typing.py | 11 ----- Lib/typing.py | 2 +- .../2021-07-06-22-22-15.bpo-44490.BJxPbZ.rst | 5 +- Objects/unionobject.c | 47 +------------------ 4 files changed, 4 insertions(+), 61 deletions(-) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 6d9d028e425903..3539addebf3438 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -3001,17 +3001,6 @@ def barfoo3(x: BA2): ... BA2 ) - BA3 = typing.Annotated[int | float, "const"] - def barfoo4(x: BA3): ... - self.assertEqual( - get_type_hints(barfoo4, globals(), locals()), - {"x": int | float} - ) - self.assertEqual( - get_type_hints(barfoo4, globals(), locals(), include_extras=True), - {"x": typing.Annotated[int | float, "const"]} - ) - def test_get_type_hints_annotated_refs(self): Const = Annotated[T, "Const"] diff --git a/Lib/typing.py b/Lib/typing.py index e5892a6f4415f8..b5e361c1e6bc57 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -322,7 +322,7 @@ def _eval_type(t, globalns, localns, recursive_guard=frozenset()): if isinstance(t, GenericAlias): return GenericAlias(t.__origin__, ev_args) if isinstance(t, types.Union): - return types.Union(ev_args) + return functools.reduce(operator.or_, ev_args) else: return t.copy_with(ev_args) return t diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-07-06-22-22-15.bpo-44490.BJxPbZ.rst b/Misc/NEWS.d/next/Core and Builtins/2021-07-06-22-22-15.bpo-44490.BJxPbZ.rst index 4c66c813ceacb4..616327ea276f6f 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2021-07-06-22-22-15.bpo-44490.BJxPbZ.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2021-07-06-22-22-15.bpo-44490.BJxPbZ.rst @@ -1,3 +1,2 @@ -Add ability to serialize ``types.Union``. Fix ``typing`` module issues with -``types.Union`` parameters and arguments resolving. Patch provided by Yurii -Karabas. +Fix ``typing`` module issues with ``types.Union`` parameters and arguments +resolving. Patch provided by Yurii Karabas. diff --git a/Objects/unionobject.c b/Objects/unionobject.c index 31e2ec72fdf99b..d2a10dfec858ea 100644 --- a/Objects/unionobject.c +++ b/Objects/unionobject.c @@ -428,13 +428,6 @@ union_repr(PyObject *self) return NULL; } -static PyObject * -union_reduce(PyObject *self, PyObject *Py_UNUSED(ignored)) -{ - unionobject *alias = (unionobject *)self; - return Py_BuildValue("O(O)", Py_TYPE(alias), alias->args); -} - static PyMemberDef union_members[] = { {"__args__", T_OBJECT, offsetof(unionobject, args), READONLY}, {0} @@ -443,7 +436,6 @@ static PyMemberDef union_members[] = { static PyMethodDef union_methods[] = { {"__instancecheck__", union_instancecheck, METH_O}, {"__subclasscheck__", union_subclasscheck, METH_O}, - {"__reduce__", union_reduce, METH_NOARGS}, {0}}; @@ -497,42 +489,6 @@ static PyNumberMethods union_as_number = { .nb_or = _Py_union_type_or, // Add __or__ function }; -static PyObject * -union_new(PyTypeObject *type, PyObject *args, PyObject *kwds) -{ - if (!_PyArg_NoKeywords("Union", kwds)) { - return NULL; - } - if (!_PyArg_CheckPositional("Union", PyTuple_GET_SIZE(args), 1, 1)) { - return NULL; - } - - return _Py_Union(PyTuple_GET_ITEM(args, 0)); -} - -static const char* const cls_attrs[] = { - "__name__", - "__qualname__", - "__module__", - NULL, -}; - -static PyObject * -union_getattro(PyObject *self, PyObject *name) -{ - unionobject *alias = (unionobject *)self; - - for (const char * const *p = cls_attrs; ; p++) { - if (*p == NULL) { - break; - } - if (_PyUnicode_EqualToASCIIString(name, *p)) { - return PyObject_GetAttr((PyObject *) Py_TYPE(alias), name); - } - } - return PyObject_GenericGetAttr(self, name); -} - PyTypeObject _Py_UnionType = { PyVarObject_HEAD_INIT(&PyType_Type, 0) .tp_name = "types.Union", @@ -546,7 +502,7 @@ PyTypeObject _Py_UnionType = { .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, .tp_traverse = union_traverse, .tp_hash = union_hash, - .tp_getattro = union_getattro, + .tp_getattro = PyObject_GenericGetAttr, .tp_members = union_members, .tp_methods = union_methods, .tp_richcompare = union_richcompare, @@ -554,7 +510,6 @@ PyTypeObject _Py_UnionType = { .tp_as_number = &union_as_number, .tp_repr = union_repr, .tp_getset = union_properties, - .tp_new = union_new, }; PyObject * From a00bfb4df9ae218b651c809846f43389908d70f9 Mon Sep 17 00:00:00 2001 From: Yurii Karabas <1998uriyyo@gmail.com> Date: Wed, 7 Jul 2021 20:54:53 +0300 Subject: [PATCH 04/10] Remove test_union_pickle --- Lib/test/test_types.py | 10 ---------- Lib/typing.py | 2 +- 2 files changed, 1 insertion(+), 11 deletions(-) diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py index aa635bf012aecc..7f7ce86ff08ef3 100644 --- a/Lib/test/test_types.py +++ b/Lib/test/test_types.py @@ -13,9 +13,6 @@ import weakref import typing - -T = typing.TypeVar('T') - class Example: pass @@ -679,13 +676,6 @@ def test_union_parameter_chaining(self): self.assertEqual((list[T] | list[S]).__parameters__, (T, S)) self.assertEqual((list[T] | list[S])[int, T], list[int] | list[T]) - def test_union_pickle(self): - alias = list[T] | int - s = pickle.dumps(alias) - loaded = pickle.loads(s) - self.assertEqual(alias.__args__, loaded.__args__) - self.assertEqual(alias.__parameters__, loaded.__parameters__) - def test_or_type_operator_with_forward(self): T = typing.TypeVar('T') ForwardAfter = T | 'Forward' diff --git a/Lib/typing.py b/Lib/typing.py index b5e361c1e6bc57..54affef44d2f77 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -1781,7 +1781,7 @@ def _strip_annotations(t): stripped_args = tuple(_strip_annotations(a) for a in t.__args__) if stripped_args == t.__args__: return t - return types.Union(stripped_args) + return functools.reduce(operator.or_, stripped_args) return t From 4ad9b66853dc691799de8ebccb30e33b844e78bc Mon Sep 17 00:00:00 2001 From: Yurii Karabas <1998uriyyo@gmail.com> Date: Wed, 7 Jul 2021 21:02:20 +0300 Subject: [PATCH 05/10] Add more tests to cover types.Union --- Lib/test/test_typing.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 3539addebf3438..8288ebe30ac9e2 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -2476,6 +2476,12 @@ def foo(a: Union['T']): self.assertEqual(get_type_hints(foo, globals(), locals()), {'a': Union[T]}) + def foo(a: tuple[ForwardRef('T')] | int): + pass + + self.assertEqual(get_type_hints(foo, globals(), locals()), + {'a': tuple[T] | int}) + def test_tuple_forward(self): def foo(a: Tuple['T']): From 140a41a813e76f27b9d3156085fd09790577154c Mon Sep 17 00:00:00 2001 From: Yurii Karabas <1998uriyyo@gmail.com> Date: Thu, 8 Jul 2021 20:49:07 +0300 Subject: [PATCH 06/10] Update Misc/NEWS.d/next/Core and Builtins/2021-07-06-22-22-15.bpo-44490.BJxPbZ.rst Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com> --- .../2021-07-06-22-22-15.bpo-44490.BJxPbZ.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-07-06-22-22-15.bpo-44490.BJxPbZ.rst b/Misc/NEWS.d/next/Core and Builtins/2021-07-06-22-22-15.bpo-44490.BJxPbZ.rst index 616327ea276f6f..d49181cd82c818 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2021-07-06-22-22-15.bpo-44490.BJxPbZ.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2021-07-06-22-22-15.bpo-44490.BJxPbZ.rst @@ -1,2 +1,3 @@ -Fix ``typing`` module issues with ``types.Union`` parameters and arguments -resolving. Patch provided by Yurii Karabas. +:mod:`typing` now searches for type parameters in ``types.Union`` objects. +``get_type_hints`` will also properly resolve annotations with nested +``types.Union`` objects. Patch provided by Yurii Karabas. From 6826d72d1fe6ade0c2cfa7b775ee5a0b43a11d30 Mon Sep 17 00:00:00 2001 From: Yurii Karabas <1998uriyyo@gmail.com> Date: Thu, 8 Jul 2021 20:50:09 +0300 Subject: [PATCH 07/10] Update Lib/test/test_typing.py Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com> --- Lib/test/test_typing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 8288ebe30ac9e2..1fa3853736d3c4 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -1789,7 +1789,7 @@ def test_extended_generic_rules_repr(self): def test_generic_forward_ref(self): def foobar(x: List[List['CC']]): ... def foobar2(x: list[list[ForwardRef('CC')]]): ... - def foobar3(x: list[ForwardRef('CC')] | int): ... + def foobar3(x: list[ForwardRef('CC | int')] | int): ... class CC: ... self.assertEqual( get_type_hints(foobar, globals(), locals()), From 8b36d36a16daaa2d68915d7d7eeab133218ff989 Mon Sep 17 00:00:00 2001 From: Yurii Karabas <1998uriyyo@gmail.com> Date: Thu, 8 Jul 2021 20:54:26 +0300 Subject: [PATCH 08/10] Update typing tests --- Lib/test/test_typing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 1fa3853736d3c4..6e6be3be3af35b 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -1801,7 +1801,7 @@ class CC: ... ) self.assertEqual( get_type_hints(foobar3, globals(), locals()), - {'x': list[CC] | int} + {'x': list[CC | int] | int} ) T = TypeVar('T') From 40811137668de173799ca40670bacdb3562a92da Mon Sep 17 00:00:00 2001 From: Yurii Karabas <1998uriyyo@gmail.com> Date: Thu, 8 Jul 2021 21:01:53 +0300 Subject: [PATCH 09/10] Update tests --- Lib/test/ann_module.py | 2 ++ Lib/test/test_typing.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Lib/test/ann_module.py b/Lib/test/ann_module.py index 0567d6de1b6478..3135d8f86659c7 100644 --- a/Lib/test/ann_module.py +++ b/Lib/test/ann_module.py @@ -58,3 +58,5 @@ def dec(func): def wrapper(*args, **kwargs): return func(*args, **kwargs) return wrapper + +core_union: int | float diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 6e6be3be3af35b..ad292848179edf 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -2863,7 +2863,7 @@ def test_get_type_hints_from_various_objects(self): gth(None) def test_get_type_hints_modules(self): - ann_module_type_hints = {1: 2, 'f': Tuple[int, int], 'x': int, 'y': str} + ann_module_type_hints = {1: 2, 'f': Tuple[int, int], 'x': int, 'y': str, 'core_union': int | float} self.assertEqual(gth(ann_module), ann_module_type_hints) self.assertEqual(gth(ann_module2), {}) self.assertEqual(gth(ann_module3), {}) From b92ba0d9c73df7c8fea504fee4a19acfe020e3b0 Mon Sep 17 00:00:00 2001 From: Yurii Karabas <1998uriyyo@gmail.com> Date: Thu, 8 Jul 2021 22:39:19 +0300 Subject: [PATCH 10/10] Update tests --- Lib/test/ann_module.py | 2 +- Lib/test/test_grammar.py | 2 +- Lib/test/test_typing.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/test/ann_module.py b/Lib/test/ann_module.py index 3135d8f86659c7..5081e6b58345a9 100644 --- a/Lib/test/ann_module.py +++ b/Lib/test/ann_module.py @@ -59,4 +59,4 @@ def wrapper(*args, **kwargs): return func(*args, **kwargs) return wrapper -core_union: int | float +u: int | float diff --git a/Lib/test/test_grammar.py b/Lib/test/test_grammar.py index c0820fd63683f2..b6c457465609af 100644 --- a/Lib/test/test_grammar.py +++ b/Lib/test/test_grammar.py @@ -473,7 +473,7 @@ class CC(metaclass=CMeta): def test_var_annot_module_semantics(self): self.assertEqual(test.__annotations__, {}) self.assertEqual(ann_module.__annotations__, - {1: 2, 'x': int, 'y': str, 'f': typing.Tuple[int, int]}) + {1: 2, 'x': int, 'y': str, 'f': typing.Tuple[int, int], 'u': int | float}) self.assertEqual(ann_module.M.__annotations__, {'123': 123, 'o': type}) self.assertEqual(ann_module2.__annotations__, {}) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index ad292848179edf..95803d5aadcc77 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -2863,7 +2863,7 @@ def test_get_type_hints_from_various_objects(self): gth(None) def test_get_type_hints_modules(self): - ann_module_type_hints = {1: 2, 'f': Tuple[int, int], 'x': int, 'y': str, 'core_union': int | float} + ann_module_type_hints = {1: 2, 'f': Tuple[int, int], 'x': int, 'y': str, 'u': int | float} self.assertEqual(gth(ann_module), ann_module_type_hints) self.assertEqual(gth(ann_module2), {}) self.assertEqual(gth(ann_module3), {})