Skip to content

Commit b5cc9cd

Browse files
committed
Add more failing tests (thanks sunmy2019!)
1 parent e114fe9 commit b5cc9cd

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

Lib/test/test_typing.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2672,6 +2672,42 @@ def __init__(self) -> None:
26722672
with self.assertRaises(TypeError):
26732673
issubclass(Eggs, Spam)
26742674

2675+
def test_no_weird_caching_with_issubclass_after_isinstance2(self):
2676+
@runtime_checkable
2677+
class Spam(Protocol):
2678+
x: int
2679+
2680+
class Eggs: ...
2681+
2682+
# gh-104555: ABCMeta might cache the result of this isinstance check
2683+
# if we called super().__instancecheck__ in the wrong place
2684+
# in _ProtocolMeta.__instancecheck__...
2685+
self.assertNotIsInstance(Eggs(), Spam)
2686+
2687+
# ...and if it did, then TypeError wouldn't be raised here!
2688+
with self.assertRaises(TypeError):
2689+
issubclass(Eggs, Spam)
2690+
2691+
def test_no_weird_caching_with_issubclass_after_isinstance3(self):
2692+
@runtime_checkable
2693+
class Spam(Protocol):
2694+
x: int
2695+
2696+
class Eggs:
2697+
def __getattr__(self, attr):
2698+
if attr == "x":
2699+
return 42
2700+
raise AttributeError(attr)
2701+
2702+
# gh-104555: ABCMeta might cache the result of this isinstance check
2703+
# if we called super().__instancecheck__ in the wrong place
2704+
# in _ProtocolMeta.__instancecheck__...
2705+
self.assertNotIsInstance(Eggs(), Spam)
2706+
2707+
# ...and if it did, then TypeError wouldn't be raised here!
2708+
with self.assertRaises(TypeError):
2709+
issubclass(Eggs, Spam)
2710+
26752711
def test_no_weird_caching_with_issubclass_after_isinstance_pep695(self):
26762712
@runtime_checkable
26772713
class Spam[T](Protocol):

0 commit comments

Comments
 (0)