commit 850c6e27a12858ad3e4de1a019df496d822f1dd6 Author: Gregory M. Turner Date: Wed Dec 3 04:50:19 2014 -0800 Lib/test/test_metaclass.py: virtual sub-metas Test capturing that type machinery ignores __subclasscheck__ and __instancecheck__ when determining the effective metaclass. Signed-off-by: Gregory M. Turner diff --git a/Lib/test/test_metaclass.py b/Lib/test/test_metaclass.py index e6fe20a..e0f0eac 100644 --- a/Lib/test/test_metaclass.py +++ b/Lib/test/test_metaclass.py @@ -246,6 +246,39 @@ Test failures in looking up the __prepare__ method work. [...] test.test_metaclass.ObscureException +Virtual types ought not to affect metaclass selection. + + >>> class VMA(type): pass + >>> class MMA(type): + ... def __subclasscheck__(m, C): + ... return (issubclass(C, VMA) or m is C + ... ) if m is MA else super().__subclasscheck__(C) + ... def __instancecheck__(m, i): + ... return (isinstance(i, VMA) or m is type(i) + ... ) if m is MA else super().__instancecheck__(i) + >>> class MA(type, metaclass=MMA): pass + >>> class SMA(MA): pass + >>> class A(metaclass=MA): pass + >>> try: + ... class B(A, metaclass=VMA): pass + ... except TypeError: + ... print('TypeError (tl;dr)') + TypeError (tl;dr) + >>> issubclass(VMA, MA) + True + >>> class C(type, metaclass=SMA): pass + >>> class D(A, C): pass + >>> type(D) + + >>> type(A) + + >>> type(C) + + >>> issubclass(SMA, MA) + False + >>> issubclass(MA, SMA) + False + """ import sys