Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ca4b252

Browse files
committed
Issue #28556: two more small upstream changes by Ivan Levkivskyi (#329, #330)
1 parent c851817 commit ca4b252

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

Lib/test/test_typing.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ def assertNotIsSubclass(self, cls, class_or_tuple, msg=None):
4545
message += ' : %s' % msg
4646
raise self.failureException(message)
4747

48+
def clear_caches(self):
49+
for f in typing._cleanups:
50+
f()
51+
4852

4953
class Employee:
5054
pass
@@ -509,6 +513,13 @@ def test_reversible(self):
509513
def test_protocol_instance_type_error(self):
510514
with self.assertRaises(TypeError):
511515
isinstance(0, typing.SupportsAbs)
516+
class C1(typing.SupportsInt):
517+
def __int__(self) -> int:
518+
return 42
519+
class C2(C1):
520+
pass
521+
c = C2()
522+
self.assertIsInstance(c, C1)
512523

513524

514525
class GenericTests(BaseTestCase):
@@ -748,8 +759,12 @@ def foobar(x: List[List['CC']]): ...
748759
class CC: ...
749760
self.assertEqual(get_type_hints(foobar, globals(), locals()), {'x': List[List[CC]]})
750761
T = TypeVar('T')
751-
def barfoo(x: Tuple[T, ...]): ...
752-
self.assertIs(get_type_hints(barfoo, globals(), locals())['x'], Tuple[T, ...])
762+
AT = Tuple[T, ...]
763+
def barfoo(x: AT): ...
764+
self.assertIs(get_type_hints(barfoo, globals(), locals())['x'], AT)
765+
CT = Callable[..., List[T]]
766+
def barfoo2(x: CT): ...
767+
self.assertIs(get_type_hints(barfoo2, globals(), locals())['x'], CT)
753768

754769
def test_extended_generic_rules_subclassing(self):
755770
class T1(Tuple[T, KT]): ...
@@ -800,6 +815,8 @@ def test_fail_with_bare_generic(self):
800815

801816
def test_type_erasure_special(self):
802817
T = TypeVar('T')
818+
# this is the only test that checks type caching
819+
self.clear_caches()
803820
class MyTup(Tuple[T, T]): ...
804821
self.assertIs(MyTup[int]().__class__, MyTup)
805822
self.assertIs(MyTup[int]().__orig_class__, MyTup[int])

Lib/typing.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,6 +1503,8 @@ class _ProtocolMeta(GenericMeta):
15031503
"""
15041504

15051505
def __instancecheck__(self, obj):
1506+
if _Protocol not in self.__bases__:
1507+
return super().__instancecheck__(obj)
15061508
raise TypeError("Protocols cannot be used with isinstance().")
15071509

15081510
def __subclasscheck__(self, cls):

0 commit comments

Comments
 (0)