Skip to content

Standard imports#4401

Merged
dschult merged 10 commits intonetworkx:masterfrom
jarrodmillman:standard-imports
Dec 8, 2020
Merged

Standard imports#4401
dschult merged 10 commits intonetworkx:masterfrom
jarrodmillman:standard-imports

Conversation

@jarrodmillman
Copy link
Copy Markdown
Member

@jarrodmillman jarrodmillman commented Nov 29, 2020

Closes #4277 and makes searching for np, sp, linalg, etc. more helpful.

If scipy implements lazy loading a la https://snarky.ca/lazy-importing-in-python-3-7/ (maybe one of us should contribute it), then we can just remove all the lines like

import scipy.linalg  # call as sp.linalg
import scipy.sparse  # call as sp.sparse

similarly for matplotlib and lines like

import matplotlib.collections  # call as mpl.collections

@jarrodmillman jarrodmillman marked this pull request as draft November 29, 2020 04:55
@jarrodmillman jarrodmillman added this to the networkx-2.6 milestone Nov 29, 2020
@jarrodmillman jarrodmillman force-pushed the standard-imports branch 2 times, most recently from 18e0685 to 6c36fba Compare November 29, 2020 05:38
@jarrodmillman jarrodmillman marked this pull request as ready for review November 29, 2020 08:49
@dschult
Copy link
Copy Markdown
Member

dschult commented Dec 1, 2020

This is interesting. If I understand right, the idea is that once scipy/numpy/matplotlib make it so importing sub packages happens automagically upon attr lookup, we will remove the commented import lines.

I like the idea that a single import of scipy allows you to be able to use e.g. scipy.sparse without having to import that sub package explicitly.

Could we do the same thing? Instead of having to import approximations separately, let's try to make it so nx.approximations loads the sub package. If we work the kinks out for that, we might learn enough to try to make it work for scipy, numpy, etc.

But that's another PR.
This looks good to me. I haven't searched to make sure all places have been changed. I guess that you have done that.

@rossbar
Copy link
Copy Markdown
Contributor

rossbar commented Dec 1, 2020

I like the idea that a single import of scipy allows you to be able to use e.g. scipy.sparse without having to import that sub package explicitly.

I definitely agree. I haven't looked too closely at the __getattr__ lazy importing, but it would be great to have that pattern. I also agree with @dschult that it's an interesting thing to investigate for networkx as well.

@jarrodmillman
Copy link
Copy Markdown
Member Author

@stefanv is looking into lazy imports and said he thinks it should be doable.

This is for another PR as well, but I noticed some of our examples were doing this

from networkx import nx

instead of this

import networkx as nx

I fixed them in #4403. But we should clean up our __init__s and __all__s soon to prevent this from being possible (among other things).

@stefanv
Copy link
Copy Markdown
Contributor

stefanv commented Dec 3, 2020

I have a working prototype.

https://gist.github.com/stefanv/fde776cef7464b4e2cc0721bd37d51f4

Using this, I had to modify scikit-image's main __init__.py to include the following:

from .lazy import require

def __getattr__(name):
    return require(f'skimage.{name}')

Then, when you do:

import skimage as ski
ski.filters.window(...)

it lazy loads, and works!

The only problem I've noticed is that it makes IPython's auto-completer very angry, but I suspect we can work with that team to address it.

@jarrodmillman jarrodmillman marked this pull request as draft December 4, 2020 04:47
@jarrodmillman
Copy link
Copy Markdown
Member Author

@rossbar I removed the out of scope changes to the tests (cb08f7d) so it should be more straightforward to review now.

Copy link
Copy Markdown
Contributor

@rossbar rossbar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple questions/comments but overall looks good.

I generally agree that the import style proposed here is an improvement, even more so with lazy importing.

@jarrodmillman jarrodmillman marked this pull request as ready for review December 7, 2020 06:40
@jarrodmillman
Copy link
Copy Markdown
Member Author

@dschult @rossbar I think this is ready to merge.

@dschult dschult merged commit 831705d into networkx:master Dec 8, 2020
MridulS pushed a commit to MridulS/networkx that referenced this pull request Feb 4, 2023
* Standardize pytest imports

* Standardize numpy/scipy imports

* Document import style

* Fix

* More

* Fix matplotlib imports

* Revert changes to tests

* Motivate import policy

Co-authored-by: Ross Barnowski <rossbar@berkeley.edu>

* Fix missed imports

* Standard np.testing use

Co-authored-by: Ross Barnowski <rossbar@berkeley.edu>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

import style best practice?

4 participants