classification
Title: Improve handling of type.__abstractmethods__ descriptor
Type: behavior Stage: needs patch
Components: Interpreter Core Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Yaroslav.Halchenko, benjamin.peterson, daniel.urban, ncoghlan
Priority: normal Keywords:

Created on 2013-09-15 07:33 by ncoghlan, last changed 2021-11-29 17:56 by iritkatriel.

Messages (2)
msg197755 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2013-09-15 07:33
Issue 10006 pointed out that attempting to access object.__abstractmethods__ raises AttributeError. The solution was to ensure type.__abstractmethods__ *also* raises AttributeError.

This is thoroughly confusing, since the name is clearly visible in the output of dir(type). While it's technically legal for a descriptor to always raise AttributeError, breaking the following typical assumed invariant is *highly* dubious:

    all(hasattr(obj, x) for x in dir(obj))

I see three main alternatives for improving the situation:

1. Make __abstractmethods__ a read-only descriptor that returns something meaningful (like a frozenset(), the same as you get if you inherit from abc.ABCMeta without defining any abstract methods)

2. Throw a better exception message that explains the broken invariant rather than the generic "AttributeError with attribute name" that is currently thrown.

3. Implement type.__dir__ to filter out the uncooperative descriptor
msg198290 - (view) Author: Daniel Urban (daniel.urban) * (Python triager) Date: 2013-09-22 16:14
I like the 3rd alternative the most. It seems to me, that __abstractmethods__ is currently an undocumented implementation detail of ABCs. The 1st alternative would cause every type to have an empty __abstractmethods__ (which is technically correct, but probably not very useful). It seems to me that the 2nd alternative would not help very much in the original problem mentioned in issue10006 (automatically looking up every name found in dir). So I think, that the best would be to fix type.__dir__ this way. (Although, if we really want to document and use __abstractmethods__ on every type, then of course we should do the 1st alternative.)
History
Date User Action Args
2021-11-29 17:56:16iritkatrielsetcomponents: + Interpreter Core
2021-11-29 17:54:49iritkatrielsetversions: + Python 3.9, Python 3.10, Python 3.11, - Python 3.4
2013-09-22 16:14:15daniel.urbansetmessages: + msg198290
2013-09-15 07:34:19ncoghlansetnosy: + benjamin.peterson, daniel.urban, Yaroslav.Halchenko
2013-09-15 07:33:28ncoghlancreate