This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: isinstance raises TypeError for metaclass with metaclass=ABCMeta
Type: behavior Stage: patch review
Components: Interpreter Core Versions: Python 3.11, Python 3.10, Python 3.9
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Jeffrey.Kintscher, donovick, emanspeaks, iritkatriel, levkivskyi, myungsekyo, rhettinger, stutzbach
Priority: normal Keywords: patch

Created on 2019-05-11 02:29 by emanspeaks, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 27648 closed iritkatriel, 2021-08-07 00:47
Messages (3)
msg342152 - (view) Author: Randy Eckman (emanspeaks) Date: 2019-05-11 02:29
>>> from abc import ABCMeta
>>> class AbstractMeta(type, metaclass=ABCMeta): pass
>>> class Meta1(AbstractMeta): pass
>>> class Meta2(AbstractMeta): pass
>>> class ClassA(metaclass=Meta1): pass
>>> isinstance(ClassA, Meta2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\miniconda\miniconda3\lib\", line 139, in __instancecheck__
    return _abc_instancecheck(cls, instance)
TypeError: __subclasscheck__() takes exactly one argument (0 given)

This is driven by the fact that the metaclass of AbstractMeta is ABCMeta; if I change its metaclass to something else that does not contain ABCMeta in the inheritance chain, isinstance returns False as expected.

Possibly related to Issue 2325?
msg342870 - (view) Author: Ivan Levkivskyi (levkivskyi) * (Python committer) Date: 2019-05-19 21:08
I think this is related to how `__subclasscheck__` is implemented in `ABCMeta`. I just checked this also happens in Python 3.6 (i.e. it is not something specific to the C version introduced in Python 3.7).
msg399162 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-08-07 00:53
PR27648 fixes this, but it also breaks test_abc.test_issubclass_bad_arguments so it's not quite right.

I don't know this area, but I think the problem is in the place the PR changes, and that the issue is due to this difference in the type of __subclasses__ between C and Meta2:

>>> class C: pass
>>> from abc import ABCMeta
>>> class AbstractMeta(type, metaclass=ABCMeta): pass
>>> class Meta2(AbstractMeta): pass
>>> C.__subclasses__
<built-in method __subclasses__ of type object at 0x7f96bf5040b0>
>>> Meta2.__subclasses__
<method '__subclasses__' of 'type' objects>
Date User Action Args
2022-04-11 14:59:15adminsetgithub: 81062
2021-08-07 00:53:04iritkatrielsetmessages: + msg399162
versions: + Python 3.9, Python 3.10, Python 3.11, - Python 3.7
2021-08-07 00:47:26iritkatrielsetkeywords: + patch
nosy: + iritkatriel

pull_requests: + pull_request26142
stage: patch review
2019-07-03 09:37:46myungsekyosetnosy: + myungsekyo
2019-07-01 19:46:22donovicksetnosy: + donovick
2019-05-28 08:37:36Jeffrey.Kintschersetnosy: + Jeffrey.Kintscher
2019-05-19 21:08:36levkivskyisetmessages: + msg342870
2019-05-19 09:20:12levkivskyisetnosy: + levkivskyi
2019-05-11 06:05:17SilentGhostsetnosy: + rhettinger, stutzbach
type: behavior
2019-05-11 02:29:04emanspeakscreate