Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions src/test_typing_extensions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sys
import os
import abc
import io
import contextlib
import collections
from collections import defaultdict
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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})
Expand Down Expand Up @@ -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):
Expand All @@ -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])
Expand Down Expand Up @@ -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):
Expand Down