From e8f6c3d5d484ed02905c09c647dcae34c0f3c835 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Fri, 12 May 2023 10:01:43 +0300 Subject: [PATCH 1/2] gh-104415: Fix refleak tests for `typing.ByteString` deprecation --- Lib/test/test_typing.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 3422dc1ed3f5f8..c9bfd2b012dc00 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -6004,8 +6004,9 @@ def test_mutablesequence(self): self.assertNotIsInstance((), typing.MutableSequence) def test_bytestring(self): + _typing = import_fresh_module('typing') with self.assertWarns(DeprecationWarning): - from typing import ByteString + ByteString = _typing.ByteString with self.assertWarns(DeprecationWarning): self.assertIsInstance(b'', ByteString) with self.assertWarns(DeprecationWarning): @@ -6013,7 +6014,7 @@ def test_bytestring(self): with self.assertWarns(DeprecationWarning): class Foo(ByteString): ... with self.assertWarns(DeprecationWarning): - class Bar(ByteString, typing.Awaitable): ... + class Bar(ByteString, _typing.Awaitable): ... def test_list(self): self.assertIsSubclass(list, typing.List) From 023a6150942772c433b6c8e5f68b32652e4a9d5e Mon Sep 17 00:00:00 2001 From: sobolevn Date: Fri, 12 May 2023 14:00:28 +0300 Subject: [PATCH 2/2] Address review --- Lib/test/test_typing.py | 5 ++--- Lib/typing.py | 9 +++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index c9bfd2b012dc00..3422dc1ed3f5f8 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -6004,9 +6004,8 @@ def test_mutablesequence(self): self.assertNotIsInstance((), typing.MutableSequence) def test_bytestring(self): - _typing = import_fresh_module('typing') with self.assertWarns(DeprecationWarning): - ByteString = _typing.ByteString + from typing import ByteString with self.assertWarns(DeprecationWarning): self.assertIsInstance(b'', ByteString) with self.assertWarns(DeprecationWarning): @@ -6014,7 +6013,7 @@ def test_bytestring(self): with self.assertWarns(DeprecationWarning): class Foo(ByteString): ... with self.assertWarns(DeprecationWarning): - class Bar(ByteString, _typing.Awaitable): ... + class Bar(ByteString, typing.Awaitable): ... def test_list(self): self.assertIsSubclass(list, typing.List) diff --git a/Lib/typing.py b/Lib/typing.py index bf7bd241972a6b..513d4d96dd6e1d 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -3586,3 +3586,12 @@ def __getattr__(attr): ) return ByteString raise AttributeError(f"module 'typing' has no attribute {attr!r}") + + +def _remove_cached_ByteString_from_globals(): + try: + del globals()["ByteString"] + except KeyError: + pass + +_cleanups.append(_remove_cached_ByteString_from_globals)