From ff2c8f19cc659263a4dabd8cb2d02f1d50ade9f7 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sat, 12 Mar 2022 11:31:10 +0200 Subject: [PATCH 1/2] bpo-46981: Remove typing._TypingEmpty * get_args(Tuple[()]) returns now () instead of ((),). * Tuple[Unpack[Ts]][()] returns now the result equal to Tuple[()]. --- Lib/test/test_typing.py | 8 +++----- Lib/typing.py | 10 ---------- .../Library/2022-03-12-11-30-42.bpo-46981.ltWCxH.rst | 2 ++ 3 files changed, 5 insertions(+), 15 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2022-03-12-11-30-42.bpo-46981.ltWCxH.rst diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index b212b523048809..92ab8e9f4490d6 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -469,14 +469,12 @@ class G(Generic[Unpack[Ts]]): pass for A in G, Tuple: B = A[Unpack[Ts]] - if A != Tuple: - self.assertEqual(B[()], A[()]) + self.assertEqual(B[()], A[()]) self.assertEqual(B[float], A[float]) self.assertEqual(B[float, str], A[float, str]) C = List[A[Unpack[Ts]]] - if A != Tuple: - self.assertEqual(C[()], List[A[()]]) + self.assertEqual(C[()], List[A[()]]) self.assertEqual(C[float], List[A[float]]) self.assertEqual(C[float, str], List[A[float, str]]) @@ -4232,7 +4230,7 @@ class C(Generic[T]): pass self.assertEqual(get_args(Union[int, Callable[[Tuple[T, ...]], str]]), (int, Callable[[Tuple[T, ...]], str])) self.assertEqual(get_args(Tuple[int, ...]), (int, ...)) - self.assertEqual(get_args(Tuple[()]), ((),)) + self.assertEqual(get_args(Tuple[()]), ()) self.assertEqual(get_args(Annotated[T, 'one', 2, ['three']]), (T, 'one', 2, ['three'])) self.assertEqual(get_args(List), ()) self.assertEqual(get_args(Tuple), ()) diff --git a/Lib/typing.py b/Lib/typing.py index dd68e71db1558c..0871c555ca28b0 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -1219,7 +1219,6 @@ def __init__(self, origin, args, *, inst=True, name=None, if not isinstance(args, tuple): args = (args,) self.__args__ = tuple(... if a is _TypingEllipsis else - () if a is _TypingEmpty else a for a in args) self.__parameters__ = _collect_parameters(args) self._paramspec_tvars = _paramspec_tvars @@ -1502,8 +1501,6 @@ def __getitem_inner__(self, params): class _TupleType(_SpecialGenericAlias, _root=True): @_tp_cache def __getitem__(self, params): - if params == (): - return self.copy_with((_TypingEmpty,)) if not isinstance(params, tuple): params = (params,) if len(params) >= 2 and params[-1] is ...: @@ -1734,13 +1731,6 @@ def __init_subclass__(cls, *args, **kwargs): cls.__parameters__ = tuple(tvars) -class _TypingEmpty: - """Internal placeholder for () or []. Used by TupleMeta and CallableMeta - to allow empty list/tuple in specific places, without allowing them - to sneak in where prohibited. - """ - - class _TypingEllipsis: """Internal placeholder for ... (ellipsis).""" diff --git a/Misc/NEWS.d/next/Library/2022-03-12-11-30-42.bpo-46981.ltWCxH.rst b/Misc/NEWS.d/next/Library/2022-03-12-11-30-42.bpo-46981.ltWCxH.rst new file mode 100644 index 00000000000000..5d8c42af9f5dff --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-03-12-11-30-42.bpo-46981.ltWCxH.rst @@ -0,0 +1,2 @@ +``typing.get_args(typing.Tuple[()])`` returns now ``()`` instead of +``((),)``. From 301d2edbfcf4db4ed27ea8388261518788c92edd Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Wed, 16 Mar 2022 18:58:49 +0200 Subject: [PATCH 2/2] Update Misc/NEWS.d/next/Library/2022-03-12-11-30-42.bpo-46981.ltWCxH.rst Co-authored-by: Ken Jin --- .../next/Library/2022-03-12-11-30-42.bpo-46981.ltWCxH.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2022-03-12-11-30-42.bpo-46981.ltWCxH.rst b/Misc/NEWS.d/next/Library/2022-03-12-11-30-42.bpo-46981.ltWCxH.rst index 5d8c42af9f5dff..29f7c9376fe368 100644 --- a/Misc/NEWS.d/next/Library/2022-03-12-11-30-42.bpo-46981.ltWCxH.rst +++ b/Misc/NEWS.d/next/Library/2022-03-12-11-30-42.bpo-46981.ltWCxH.rst @@ -1,2 +1,2 @@ -``typing.get_args(typing.Tuple[()])`` returns now ``()`` instead of +``typing.get_args(typing.Tuple[()])`` now returns ``()`` instead of ``((),)``.