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
enable discovery of class source code in IPython interactively defined classes #78007
Comments
Hello, thank you for Python! I do have a patch (against python 3.6), but as this is my first attempt at digging in the internals, it is probably more than just a bit raw (I see test failures in test_dbm test_lib2to3 test_pydoc test_site, at least test_pydoc is due to the patch). Best regards Thomas |
In [1]: import logging In [2]: logging.Logger.__module__ In [4]: import sys In [6]: sys.modules[logging.Logger.__module__].__file__ |
What do you mean by "class source code" anyway? A class doesn't actually contain any executable code (and there's no code object associated with it). |
Apologies for not being clear. In IPython (or Jupyter), By this I mean: import inspect
class X:
pass
inspect.getsource(X) Note that:
By the way: Would a PR be more convenient than a patch? |
I got that part, thank you. I can't get 1)what you're expecting as an output and 2)why this should work the way you're suggesting 'cuz functions and classes are very different. In particular, classes can be modified dynamically, unlike functions. So if you just print out the lines from the file with the definition (which is what getsource() does), it may not be what the interpreter is actually using. |
So I want "inspect.showsource" to have the same output whether my class has been defined in a .py or in my Jupyter notebook. I appreciate there are limitations to what "inspect.showsource" can do, but I am not so sure how that creates uncertainty my expectation of the output when it should just be the same as the current behaviour when the class is defined in a .py module. |
Oh, I see, you want to be able to get the source for code entered into the interactive session. IPython does this by creating a separate fake "file name" for every input and adding corresponding entries for them into `linecache.cache'. This doesn't work for classes 'cuz for them, inspect.getfile() rather looks at __module__.__file__ , and there's no single __file__ for __main__. Now, there are two concerns here:
|
Yeah, it's a shame no-one looked at the patch that seems to fix the underlying cause and now it's just a duplicate of a bug to improve error messages. |
@thomas Viehmann , as it's currently formulated, this is a duplicate because it strives to allow getting class source from past interactive input -- which, as I explained, is already possible without the patch and seems to be inappropriate for vanilla console anyway. Your patch is rather a new class feature that provides a more robust link to its source, wherever it's located. If you reformulate your ticket to propose that instead, it will no longer be a duplicate. I'm not sure if the UI allows that, it may be better to create a new ticket. |
Sorry, I myself explained in https://bugs.python.org/msg319692 that it's not possible. The rest still stands though. |
Sorry about that I thought to redirect the discussion to the linked issue since it had a lot of people who might provide a better discussion about it. Feel free to reopen this if needed. |
Given that classes already have a '__module__' attribute, using which it is rather easy to find the filename, I don't see a good reason to add such an attribute to classes. IMO it would be more appropriate to directly create enhancement proposal for IPython/Jupyter to enhance the capabilities of their source code retrieval. I suggest rejecting this enhancement. |
Note that the module is less granular than the filename in IPython - i.e. it's useless for within notebooks. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: