Skip to content

Don't require astropy to be importable at setup.py time - #8402

Merged
astrofrog merged 13 commits into
astropy:masterfrom
astrofrog:no-require-import-setup
Feb 21, 2019
Merged

astrofrog merged 13 commits into
astropy:masterfrom
astrofrog:no-require-import-setup

Conversation

@astrofrog

@astrofrog astrofrog commented Feb 7, 2019 •

Copy link
Copy Markdown
Member

One of my missions currently is to try and reduce the complexity of the very idiosyncratic infrastructure that we've accumulated over the lifetime of the project. One of these in particular is the fact that until now packages have had to be importable at setup.py time even if they contain C extensions, which requires the convoluted approach of defining the __ASTROPY_SETUP__ builtin and making sure that every single C extension we import is conditional on this variable.

In the past, the reason why astropy needed to be importable is because astropy-helpers and the setup_package.py files tried to import astropy.version at various points in the process, though this then grew to include other files. After a series of pull requests in recent weeks, astropy-helpers should now no longer import the whole package (although packages might still do this to themselves) - instead if it really needs to access the version, then for now it uses the import_file function which allows us to import specific files without importing the whole package. This PR now extends this kind of work to the core package to make sure that when we do need to import other files during the setup process, we use import_file rather than importing e.g. astropy.version directly.

I should note the following changes in particular:

  • For the WCS module, I had to rearrange one of the files to facilitate this approach, but overall this doesn't require too many changes, mostly just combining two files.

  • We formerly imported astropy in the setup.py file to get __minimum_numpy_version__, but this PR changes that to define the numpy requirement properly in setup.cfg. This does mean duplication for now, but I would argue that since we define the requirement properly in setup.cfg/setup.py, we should consider getting rid of the definition of __minimum_numpy_version__ and the check for the numpy version in astropy/__init__.py, since that is no longer really necessary (when astropy is installed with pip or conda, a compatible version of numpy will be installed)

  • Currently I raise an exception in astropy/__init__.py if astropy is imported during the setup process. I had intended for that to just be a temporary thing to make sure this all works properly, but it might actually just make sense to keep it to prevent regressions.

Note that the 32-bit CircleCI build will fail but that's just because for some reason it uses astropy-helpers from master not this PR, so let's just ignore that failure.

@Cadair

Cadair commented Feb 7, 2019

Copy link
Copy Markdown
Member

🔥 🔥 🔥 🔥 🔥 🔥 🔥 🔥

@astrofrog

astrofrog commented Feb 7, 2019 •

Copy link
Copy Markdown
Member Author

There are a few issues with the builds that don't rely on setup.py commands, so I'll investigate that shortly, but I think this is related to other changes in astropy-helpers (related to build_ext). So feel free to review the PR as if there were no CI failures :)

Note to self: the issue is related to pypa/setuptools#1317 - that is because we import SetuptoolsBuildExt in astropy-helpers and that triggers a cython import in setuptools which fails as setup_requires hasn't been triggered yet. I'm not sure why this isn't an issue when using setup.py directly though, just pip install .. I think the solution is going to be to install the setup_requires dependencies ourselves in astropy-helpers.

@pllim

pllim commented Feb 7, 2019

Copy link
Copy Markdown
Member

Nope.

Traceback (most recent call last):
  File "setup.py", line 29, in <module>
    setup(setup_requires=setup_requires)
  File "/root/project/astropy_helpers/astropy_helpers/setup_helpers.py", line 93, in setup
    version = generate_version_py()
  File "/root/project/astropy_helpers/astropy_helpers/version_helpers.py", line 312, in generate_version_py
    version_module = get_pkg_version_module(packagename)
  File "/root/project/astropy_helpers/astropy_helpers/version_helpers.py", line 385, in get_pkg_version_module
    return __import__(packagename + '.version', fromlist=[''])
  File "/root/project/astropy/__init__.py", line 14, in <module>
    raise Exception("Can't import astropy at setup time")
Exception: Can't import astropy at setup time
Exited with code 1

@astrofrog

astrofrog commented Feb 7, 2019 •

Copy link
Copy Markdown
Member Author

@pllim - what did you do, and is your astropy_helpers submodule up to date? It looks like you are using an old version of astropy-helpers. Unless you are referring to one of the CI results, in which case as mentioned above the 32-bit failure is expected.

@pllim

pllim commented Feb 7, 2019

Copy link
Copy Markdown
Member

Oh... I didn't read the note about 32 bit, I just saw "feel free to review the PR as if there were no CI failures". Besides 32-bit, Travis, html-docs, and mpldev also failed. Is that also expected?

@drdavella

Copy link
Copy Markdown
Contributor

Just want to say that this is my favorite PR.

@nden

nden commented Feb 7, 2019

Copy link
Copy Markdown
Contributor

