On 2019-12-31 3:56 p.m., Andrew Barnert wrote:
On Dec 31, 2019, at 09:43, Soni L. <fakedme...@gmail.com> wrote:
>
> I would like this code to work, but currently python ignores __subclasscheck__ in many places where it checks for subclasses:
>
> class MM(type):
> def __subclasscheck__(self, subclass):
> return issubclass(subclass, type)
>
>
> class M(type, metaclass=MM):
> pass
>
>
> class N(type):
> pass
>
>
> class C(metaclass=M):
> pass
>
>
> class D(metaclass=N):
> pass
>
>
> class E(C, D, metaclass=N):
> pass
Is the problem you’re trying to solve here, that you want to be able to
override the metaclass conflict check in class definitions, so you can allow
this?
If so, I think it makes sense. N is (trivially) a subclass of the N (the
metaclass of D), and you’re making it a virtual subclass (via MM) of M (the
metaclass of C). So there actually is no conflict if you take virtual
subclasses into account; N is unambiguously the most derived metaclass.
(I suppose it would be ambiguous again if you also made M a (virtual or direct)
subclass of N, but that’s probably consulting-adults territory: don’t do that,
and if you do, the fact that the error message isn’t as meaningful as it could
be isn’t a huge deal.)
But you said “many places”. Is there a wider class of places, that includes the
metaclass conflict check, where virtual subclassing is ignored but (you think)
shouldn’t be?
In the documentation for metaclass stuff, Python mentions "subtype"
without referring to issubtype. I assume other parts of the docs also do
similar things, but I haven't checked.
However, I can imagine cases where virtual subtypes would cause
problems. Namely, the C API and builtins. But, more importantly, it
wouldn't work there anyway since you can't monkeypatch __instancecheck__
/ __subclasscheck__ of built-ins in the first place. So, in practice, I
can't imagine a case where virtual subclasses would be a problem.
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/python-ideas@python.org/message/7RXJ7PZOCT6R7SPEW2KXYLJIR6IR5LVW/
Code of Conduct: http://python.org/psf/codeofconduct/