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

Method test_non_protocol_subclasses

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

Source from the content-addressed store, hash-verified

3842 self.assertNotIsInstance(C(), P)
3843
3844 def test_non_protocol_subclasses(self):
3845 class P(Protocol):
3846 x = 1
3847
3848 @runtime_checkable
3849 class PR(Protocol):
3850 def meth(self): pass
3851
3852 class NonP(P):
3853 x = 1
3854
3855 class NonPR(PR): pass
3856
3857 class C(metaclass=abc.ABCMeta):
3858 x = 1
3859
3860 class D(metaclass=abc.ABCMeta):
3861 def meth(self): pass
3862
3863 self.assertNotIsInstance(C(), NonP)
3864 self.assertNotIsInstance(D(), NonPR)
3865 self.assertNotIsSubclass(C, NonP)
3866 self.assertNotIsSubclass(D, NonPR)
3867 self.assertIsInstance(NonPR(), PR)
3868 self.assertIsSubclass(NonPR, PR)
3869
3870 self.assertNotIn("__protocol_attrs__", vars(NonP))
3871 self.assertNotIn("__protocol_attrs__", vars(NonPR))
3872 self.assertNotIn("__non_callable_proto_members__", vars(NonP))
3873 self.assertNotIn("__non_callable_proto_members__", vars(NonPR))
3874
3875 self.assertEqual(get_protocol_members(P), {"x"})
3876 self.assertEqual(get_protocol_members(PR), {"meth"})
3877
3878 # the returned object should be immutable,
3879 # and should be a different object to the original attribute
3880 # to prevent users from (accidentally or deliberately)
3881 # mutating the attribute on the original class
3882 self.assertIsInstance(get_protocol_members(P), frozenset)
3883 self.assertIsNot(get_protocol_members(P), P.__protocol_attrs__)
3884 self.assertIsInstance(get_protocol_members(PR), frozenset)
3885 self.assertIsNot(get_protocol_members(PR), P.__protocol_attrs__)
3886
3887 acceptable_extra_attrs = {
3888 '_is_protocol', '_is_runtime_protocol', '__typing_is_deprecated_inherited_runtime_protocol__',
3889 '__parameters__', '__init__', '__annotations__', '__subclasshook__', '__annotate__',
3890 '__annotations_cache__', '__annotate_func__',
3891 }
3892 self.assertLessEqual(vars(NonP).keys(), vars(C).keys() | acceptable_extra_attrs)
3893 self.assertLessEqual(
3894 vars(NonPR).keys(), vars(D).keys() | acceptable_extra_attrs
3895 )
3896
3897 def test_custom_subclasshook(self):
3898 class P(Protocol):

Callers

nothing calls this directly

Calls 13

get_protocol_membersFunction · 0.90
NonPRClass · 0.85
assertNotIsInstanceMethod · 0.80
assertNotIsSubclassMethod · 0.80
assertIsInstanceMethod · 0.80
assertIsSubclassMethod · 0.80
assertNotInMethod · 0.80
assertIsNotMethod · 0.80
assertLessEqualMethod · 0.80
CClass · 0.70
DClass · 0.70
assertEqualMethod · 0.45

Tested by

no test coverage detected