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

Add astropy import guard #2470

Merged
merged 2 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .mailmap
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Alexander Holas <[email protected]>
Alexander Holas <[email protected]>
Alexander Holas <[email protected]> AlexHls <[email protected]>
Alexander Holas <[email protected]> AlexHls <[email protected]>
Alexander Holas <[email protected]>

Alice Harpole <[email protected]>
Alice Harpole <[email protected]> Alice Harpole <[email protected]>
Expand Down
13 changes: 10 additions & 3 deletions tardis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,20 @@
# ----------------------------------------------------------------------------

import sys
import warnings

# ----------------------------------------------------------------------------

from astropy import physical_constants, astronomical_constants
if ("astropy.units" in sys.modules) or ("astropy.constants" in sys.modules):
warnings.warn(
"Astropy is already imported externally. Astropy should be imported"
" after TARDIS."
Copy link
Member

Choose a reason for hiding this comment

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

Sounds like a quick fix for me now.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think that there is any real 'fix' for this as the Exception comes from Astropy itself. This is just a guard such that TARDIS continues running and doesn't leave the user with a potentially cryptic Exception. At the worst, one uses a different version of the Astropy constants. This will probably mess with e.g. test results, but I think a warning is enough and the code doesn't need to raise an Exception at this point.

On a side note, ruff formatting and checking sorts the Astropy import before the TARDIS import, so this is bound to occur at some point.

)
else:
from astropy import physical_constants, astronomical_constants

physical_constants.set("codata2014")
astronomical_constants.set("iau2012")
physical_constants.set("codata2014")
astronomical_constants.set("iau2012")

# ----------------------------------------------------------------------------

Expand Down
Loading