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

Show subclasses when using pinfo/pinfo2/?/?? #11432

Open
Carreau opened this issue Oct 22, 2018 · 7 comments
Open

Show subclasses when using pinfo/pinfo2/?/?? #11432

Carreau opened this issue Oct 22, 2018 · 7 comments

Comments

@Carreau
Copy link
Member

Carreau commented Oct 22, 2018

See python/cpython#5066

We should probably match that.

Extra points, when possible display the tree hierarchy ?

@Carreau
Copy link
Member Author

Carreau commented Oct 30, 2018

Our goal here is to get something like the following. Once it is working we can refine a nd get a better User interface:

$ ipython
Python 3.6.5 | packaged by conda-forge | (default, Apr  6 2018, 13:44:09)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.2.0.dev -- An enhanced Interactive Python. Type '?' for help.

In [1]: class Foo:
   ...:     pass
   ...:

In [2]: class Bar(Foo):
   ...:     pass
   ...:

In [3]: Foo?
Init signature: Foo()
Docstring:      <no docstring>
Type:           type
Subclasses:      - Bar    <- that is new and useful

Here we have only one subclass, we probably want to limit to the first few subclasses if we have hundreds.

How to implement that.

  1. make sure you have IPython working on your machine. We recommend installing anaconda. And then conda install ipython

  2. clone this repository:

$ git clone https://github.com/ipython/ipython

Cd into the repository and do a dev-install :

$ cd ipython
$ pip install -e . 

now when you start $ ipython , the third live should say something like 7.2.0.dev if you see dev, then all s good. This mean that you run IPython from the source, and noot from the install packages. You are officially a developper now.

Time to do modifications to the source code. They will be taken into account everytime you restart IPython .

In our case we want to modify IPython/core/oinspect.py. Around line 830 find the line:

if inspect.isclass(obj):

Under this block we want to modify out, to contain the list of subclasses.

We can get all the subclasses of obj by using obj__subclasses()
As we want a something that look nice to the user we are actually interested in in each subclass name, and not the subclass them selves. That is to say we want to have:

Subclasses: - Bar and not Subclasses: - <class bar at 0x123456>
so let's so around line 862:

names = [sub.__name__ for sub in obj.__subclasses__()]

We now want to join all the names by commas for example:

all_names = ', '.join(names)

and let's now put that into out under the names subclasses.::

out['subclasses'] = all_names

Good ! We've done the hard work.

Now close to line 642 find elif info['isclass'],

Modify close to the end of the block to look like :

 append_field(_mime, 'File', 'file')
 append_field(_mime, 'Type', 'type_name')
+append_field(_mime, 'Subclasses', 'subclasses') # line added

Save the file, Open IPython and try the code at the top of this comment.

If it works will go through how to commit it push it on github ad make a pull-request

@Carreau
Copy link
Member Author

Carreau commented Oct 31, 2018

Assigning to @cmentzel :-)

@Carreau
Copy link
Member Author

Carreau commented Nov 1, 2018

Following some question about branching/forking the repository, in particular do you need to do a branch, or to fork before making any changes.

It is IMHO, mostly a question of taste, and where/when to put the complexity.
Some people will tend to - before doing any work – create a new branch:

$ git branch a-new-name
$ git checkout a-new-name

That is typically creating a named alternate reality that diverge from master, at the time you wrote git branch a-new-name, it is in no way mandatory – branch names are convenience names for human, and can be anyway move around, renamed after the fact.

It is also in no way mandatory for the names of the branch on your local machine to match the names on github. It is also perfectly fine to push master on GitHub. You cannot do anything wrong as you do not have anyway permission to push on this repository. You can only screw-up your fork, and if anything as branch names are convenience name they can always be fixed after the fact.

Here is a gif of me pushing on my local master branch to the master branch of my github fork,
and then pushing my local master branch to another name.

example push

Here are some further instructions once your code is ready:

  • Feel free to commit any time: git add <changed file>, git commit, Write you commit message and <esc>wq to save and quit (or git commit -a -m "message for the commit").
  • For ipython (press the fork button at the top right of the page).
  • Add your fork at as remote of your clone: git remote add cmentzel git://github.com/cmentzel/ipython
  • push to your repository: git push cmentzel (here you can put more option but that is sufficient)

Git will tell you something like:

$ git push cmentzel 
Enumerating objects: 5, done.
...
...
remote: Create a pull request for 'another-name' on GitHub by visiting:
remote:      https://github.com/cmentzel/ipython/pull/new/another-name
remote:
To ssh://github.com/cmentzel/ipython
 * [new branch]          master -> master

Visit the above URL and submit the pull request. We can figure out the rest from there. In particular once you have submitted the pull request that would allow me to do extra fixes and the given branch if necessary.

Git is complex and take some time to grasp, but regardless of what you do you will have a really hard time breaking anything.

@cmentzel
Copy link
Contributor

cmentzel commented Nov 8, 2018

Great, just pulled the trigger

@cmentzel
Copy link
Contributor

cmentzel commented Nov 8, 2018

Yay! AppVeyor failed, looking through logs now. Not sure what I am looking at...

@cmentzel
Copy link
Contributor

cmentzel commented Nov 8, 2018

Thanks for the walk-through and for doing all the work @Carreau !!

@Carreau
Copy link
Member Author

Carreau commented Nov 8, 2018

You did the PR, I only guided you ! Happy to have your contributions ! Are you going to contribute regularly now ?

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

No branches or pull requests

2 participants