MCPcopy Index your code
hub / github.com/RustPython/RustPython / test_protocols_isinstance

Method test_protocols_isinstance

Lib/test/test_typing.py:3464–3524  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

3462 issubclass(Eggs, Spam)
3463
3464 def test_protocols_isinstance(self):
3465 T = TypeVar('T')
3466
3467 @runtime_checkable
3468 class P(Protocol):
3469 def meth(x): ...
3470
3471 @runtime_checkable
3472 class PG(Protocol[T]):
3473 def meth(x): ...
3474
3475 @runtime_checkable
3476 class WeirdProto(Protocol):
3477 meth = str.maketrans
3478
3479 @runtime_checkable
3480 class WeirdProto2(Protocol):
3481 meth = lambda *args, **kwargs: None
3482
3483 class CustomCallable:
3484 def __call__(self, *args, **kwargs):
3485 pass
3486
3487 @runtime_checkable
3488 class WeirderProto(Protocol):
3489 meth = CustomCallable()
3490
3491 class BadP(Protocol):
3492 def meth(x): ...
3493
3494 class BadPG(Protocol[T]):
3495 def meth(x): ...
3496
3497 class C:
3498 def meth(x): ...
3499
3500 class C2:
3501 def __init__(self):
3502 self.meth = lambda: None
3503
3504 for klass in C, C2:
3505 for proto in P, PG, WeirdProto, WeirdProto2, WeirderProto:
3506 with self.subTest(klass=klass.__name__, proto=proto.__name__):
3507 self.assertIsInstance(klass(), proto)
3508
3509 no_subscripted_generics = "Subscripted generics cannot be used with class and instance checks"
3510
3511 with self.assertRaisesRegex(TypeError, no_subscripted_generics):
3512 isinstance(C(), PG[T])
3513 with self.assertRaisesRegex(TypeError, no_subscripted_generics):
3514 isinstance(C(), PG[C])
3515
3516 only_runtime_checkable_msg = (
3517 "Instance and class checks can only be used "
3518 "with @runtime_checkable protocols"
3519 )
3520
3521 with self.assertRaisesRegex(TypeError, only_runtime_checkable_msg):

Callers

nothing calls this directly

Calls 6

TypeVarClass · 0.85
isinstanceFunction · 0.85
subTestMethod · 0.80
assertIsInstanceMethod · 0.80
assertRaisesRegexMethod · 0.80
CClass · 0.70

Tested by

no test coverage detected