[Python-Dev] inspect and metaclasses
ethan at stoneleaf.us
Fri Sep 6 17:14:09 CEST 2013
On 09/06/2013 07:47 AM, Armin Rigo wrote:
> Are you suggesting that inspect.getmro(A) would return (A, object,
> type)? That seems very wrong to me.
Currently, `inspect.getmro(A)` returns `(A, object)`.
Considering that Python actually will look in A's metaclass to find a class attribute, I think returning `(A, object,
type(A)` is appropriate:
Python 3.4.0a1+ (default:61ca4732399b+, Sep 4 2013, 22:28:04)
[GCC 4.7.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
--> class Meta(type):
... meta_attr = 42
--> class Class(metaclass=Meta):
... cls_attr = 'Vitamin-soaked towel'
... def __init__(self):
... self.inst_attr = 'dolphins'
--> test = Class()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Class' object has no attribute 'meta_attr'
--> import inspect
--> inspect.getmro(Class) # with patch in place
(<class '__main__.Class'>, <class 'object'>, <class '__main__.Meta'>)
> If the goal is to fix `inspect.classify_class_attrs()`, then this
> function only needs a specific fix, along the lines of looking in
> `getmro(A) + getmro(type(A))`. (A more minor issue is that the bug
> report suggests `... + (type(A),)` only, but that's wrong: Python will
> also look in all the base classes of type(A).)
Good point. Will incorporate that into the final fix, whichever way it ends up.
> "Fixing" inspect.getmro() as suggested would break a lot of other usages of it.
More information about the Python-Dev