Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fix issue 447 - inspecting old-style classes. #449

Merged
merged 2 commits into from

3 participants

@takluyver
Owner

Tweak Inspector.info() so it doesn't fail on old style classes. Added test.

Closes gh-447

@minrk

You can just do callable_obj = getattr(obj, '__init__', obj). A class object itself is callable.

It is, but getargspec won't handle a class.

Owner

Interesting, because I made exactly this change and it works just fine.

class A:
    """doc"""
    pass
A(

correctly shows 'doc' in the tooltip.

The bit that gets the docstring is working already, this is just an incidental error in Inspector.info() with old-style classes. I've just checked, and we catch the error from getargspec, so the result will be the same. I went for catching AttributeError because it's a pattern that's used several times already in the info method.

Owner

Fair enough. I still like getattr(obj, '__init__', None) better than 4 lines, but that's not important. I say merge.

Actually, yes. While we're here, let's make it tidy...

@meawoppl

I wish to preemptively apologize for this and future things I will break in iPython. By mid-July, I expect I will know the code base well enough to start pitching in.

Thanks guys :)

@takluyver
Owner

@meawoppl: Don't worry - you didn't break it, you just found that it was already broken. We like people to find bugs before release. Especially when they're easily fixed, like this one.

Waiting an hour or so for any objections, then we'll merge, since it's a small fix.

@takluyver takluyver merged commit 12af0e2 into ipython:master
@ellisonbg ellisonbg referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 14 additions and 1 deletion.
  1. +2 −1  IPython/core/oinspect.py
  2. +12 −0 IPython/core/tests/test_oinspect.py
View
3  IPython/core/oinspect.py
@@ -662,7 +662,8 @@ def info(self, obj, oname='', formatter=None, info=None, detail_level=0):
# from its __call__ method.
if inspect.isclass(obj):
- callable_obj = obj.__init__
+ # Old-style classes need not have an __init__
+ callable_obj = getattr(obj, "__init__", None)
elif callable(obj):
callable_obj = obj
else:
View
12 IPython/core/tests/test_oinspect.py
@@ -44,6 +44,10 @@ def __call__(self, *a, **kw):
def method(self, x, z=2):
"""Some method's docstring"""
+
+class OldStyle:
+ """An old-style class for testing."""
+ pass
def f(x, y=2, *a, **kw):
"""A simple function."""
@@ -117,3 +121,11 @@ def test_info():
nt.assert_equal(i['class_docstring'], Call.__doc__)
nt.assert_equal(i['init_docstring'], Call.__init__.__doc__)
nt.assert_equal(i['call_docstring'], c.__call__.__doc__)
+
+ # Test old-style classes, which for example may not have an __init__ method.
+ i = inspector.info(OldStyle)
+ nt.assert_equal(i['type_name'], 'classobj')
+
+ i = inspector.info(OldStyle())
+ nt.assert_equal(i['type_name'], 'instance')
+ nt.assert_equal(i['docstring'], OldStyle.__doc__)
Something went wrong with that request. Please try again.