The WCS changes look fine. Though I didn't install and test locally.

@astrofrog

Copy link
Copy Markdown
Member Author

@pllim - for now yes, I need astropy/astropy-helpers#452 for things to work properly.

Comment thread astropy/wcs/wcs.py
# LOCAL
from astropy import log
from astropy.io import fits
from . import _docutil as __

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not really a review comment, but I never understood the rationale for importing things this way. Is there a good reason for using __ as the module name?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I have no idea and I found it veeeery confusing

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

as a way to show it's super private, and no one outside should use any of that functionality? If that's not the purpose, then I'm confused as well. 😕

@bsipocz

bsipocz commented Feb 7, 2019

Copy link
Copy Markdown
Member

I would say this deserves a mention in the changelog under "other changes"

@bsipocz bsipocz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Overall looks very neat. I had only one minor comment, and also would like to see a full green CI before approval and merging.

Comment thread astropy/wcs/wcs.py Outdated
raise
else:
_wcs = None
from . import docstrings as docutils

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

can we call it something else then docutils? Not that I plan to spend much time around in wcs, but it always confuses me when a name of an existing package is used as an alias for something else.

@codecov

codecov Bot commented Feb 8, 2019 •

Copy link
Copy Markdown

Codecov Report

Merging #8402 into master will increase coverage by 0.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #8402      +/-   ##
==========================================
+ Coverage   86.78%   86.79%   +0.01%     
==========================================
  Files         387      386       -1     
  Lines       58123    58090      -33     
  Branches     1060     1060              
==========================================
- Hits        50440    50422      -18     
+ Misses       7068     7053      -15     
  Partials      615      615
Impacted Files Coverage Δ
astropy/wcs/wcs.py 78.1% <100%> (+0.27%) ⬆️
astropy/wcs/__init__.py 66.66% <100%> (+16.66%) ⬆️
astropy/config/configuration.py 83.39% <100%> (+0.25%) ⬆️
astropy/coordinates/earth.py 75.84% <100%> (+0.75%) ⬆️
astropy/convolution/__init__.py 100% <100%> (+37.5%) ⬆️
astropy/samp/hub.py 77.52% <0%> (+0.13%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b8d29ba...58a47db. Read the comment docs.

@astrofrog

Copy link
Copy Markdown
Member Author

The CI is now passing! 🎉 - this is now ready for final reviewing (I think we should make sure it has several approvals before merging though as it's a reasonably significant change)

@pllim pllim left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Definitely cleaner! And the changes does not seem to break wcs docstring magic.

@Cadair Cadair left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm, but what do I know 😛

@nden nden left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

lgtm

@bsipocz bsipocz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It all looks good to me, and all my concerns were addressed, too.

@eteq eteq left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I also think this is a very good idea, but I have one concern (well, technically two, but it's really sort of an "either do A or if not, change B" thing) about the exception-raising - see inline.

One other related thing which doesn't necessarily need to hold up the PR but we probably want to keep in mind given these changes: We also use the _ASTROPY_SETUP_ trick in the package template right now, right? Do we want to it from there as well? It's possible that some affiliated package depends on this in some subtle way (a la the wcs change required here), but we probably want to discourage this at least for new packages moving forward, at least? (If not also help fix old ones...)

Comment thread astropy/__init__.py Resolved
@astrofrog

Copy link
Copy Markdown
Member Author

@eteq - good catch! Astropy needs to be importable by affiliated packages when they run python setup.py test, so we need a dedicated variable here, not _ASTROPY_SETUP_. I've fixed this in the latest commit.

@astrofrog
astrofrog force-pushed the no-require-import-setup branch from 2a48694 to b0d2899 Compare February 20, 2019 16:33
@astrofrog
astrofrog force-pushed the no-require-import-setup branch from b0d2899 to b884da9 Compare February 20, 2019 17:03
@bsipocz

bsipocz commented Feb 20, 2019

Copy link
Copy Markdown
Member

I wonder whether there is any long term open issue that this would close :) We need to keep on eye on them when triaging and close them :)

@eteq

eteq commented Feb 20, 2019

Copy link
Copy Markdown
Member

I restarted the docs build because it looked unrelated but want to be sure. Other than that this looks good, but just to confirm - @astrofrog , did you mean to reject my suggestion of a re-classing of the exception, or just miss it above? (It's ok either way, I just want to make sure it was intended and not missed since I buried it in an inline comment with some other stuff...)

@bsipocz

bsipocz commented Feb 20, 2019

Copy link
Copy Markdown
Member

@eteq the docs build could fail now due to the linkchecking. While it's being revised separately any failures related to that should be ignored.

Co-Authored-By: astrofrog <[email protected]>
@astrofrog

Copy link
Copy Markdown
Member Author

@eteq - I approved your suggestion, sorry I missed that before!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants