Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Closed
t-vi wants to merge 1 commit into
python:masterpython/cpython:masterfrom
t-vi:class_filenamet-vi/cpython:class_filenameCopy head branch name to clipboard
Closed

bpo-33826: add __filename__ to Python-defined classes for introspection#13894
t-vi wants to merge 1 commit into
python:masterpython/cpython:masterfrom
t-vi:class_filenamet-vi/cpython:class_filenameCopy head branch name to clipboard

Conversation

@t-vi

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

Copy link
Copy Markdown

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
@t-vi t-vi force-pushed the class_filename branch from 0469a84 to 441a710 Compare June 7, 2019 14:31
This adds an attribute __filename__ to classes. It enables introspection
in IPython using inspect.getsource().
@t-vi t-vi force-pushed the class_filename branch from 441a710 to f7ee08c Compare June 7, 2019 14:35
@gvanrossum

Copy link
Copy Markdown
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

t-vi commented Jun 7, 2019

Copy link
Copy Markdown
Author

I'll do that next year. Thanks!

@t-vi t-vi closed this Jun 7, 2019
@t-vi

t-vi commented Jun 15, 2020

Copy link
Copy Markdown
Author

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
Copy Markdown
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.

4 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.