MCPcopy Index your code
hub / github.com/python/cpython / test_no_instantiation

Method test_no_instantiation

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

Source from the content-addressed store, hash-verified

2981 self.assertEqual(typing.get_protocol_members(NewGeneric), {'z'})
2982
2983 def test_no_instantiation(self):
2984 class P(Protocol): pass
2985
2986 with self.assertRaises(TypeError):
2987 P()
2988
2989 class C(P): pass
2990
2991 self.assertIsInstance(C(), C)
2992 with self.assertRaises(TypeError):
2993 C(42)
2994
2995 T = TypeVar('T')
2996
2997 class PG(Protocol[T]): pass
2998
2999 with self.assertRaises(TypeError):
3000 PG()
3001 with self.assertRaises(TypeError):
3002 PG[int]()
3003 with self.assertRaises(TypeError):
3004 PG[T]()
3005
3006 class CG(PG[T]): pass
3007
3008 self.assertIsInstance(CG[int](), CG)
3009 with self.assertRaises(TypeError):
3010 CG[int](42)
3011
3012 def test_protocol_defining_init_does_not_get_overridden(self):
3013 # check that P.__init__ doesn't get clobbered

Callers

nothing calls this directly

Calls 5

PGClass · 0.85
assertIsInstanceMethod · 0.80
PClass · 0.70
CClass · 0.70
assertRaisesMethod · 0.45

Tested by

no test coverage detected