homepage

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.

classification
Title: super behaviour and abstract base classes (either implementation or documentation/error message is wrong)
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: closed Resolution: fixed
Dependencies: 23674 Superseder:
Assigned To: rhettinger Nosy List: Gerrit.Holl, docs@python, eryksun, martin.panter, miss-islington, rhettinger
Priority: normal Keywords: patch

Created on 2014年02月03日 15:36 by Gerrit.Holl, last changed 2022年04月11日 14:57 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 25175 merged rhettinger, 2021年04月04日 01:45
PR 25202 merged miss-islington, 2021年04月05日 19:48
Messages (7)
msg210141 - (view) Author: Gerrit Holl (Gerrit.Holl) * Date: 2014年02月03日 15:36
When using an abstract base class, super(type, obj) throws a TypeError stating "obj must be an instance (...) of type", even though isinstance(obj, type) returns True. I'm not sure what is supposed to happen here, but either the error message and the documentation for super would need to be reformulated, or there is an issue with the implementation, or I am misunderstanding something.
Python 3.3.3 (default, Dec 12 2013, 11:13:02) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numbers
>>> super(numbers.Number, 0)
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
TypeError: super(type, obj): obj must be an instance or subtype of type
>>> isinstance(0, numbers.Number)
True
msg235852 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2015年02月12日 19:28
Given super(cls, obj), cls needs to be somewhere in type(obj).__mro__. Thus the implementation checks PyType_IsSubtype instead of the more generic PyObject_IsSubclass. 
In this case int's MRO is unrelated to numbers.Number:
 >>> print(*int.__mro__, sep='\n')
 <class 'int'>
 <class 'object'>
It gets registered as a subclass via numbers.Integral.register(int).
 >>> print(*numbers.Integral._abc_registry)
 <class 'int'>
issubclass calls PyObject_IsSubclass, which uses the __subclasscheck__ API. In this case ABCMeta.__subclasscheck__ recursively checks the registry and caches the result to speed up future checks.
 >>> numbers.Number.__subclasscheck__(int)
 True
 >>> print(*numbers.Number._abc_cache)
 <class 'int'>
msg255686 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2015年12月02日 01:02
I am proposing some documentation changes in Issue 23674 which would address this.
msg387731 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2021年02月26日 17:16
The docs still need to clarified that isinstance(obj, type) is a necessary but not sufficient condition for success. It would also be helpful if the error message were less confusing in the case of registered subclasses such as numbers.Number.
msg387797 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021年02月28日 00:10
This is a bit out of date. The isinstance() docs now specify that the predicate tests for direct, indirect, and virtual inheritance. Likewise, the super() docs already specify that only the __mro__ is searched. So those docs are already precise.
Am thinking of adding a FAQ entry about when direct inheritance is required and include the OP's example. FWIW, this isn't really about super(). It affects any attribute lookup or any other property of inheritance. A true result from instance() doesn't imply that actual inheritance has occurred.
msg390254 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021年04月05日 19:48
New changeset 7bc25ec7276db2a81e7823671a74eeb8aa6b4542 by Raymond Hettinger in branch 'master':
bpo-20503: Show how isinstance() works with ABC registered classes. (GH-25175)
https://github.com/python/cpython/commit/7bc25ec7276db2a81e7823671a74eeb8aa6b4542
msg390258 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021年04月05日 20:11
New changeset 028d5286d4255195ba6715e1aeb4bffed6b0279e by Miss Islington (bot) in branch '3.9':
bpo-20503: Show how isinstance() works with ABC registered classes. (GH-25175) (GH-25202)
https://github.com/python/cpython/commit/028d5286d4255195ba6715e1aeb4bffed6b0279e
History
Date User Action Args
2022年04月11日 14:57:58adminsetgithub: 64702
2021年04月05日 20:12:39rhettingersetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2021年04月05日 20:11:57rhettingersetmessages: + msg390258
2021年04月05日 19:48:36miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request23941
2021年04月05日 19:48:32rhettingersetmessages: + msg390254
2021年04月04日 01:45:40rhettingersetkeywords: + patch
stage: patch review
pull_requests: + pull_request23917
2021年02月28日 00:10:50rhettingersetassignee: docs@python -> rhettinger
messages: + msg387797
2021年02月27日 02:21:00larrysetnosy: - larry
components: + Interpreter Core, - Documentation, Argument Clinic
2021年02月26日 17:37:30meanboy2727.7setnosy: + larry
type: enhancement -> behavior
components: + Argument Clinic
2021年02月26日 17:16:10eryksunsettype: enhancement
messages: + msg387731
versions: + Python 3.8, Python 3.9, Python 3.10, - Python 2.7, Python 3.4, Python 3.5, Python 3.6
2015年12月02日 01:02:16martin.pantersetassignee: docs@python
dependencies: + super() documentation isn't very clear
components: + Documentation, - Interpreter Core
versions: + Python 2.7, Python 3.6
nosy: + docs@python, martin.panter

messages: + msg255686
2015年02月12日 19:28:51eryksunsetnosy: + eryksun
messages: + msg235852
2015年02月12日 14:22:15BreamoreBoysetnosy: + rhettinger
versions: + Python 3.4, Python 3.5, - Python 3.3

title: super behavioru and abstract base classes (either implementation or documentation/error message is wrong) -> super behaviour and abstract base classes (either implementation or documentation/error message is wrong)
2014年02月03日 15:36:42Gerrit.Hollcreate

AltStyle によって変換されたページ (->オリジナル) /