MNT: Make asdf import truly optional in io.misc.asdf#10260
MNT: Make asdf import truly optional in io.misc.asdf#10260pllim wants to merge 2 commits intoastropy:masterfrom
Conversation
|
@pllim this also works, if we add it to a conftest.py file in astropy/io/misc/asdf: It has the advantage of maintaining itself, and keeping some clutter out of the other .py files in that subdirectory. On the other hand, it only fixes pytest collection, not other cases where an attempt to import the module fails. |
|
My personal opinion: Astropy's only hard dependency at run-time is Numpy, therefore it seems ungraceful to have import error when accessing |
|
With the conftest solution I still get the same collection errors, at least when using the latest pytest, etc. |
|
I would very much like to move ahead with #7939, which would mean |
|
As of milestone, I think 4.1 is appropriate as this is a cleanup without functionality change. |
This comment has been minimized.
This comment has been minimized.
2e482b7 to
b4e2960
Compare
This comment has been minimized.
This comment has been minimized.
b4e2960 to
21c5048
Compare
| node['value'] = custom_tree_to_tagged_tree(spec_coord.value, ctx) | ||
| node['unit'] = custom_tree_to_tagged_tree(spec_coord.unit, ctx) | ||
| node['observer'] = custom_tree_to_tagged_tree(spec_coord.observer, ctx) | ||
| node['target'] = custom_tree_to_tagged_tree(spec_coord.target, ctx) |
|
Interestingly there are 4 remaining ones when testing with your doctestplus PR+pytest5.4. I'm not 100% certain that it's relevant to this PR, as with that plugin PR the test collection should not fail with import issues. |
21c5048 to
d7cc3b4
Compare
|
@bsipocz , at least one of the test files have |
|
@bsipocz I tried this PR on: and also each with doctestplus master and minimum dependencies and was able to collect everything without errors. |
|
@bsipocz , I am fine if you want to remilestone this one. No rush on my side. FYI. |
|
@pllim - I feel this can be a backport, but it's fine to change to the final milestone when we get to pushing the green button. |
|
So is the purpose of this PR to not get pytest collection errors? I believe there's much more simple ways to do this. I.e. In the end, this PR ends up raising In [1]: from astropy.io.misc.asdf import extension
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-2-fac6e72e8a75> in <module>
----> 1 from astropy.io.misc.asdf import extension
~/dev/astropy/astropy/io/misc/asdf/extension.py in <module>
4 import os
5
----> 6 from asdf.extension import AsdfExtension, BuiltinExtension
7 from asdf.resolver import Resolver, DEFAULT_URL_MAPPING
8 from asdf.util import filepath_to_url
ModuleNotFoundError: No module named 'asdf'is probably pretty clear for most debugging purposes. |
|
My personal opinion is a more philosophical one, so I'll defer to @bsipocz since she originally reported the problem. |
eslavich
left a comment
There was a problem hiding this comment.
I have a couple of suggestions for changes, but nothing terribly important.
I don't yet understand the need to import these modules, since even when asdf is installed the code is automatically loaded via entry points. Users shouldn't need to access any of this directly. If there doesn't turn out to be a use case for importing, then I'd prefer to solve the problem with a pytest feature like @jdavies-st suggests.
| HAS_ASDF = False | ||
| AsdfExtension = object | ||
| BuiltinExtension = object | ||
| ASTROPY_SCHEMA_URI_BASE = '' |
There was a problem hiding this comment.
ASTROPY_SCHEMA_URI_BASE and SCHEMA_PATH don't use anything from the asdf package, so they can continue to live outside of the try/except/else.
| else: | ||
| HAS_ASDF = True | ||
|
|
||
| # Make sure that all tag implementations are imported by the time we create |
There was a problem hiding this comment.
I think we can continue to import these even if asdf is missing, since each of the individual modules already needs to be importable so that pytest can collect from them. I tested moving them back outside the try/except/else and pytest doesn't complain.
| ASTROPY_SCHEMA_URI_BASE = 'http://astropy.org/schemas/' | ||
| SCHEMA_PATH = os.path.abspath( | ||
| os.path.join(os.path.dirname(__file__), 'data', 'schemas')) | ||
| ASTROPY_URL_MAPPING = [ |
There was a problem hiding this comment.
Another option here is to just compute this inside of AsdfExtension.url_mapping below. When asdf loads an extension, it only calls that method once, so there's no performance concern.
| assert_array_equal(a.lon, b.lon) | ||
|
|
||
|
|
||
| # Cannot use super() in __init__ to check for asdf import here because it |
There was a problem hiding this comment.
We wouldn't need to anyway since AstropyAsdfType already provides an __init__ that checks for asdf?
|
Thanks! I'll wait for a resolution whether we should move forward with this or not before I spend time on another commit. |
|
In order to write an object to ASDF or to read an ASDF file, the first command a user needs is |
Description
This pull request is to address
asdfimport errors reported in #10246. Also fixed PEP 8 in the files I touched.With this patch, all
io.misc.asdftests are skipped without collection error whenasdfis not installed. But I am not familiar with howasdfworks, so review from experts would be greatly appreciated.cc @eslavich
TODO