New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Getting the source of an object defined in an iPython session. #11249
Comments
Is there a solution or workaround to this by now? |
Want to refresh this issue |
Folks, it's been four years...any workaround/potential to fix this? |
The root problem is Digging around, I found this gem on Stackoverflow - ping @theevann: import inspect, sys
def new_getfile(object, _old_getfile=inspect.getfile):
if not inspect.isclass(object):
return _old_getfile(object)
# Lookup by parent module (as in current inspect)
if hasattr(object, '__module__'):
object_ = sys.modules.get(object.__module__)
if hasattr(object_, '__file__'):
return object_.__file__
# If parent module is __main__, lookup by methods (NEW)
for name, member in inspect.getmembers(object):
if inspect.isfunction(member) and object.__qualname__ + '.' + member.__name__ == member.__qualname__:
return inspect.getfile(member)
else:
raise TypeError('Source for {!r} not found'.format(object))
inspect.getfile = new_getfile ... which allows to retrieve the code of import inspect
from IPython.core.magics.code import extract_symbols
obj = MyClass
cell_code = "".join(inspect.linecache.getlines(new_getfile(obj)))
class_code = extract_symbols(cell_code, obj.__name__)[0][0]
print(class_code) Bottom line: This proves that it can be done (though it should probably be done better). It would be nice if The lack of an API like this breaks tools like The CPython project considers this an issue in IPython and refused a change in its standard library - probably correctly, because source code retrieval really depends on (moving) implementation details in IPython. |
When running the lightweight components in a notebook, the component class code inspection fails with error `source code is not available`. Full error log here [log.txt](https://github.com/ml6team/fondant/files/14114022/new.3.txt) The error is related to the fact that `inspect` relies on the class located in a source file which is not the case in a notebook where everything runs interactively. This is described in details [here](https://stackoverflow.com/questions/35854373/python-get-source-code-of-class-using-inspect#:~:text=getsource%20works%20by%20actually%20opening%20the%20source%20file%20that%20defines%20the%20object%20you%27re%20looking%20at.) Implemented the workaround suggested [here](ipython/ipython#11249) --------- Co-authored-by: Robbe Sneyders <robbe.sneyders@ml6.eu>
Usually, if you want to get the source of an object, you can get it via the inspect module:
However, in a Jupyter notebook / ipython console, this doesn't work:
Throws a TypeError:
TypeError: <module '__main__'> is a built-in class
. This appears to be because__file__
isn't defined in ipython sessions.Is there a way to inspect an object to get its source in a Jupyter notebook? The only way that comes to mind is something like
get_ipython
and then walking the history until you find the class definition you want. Is there another way?This appears to be related to #29, but that was closed, so maybe I'm missing something.
The text was updated successfully, but these errors were encountered: