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: Provide an API to help debug super().__getattribute__ results
Type: Stage:
Components: Versions: Python 3.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: doughellmann, ncoghlan, rhettinger, serhiy.storchaka
Priority: normal Keywords:

Created on 2016-05-05 01:43 by ncoghlan, last changed 2022-04-11 14:58 by admin.

Messages (6)
msg264869 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2016-05-05 01:43
Prompted by a discussion with Doug Hellmann, I'd like to suggest exposing some of the descriptor MRO walking logic as "operator.get_providing_type".

Specifically, where "getattr(obj, 'attrname')" performs a full attribute lookup and "getattr(type(obj), 'attrname'), performs a type-only attribute lookup, "operator.get_providing_type(obj, 'attrname')" would perform an attribute lookup in the vein of _PyType_Lookup, but report the *type* containing the relevant descriptor, rather than fulling resolving the descriptor protocol.

This would be intended primarily as a debugging aid, for cases where a super() call is reporting an error, but it isn't clear which particular mixin class is failing, and the traceback isn't providing sufficient detail to figure it out (e.g. due to C functions in the call chain).
msg264875 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-05-05 04:43
Could you please provide an example of using get_providing_type? If this is intended primarily as a debugging aid, I think the inspect module is better place than the operator module. I don't know wherever this functionality is already provided by the inspect module.
msg264877 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2016-05-05 05:41
The main reason for suggesting operator is that it needs to be in an extension module if it's going to share the underlying lookup algorithm used by super().__getattribute__ rather than reimplementing it.

I'm not particularly concerned about the exact API name though, so I changed the title of the issue to explain the problem to be solved, rather than to propose a particular solution.

Doug, could you provide a specific example of the cryptic traceback that started the discussion?
msg264880 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2016-05-05 06:42
Actually, since this is intended primarily for debugging:

Doug, do you actually need the type information? Or would combining inspect.getsourcefile() and inspect.getlines() with super().__init__ to locate the source code for the offending method implementation be sufficient?

If that's the case, then the only benefit of this API would be to extend such debugging to extension module types in addition to regular Python classes.
msg264907 - (view) Author: Doug Hellmann (doughellmann) * (Python committer) Date: 2016-05-05 13:41
The specific case I have right now is with a large code base written by someone else who is seeing a TypeError when they call super(their-local-class, self).__init__() because whatever class super() is returning is expecting arguments to __init__(). The TypeError thrown doesn't say what is being called (that's a separate issue) so I was trying to debug. The __init__() method of super() is a special wrapper, so there isn't any way using only super() to access the class it has decided to use. I'm currently walking the MRO by hand to find the class that's causing the problem. I expected to be able to do something like super(their-local-class, self).give_me_the_type_with('__init__') and get back a class.
msg264909 - (view) Author: Doug Hellmann (doughellmann) * (Python committer) Date: 2016-05-05 15:24
@Nick - Being able to get to the source is ok, but if I can get the actual type I can always get the source from that. I don't need the actual type object except to know what it is, and it seems more flexible to return the class object than a name or other reference to it that I then have to turn into the class object if I really did need it.
History
Date User Action Args
2022-04-11 14:58:30adminsetgithub: 71148
2016-05-05 17:46:22rhettingersetnosy: + rhettinger
2016-05-05 15:24:51doughellmannsetmessages: + msg264909
2016-05-05 13:41:35doughellmannsetmessages: + msg264907
2016-05-05 06:42:31ncoghlansetmessages: + msg264880
2016-05-05 05:41:16ncoghlansetmessages: + msg264877
title: Add operator.get_providing_type -> Provide an API to help debug super().__getattribute__ results
2016-05-05 04:43:15serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg264875
2016-05-05 01:43:49ncoghlansetversions: + Python 3.6
2016-05-05 01:43:34ncoghlancreate