Skip to content

Commit 05d0807

Browse files
committed
Add failing test for custom isinstance check
1 parent 5fe139c commit 05d0807

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

Lib/test/test_types.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,21 @@ def __subclasscheck__(cls, sub):
909909
self.assertIsSubclass(int, x)
910910
self.assertRaises(ZeroDivisionError, issubclass, list, x)
911911

912+
def test_custom_instancecheck(self):
913+
class CustomIsInstanceMeta(type):
914+
def __instancecheck__(cls, instance):
915+
return type(instance) is int
916+
917+
class CustomIsInstance(metaclass=CustomIsInstanceMeta):
918+
...
919+
920+
class CustomIsInstanceSubclass(CustomIsInstance):
921+
...
922+
923+
self.assertTrue(isinstance(5, CustomIsInstance))
924+
self.assertFalse(isinstance(CustomIsInstance(), CustomIsInstance))
925+
self.assertFalse(isinstance(CustomIsInstanceSubclass(), CustomIsInstance))
926+
912927
def test_or_type_operator_with_TypeVar(self):
913928
TV = typing.TypeVar('T')
914929
self.assertEqual(TV | str, typing.Union[TV, str])

0 commit comments

Comments
 (0)