Would have been an option in the beginning of the project, maybe.
In the current state of the project though it would require some major refactoring for each individual optional dependency. Overriding the import has turned out to be the better solution.
Well, maybe one missing detail is that it doesn't really override the original import but just wraps it. The original import instruction is used internally to import all other non-optional modules and optional modules that are installed.
What about the places where the modules are actually used? We would have had to catch those calls as well in the case that a module is not available. That were a huge amount of additional try-except blocks and another huge amount of actual error handling (avoiding all calls to that module). We tried that. It's ugly.
Specifically for matplotlib that hasn't made much sense to us. It's totally okay that the program runs with or without matplotlib since matplotlib does not alter anything. The MagicMock objects returned by the wrapped import do no harm. They just pretend to do what matplotlib was supposed to do - without actually doing it. A dry run, if you will. The user will be informed that matplotlib is optional and not installed so that they don't wonder why visualization does not work.
To help you understand: we were after a solution where most of the already existing code could remain untouched. Only specific modules are marked optional. That is, if a module is missing that is not explicitly marked optional, Python will still raise an ImportError. Otherwise it will only show a warning, but the program will continue to run. The thing is, the existing code base is quite large and quite old. We still have instructions/syntax in there that date back to Python 2.5. Messing with that code aside from upgrading it to at least Python 2.7 is a risky challenge.
Furthermore, it's political. There's currently no official project that involves working on the code. So everything that has to be done, must take as little time as possible.
Wrappingimport was the quickest and cleanest solution we could come up with within the available time. It may not be beautiful but it works and we're okay with it. It's also documented so that future developers will understand what's happening.
I mean I do exactly that. Here's an excerpt from some code of mine.
try:
import matplotlib.pyplot as plt
except ImportError:
plt = None
print( "Warning: could not import from matplotlib. Echogram.show() will not work.")
Now later on in the class that would call imshow ...
def show(self):
'''
Plot an image of the echogram.
'''
if plt is None:
logging.warning("matplotlib was not imported, cannot plot")
return
plt.imshow(self.data, aspect='auto')
So your code (apparently) ...
literally changes the semantic meaning of a python builtin
alters behavior the user may not be expecting
probably makes the code seem odd to a new comer
I would prefer my solution - which isn't full of thousands of try / excepts - any day.
0
u/[deleted] Jan 05 '18
Would have been an option in the beginning of the project, maybe.
In the current state of the project though it would require some major refactoring for each individual optional dependency. Overriding the
importhas turned out to be the better solution.