Skip to content
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

bpo-33826: add __filename__ to Python-defined classes for introspection #13894

Closed
wants to merge 1 commit into from

Conversation

t-vi
Copy link

@t-vi t-vi commented Jun 7, 2019

This adds an attribute __filename__ to classes. It enables introspection in IPython/Jupyter using inspect.getsource().

https://bugs.python.org/issue33826

@taleinat taleinat changed the title bpo-37177: add __filename__ to Python-defined classes for introspection bpo-33826: add __filename__ to Python-defined classes for introspection Jun 7, 2019
This adds an attribute __filename__ to classes. It enables introspection
in IPython using inspect.getsource().
@gvanrossum
Copy link
Member

I'm sorry, I don't read consensus in the ticket that this is the solution. You may have to make a quick detour through python-dev or Discourse (discuss.python.org) to see what other core devs think.

@t-vi
Copy link
Author

t-vi commented Jun 7, 2019

I'll do that next year. Thanks!

@t-vi t-vi closed this Jun 7, 2019
@t-vi
Copy link
Author

t-vi commented Jun 15, 2020

So a year passed, I sent the following to python-dev, but I'm not sure whether it is filtered (it doesn't show up in the archives).

I would love to hear your opinion on the following aspect of inspect that I believe might be worth improving:

Consider the following program saved in a file (say hello.py):

import inspect

def hello():
    print("Hello World")

print(inspect.getsource(hello))

class Hello:
    def __init__(self):
        print("Hello World")

print(inspect.getsource(Hello))

Running hello.py will, unsurprisingly, print the source of hello and Hello.

Now, some of us use an Jupyter (with the capabilities provided by IPython) notebooks, which are a great tool and awesome match with Python. These notebooks can be large and complex enough to want to use introspection on methods defined in itself (also, I'm prototyping things I might want to use as a library in Notebooks a lot, and I think I'm not alone).

IPython enhances the interactive console to enable introspection (by providing "files" for the cells).
As a result, the following will work as expected:

def hello():
    print("Hello World")
print(inspect.getsource(hello))

However, it does not work for classes:

class Hello:
    def __init__(self):
        print("Hello World")
print(inspect.getsource(Hello))

will run into an error in a Jupyter notebook, more precisely

TypeError: <class '__main__.Hello'> is a built-in class

The reason why the latter does not work is because inspect cannot find a source file.

The technical background is that for a function hello, inspect.getfile finds the file through hello.__code__.co_filename which IPython can arrange for, while for the class Hello, it tries Hello.__module__, which is __main__ and then would see if sys.modules[Hello.__module__] has a __file__ attribute, which it does not (and which could not be disambiguated into cell-level).

I once made this PR in github and sent a bug and patch earlier but got, let's say, reactions that were not entirely encouraging. I still think that it is a useful feature and I don't think that there are readily available solutions and after another year has passed, I humbly submit this for your considerations.

@gvanrossum
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants