From 80784b3ffc6f5ba535cb4808b9265ae550c8e200 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Sun, 9 Apr 2023 21:57:11 +0100 Subject: [PATCH 1/2] Suppress DeprecationWarnings and `print`s when running tests --- src/test_typing_extensions.py | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/test_typing_extensions.py b/src/test_typing_extensions.py index ab0cc256..6dda9236 100644 --- a/src/test_typing_extensions.py +++ b/src/test_typing_extensions.py @@ -1,6 +1,7 @@ import sys import os import abc +import io import contextlib import collections from collections import defaultdict @@ -62,6 +63,13 @@ def assertNotIsSubclass(self, cls, class_or_tuple, msg=None): message += f' : {msg}' raise self.failureException(message) + @contextlib.contextmanager + def assertWarnsIf(self, condition: bool, expected_warning: type[Warning]): + with contextlib.ExitStack() as stack: + if condition: + stack.enter_context(self.assertWarns(expected_warning)) + yield + class Employee: pass @@ -238,8 +246,9 @@ class A: with self.assertWarnsRegex(DeprecationWarning, "A will go away soon"): A() - with self.assertRaises(TypeError): - A(42) + with self.assertWarnsRegex(DeprecationWarning, "A will go away soon"): + with self.assertRaises(TypeError): + A(42) @deprecated("HasInit will go away soon") class HasInit: @@ -1959,7 +1968,8 @@ def test_basics_iterable_syntax(self): self.assertEqual(Emp.__total__, True) def test_basics_keywords_syntax(self): - Emp = TypedDict('Emp', name=str, id=int) + with self.assertWarnsIf(sys.version_info >= (3, 11), DeprecationWarning): + Emp = TypedDict('Emp', name=str, id=int) self.assertIsSubclass(Emp, dict) self.assertIsSubclass(Emp, typing.MutableMapping) self.assertNotIsSubclass(Emp, collections.abc.Sequence) @@ -1974,8 +1984,9 @@ def test_basics_keywords_syntax(self): self.assertEqual(Emp.__total__, True) def test_typeddict_special_keyword_names(self): - TD = TypedDict("TD", cls=type, self=object, typename=str, _typename=int, - fields=list, _fields=dict) + with self.assertWarnsIf(sys.version_info >= (3, 11), DeprecationWarning): + TD = TypedDict("TD", cls=type, self=object, typename=str, _typename=int, + fields=list, _fields=dict) self.assertEqual(TD.__name__, 'TD') self.assertEqual(TD.__annotations__, {'cls': type, 'self': object, 'typename': str, '_typename': int, 'fields': list, '_fields': dict}) @@ -2044,7 +2055,7 @@ def test_py36_class_syntax_usage(self): def test_pickle(self): global EmpD # pickle wants to reference the class by name - EmpD = TypedDict('EmpD', name=str, id=int) + EmpD = TypedDict('EmpD', {"name": str, "id": int}) jane = EmpD({'name': 'jane', 'id': 37}) point = Point2DGeneric(a=5.0, b=3.0) for proto in range(pickle.HIGHEST_PROTOCOL + 1): @@ -2066,7 +2077,7 @@ def test_pickle(self): self.assertEqual(Point2DGenericNew({'a': 5.0, 'b': 3.0}), point) def test_optional(self): - EmpD = TypedDict('EmpD', name=str, id=int) + EmpD = TypedDict('EmpD', {"name": str, "id": int}) self.assertEqual(typing.Optional[EmpD], typing.Union[None, EmpD]) self.assertNotEqual(typing.List[EmpD], typing.Tuple[EmpD]) @@ -3134,7 +3145,10 @@ def cached(self): ... class RevealTypeTests(BaseTestCase): def test_reveal_type(self): obj = object() - self.assertIs(obj, reveal_type(obj)) + + with contextlib.redirect_stderr(io.StringIO()) as stderr: + self.assertIs(obj, reveal_type(obj)) + self.assertEqual("Runtime type is 'object'", stderr.getvalue().strip()) class DataclassTransformTests(BaseTestCase): From 455326ba183b773b21949aba3cb372041060d5eb Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Sun, 9 Apr 2023 21:58:47 +0100 Subject: [PATCH 2/2] Update src/test_typing_extensions.py --- src/test_typing_extensions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test_typing_extensions.py b/src/test_typing_extensions.py index 6dda9236..21630ad0 100644 --- a/src/test_typing_extensions.py +++ b/src/test_typing_extensions.py @@ -64,7 +64,7 @@ def assertNotIsSubclass(self, cls, class_or_tuple, msg=None): raise self.failureException(message) @contextlib.contextmanager - def assertWarnsIf(self, condition: bool, expected_warning: type[Warning]): + def assertWarnsIf(self, condition: bool, expected_warning: Type[Warning]): with contextlib.ExitStack() as stack: if condition: stack.enter_context(self.assertWarns(expected_warning))