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: inspect, class instances and __getattr__
Type: Stage:
Components: Library (Lib) Versions: Python 2.5
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: baijum81, doerwalter, facundobatista, gvanrossum, ping, rhettinger, sschwarzer
Priority: normal Keywords:

Created on 2003-04-09 20:01 by sschwarzer, last changed 2022-04-10 16:08 by admin. This issue is now closed.

Messages (11)
msg15412 - (view) Author: Stefan Schwarzer (sschwarzer) Date: 2003-04-09 20:01
inspect.isclass(class_instance) fails if the
corresponding class uses a "wildcard" implementation of
__getattr__.

Example:

Python 2.2.2 (#1, Nov 13 2002, 22:53:57) 
[GCC 2.95.4 20020320 [FreeBSD]] on freebsd4
Type "help", "copyright", "credits" or "license" for
more information.
>>> import inspect
>>> class X:
...     def __getattr__(self, name):
...             if name == 'foo':
...                     return 1   
...             if name == 'bar':
...                     return 2
...             else:
...                     return "default"
... 
>>> x = X()
>>> inspect.isclass(x)
1

The problematic expression in inspect.isclass is
hasattr(object, '__bases__') which returns a true value.
msg15413 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2003-04-15 00:36
Logged In: YES 
user_id=80475

Hmm.  I'm not sure that counts as a bug.  In an OO 
language, it's a feature that objects can be made to look 
like and be substituable for other types.  In this case, 
you've taught your object to be able to fake some classlike 
behavior (having a __bases__ attribute).

OTOH, inspect could have a stronger test for classhood:    
type(object) in (types.TypeType, types.ClassType)
msg15414 - (view) Author: Stefan Schwarzer (sschwarzer) Date: 2003-04-15 08:01
Logged In: YES 
user_id=383516

Hello Raymond, thanks for your reply. In fact, I'm also not
sure if it counts as a bug. I also suggested a patch (handle
__getattr__ requests for __bases__ with an AttributeError)
for for the SF project which causes/d the problem.

I think, if there's a better way to decide on "class-ness"
than now, the code in inspect should be changed.
Fortunately, it doesn't have to be backward-compatible,
because the module is always distributed with a certain
interpreter version.
msg15415 - (view) Author: Walter Dörwald (doerwalter) * (Python committer) Date: 2003-04-15 10:01
Logged In: YES 
user_id=89016

type(object) in (types.TypeType, types.ClassType)
won't work with custom metaclasses.
isinstance(object, (type, types.ClassType))
would be better.
msg15416 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2003-04-15 10:40
Logged In: YES 
user_id=80475

Ping, if this change is made, will isclass() still be able to 
find extension classes?

The addition of the hasattr(object, '__bases__') was made 
by you in ver 1.11 about two years ago. 
msg15417 - (view) Author: Facundo Batista (facundobatista) * (Python committer) Date: 2005-01-15 17:50
Logged In: YES 
user_id=752496

Please, could you verify if this problem persists in Python 2.3.4
or 2.4?

If yes, in which version? Can you provide a test case?

If the problem is solved, from which version?

Note that if you fail to answer in one month, I'll close this bug
as "Won't fix".

Thank you! 

.    Facundo
msg15418 - (view) Author: Stefan Schwarzer (sschwarzer) Date: 2005-01-28 16:44
Logged In: YES 
user_id=383516

Hi Facundo

The problem still exists in both Python 2.3.4 and 2.4.

A possible test case is:

import inspect
import unittest

class TestIsclass(unittest.TestCase):
    def test_instance_with_getattr(self):
        class Cls:
            def __getattr__(self, name):
                return "not important"
        obj = Cls()
        # obj is not a class
        self.failIf(inspect.isclass(obj))
msg15419 - (view) Author: Facundo Batista (facundobatista) * (Python committer) Date: 2005-05-30 19:15
Logged In: YES 
user_id=752496

Don't know yet if it's a bug or not, but in Py2.4.1
inspect.isclass() is still returning True in these cases...
msg15420 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2005-08-24 05:25
Logged In: YES 
user_id=80475

Ping, do you have a few minutes to look at this one and make
sure its the right thing to do.
msg15421 - (view) Author: Baiju M (baijum81) Date: 2006-09-09 13:14
Logged In: YES 
user_id=565450

Due to this bug, 'pydoc modulename' is not working.
pydoc tries to access __name__ attribute of classes,
so it raises attribute error. (actually it is not a class,
but an instance only).
So please increase the priority of this bug.

And this case is also not working (same issue):

class X:
  __bases__ = ()

x = X()
msg57860 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2007-11-26 23:13
Obviously Ping isn't listening, so waiting for him is not productive. 
Looking at the issue more, I can't really see a bug in inspect -- it's
the class definitions that are broken.  So closing as "rejected".

> Due to this bug, 'pydoc modulename' is not working.

Can you be more specific? I can't reproduce this.


> And this case is also not working (same issue):
> 
> class X:
>   __bases__ = ()
> 
> x = X()

Again, this is just a malformed class.
History
Date User Action Args
2022-04-10 16:08:05adminsetgithub: 38281
2007-11-26 23:13:53gvanrossumsetstatus: open -> closed
versions: - Python 2.6, Python 2.4, Python 3.0
messages: + msg57860
priority: high -> normal
assignee: ping ->
resolution: rejected
2007-11-20 01:16:38christian.heimessetnosy: + gvanrossum
2007-11-09 22:02:49christian.heimessetversions: + Python 2.6, Python 2.5, Python 3.0
2003-04-09 20:01:14sschwarzercreate