Skip to content

Commit c531506

Browse files
committed
make Astropy an optional dependency
cleanup dynamic generation Signed-off-by: Nathaniel Starkman (@nstarman) <[email protected]>
1 parent f0805eb commit c531506

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

setup.cfg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ install_requires =
2828
sphinx-automodapi
2929
sphinx-gallery
3030
pillow
31+
32+
[options.extras_require]
33+
all =
3134
astropy
3235

3336
[options.package_data]

sphinx_astropy/conf/v1.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,16 @@
1515

1616
from os import path
1717

18-
import astropy
1918
import sphinx
2019
from distutils.version import LooseVersion
2120

21+
try:
22+
import astropy
23+
except ImportError:
24+
ASTROPY_INSTALLED = False
25+
else:
26+
ASTROPY_INSTALLED = True
27+
2228

2329
# -- General configuration ----------------------------------------------------
2430

@@ -102,8 +108,8 @@ def check_sphinx_version(expected_version):
102108
# name. This can be overwritten or modified in packages and is provided here for
103109
# convenience.
104110
numpydoc_xref_ignore = {
105-
'type', 'optional', 'default', 'or', 'of', 'method', 'instance', "like",
106-
"class", 'subclass', "keyword-only", "default", "thereof"
111+
"type", "optional", "default", "or", "of", "method", "instance",
112+
"class", "subclass", "keyword-only", "default", "thereof",
107113
}
108114

109115
# Mappings to fully qualified paths (or correct ReST references) for the
@@ -120,14 +126,11 @@ def check_sphinx_version(expected_version):
120126
# Aliases to Astropy's glossary. In packages these can be turned on with
121127
# ``numpydoc_xref_aliases.update(numpydoc_xref_aliases_astropy_glossary)``
122128
# (if astropy is in the intersphinx mapping).
123-
numpydoc_xref_aliases_astropy_glossary = {} # works even if older astropy
124-
if float(astropy.__version__[:3]) >= 4.3:
129+
numpydoc_xref_aliases_astropy_glossary = {} # works even if older Astropy
130+
if ASTROPY_INSTALLED and float(astropy.__version__[:3]) >= 4.3:
125131
numpydoc_xref_aliases_astropy_glossary = {
126132
# general
127133
"-like": ":term:`astropy:-like`",
128-
# "number": ":term:`number`",
129-
# "writable": ":term:`writable file-like object`",
130-
# "readable": ":term:`readable file-like object`",
131134
# coordinates
132135
"angle-like": ":term:`astropy:angle-like`",
133136
"coordinate-like": ":term:`astropy:coordinate-like`",
@@ -145,18 +148,14 @@ def check_sphinx_version(expected_version):
145148
# ``numpydoc_xref_aliases.update(numpydoc_xref_aliases_astropy_physical_type)``
146149
# (if astropy is in the intersphinx mapping).
147150
numpydoc_xref_aliases_astropy_physical_type = {} # works even if older astropy
148-
if float(astropy.__version__[:3]) >= 4.3:
149-
150-
# TODO! refactor if #11678 is implemented
151-
from astropy.units.physical import _units_and_physical_types
152-
for _, ptypes in _units_and_physical_types:
153-
ptypes = {ptypes} if isinstance(ptypes, str) else ptypes
154-
for ptype in ptypes:
155-
key = f"'{ptype}'"
156-
val = f":ref:`:ref: '{ptype}' <astropy:{ptype}>`" # <= intersphinxed.
157-
numpydoc_xref_aliases_astropy_physical_type[key] = val
158-
159-
del ptypes, key, val, _units_and_physical_types
151+
if ASTROPY_INSTALLED and float(astropy.__version__[:3]) >= 4.3:
152+
153+
from astropy.units.physical import _name_physical_mapping
154+
for ptype in _name_physical_mapping.keys():
155+
val = f":ref:`:ref: '{ptype}' <astropy:{ptype}>`" # <= intersphinxed
156+
numpydoc_xref_aliases_astropy_physical_type[f"'{ptype}'"] = val
157+
158+
del ptype, val, _name_physical_mapping # cleanup namespace
160159

161160

162161
# Convenient collection of all of astropy's options for numpydoc xref.

0 commit comments

Comments
 (0)