Skip to content

Commit 3703265

Browse files
authored
Merge pull request #40 from nstarman/intersphinx-xref-physical-types
intersphinx xref
2 parents 0e35fd0 + 1001d69 commit 3703265

File tree

2 files changed

+90
-6
lines changed

2 files changed

+90
-6
lines changed

setup.cfg

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@ universal = 1
2222
zip_safe = False
2323
packages = find:
2424
install_requires =
25-
sphinx>=1.7
26-
astropy-sphinx-theme
27-
numpydoc
28-
sphinx-automodapi
29-
sphinx-gallery
30-
pillow
25+
sphinx>=1.7
26+
astropy-sphinx-theme
27+
numpydoc
28+
sphinx-automodapi
29+
sphinx-gallery
30+
pillow
31+
32+
[options.extras_require]
33+
all =
34+
astropy
3135

3236
[options.package_data]
3337
sphinx_astropy = local/*

sphinx_astropy/conf/v1.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,22 @@
1111

1212
import os
1313
import warnings
14+
from collections import ChainMap
1415

1516
from os import path
1617

1718
import sphinx
1819
from distutils.version import LooseVersion
1920

21+
try:
22+
import astropy
23+
except ImportError:
24+
ASTROPY_INSTALLED = False
25+
else:
26+
ASTROPY_INSTALLED = True
27+
28+
from astropy.utils import minversion
29+
2030

2131
# -- General configuration ----------------------------------------------------
2232

@@ -88,6 +98,76 @@ def check_sphinx_version(expected_version):
8898

8999
suppress_warnings = ['app.add_directive', ]
90100

101+
# -- NumpyDoc X-Ref ------------------------
102+
103+
# Whether to create cross-references for the parameter types in the
104+
# Parameters, Other Parameters, Returns and Yields sections of the docstring.
105+
# Should be set = True in packages manually! included here as reference.
106+
# numpydoc_xref_param_type = False
107+
108+
# Words not to cross-reference. Most likely, these are common words used in
109+
# parameter type descriptions that may be confused for classes of the same
110+
# name. This can be overwritten or modified in packages and is provided here for
111+
# convenience.
112+
numpydoc_xref_ignore = {"or", "of", "thereof",
113+
"default", "optional", "keyword-only",
114+
"instance", "type", "class", "subclass", "method"}
115+
116+
# Mappings to fully qualified paths (or correct ReST references) for the
117+
# aliases/shortcuts used when specifying the types of parameters.
118+
# Numpy provides some defaults
119+
# https://github.com/numpy/numpydoc/blob/b352cd7635f2ea7748722f410a31f937d92545cc/numpydoc/xref.py#L62-L94
120+
numpydoc_xref_aliases = {
121+
# Python terms
122+
"function": ":term:`python:function`",
123+
"iterator": ":term:`python:iterator`",
124+
"mapping": ":term:`python:mapping`",
125+
}
126+
127+
# Aliases to Astropy's glossary. In packages these can be turned on with
128+
# ``numpydoc_xref_aliases.update(numpydoc_xref_aliases_astropy_glossary)``
129+
# (if astropy is in the intersphinx mapping).
130+
numpydoc_xref_aliases_astropy_glossary = {} # works even if no Astropy
131+
if ASTROPY_INSTALLED and minversion(astropy, "4.3"):
132+
numpydoc_xref_aliases_astropy_glossary = {
133+
# general
134+
"-like": ":term:`astropy:-like`",
135+
# coordinates
136+
"angle-like": ":term:`astropy:angle-like`",
137+
"coordinate-like": ":term:`astropy:coordinate-like`",
138+
"frame-like": ":term:`astropy:frame-like`",
139+
# units
140+
"unit-like": ":term:`astropy:unit-like`",
141+
"quantity-like": ":term:`astropy:quantity-like`",
142+
# table
143+
"table-like": ":term:`astropy:table-like`",
144+
# time
145+
"time-like": ":term:`astropy:time-like`",
146+
}
147+
148+
# Aliases to Astropy's physical types. In packages these can be turned on with
149+
# ``numpydoc_xref_aliases.update(numpydoc_xref_aliases_astropy_physical_type)``
150+
# (if astropy is in the intersphinx mapping).
151+
numpydoc_xref_aliases_astropy_physical_type = {} # works even if no astropy
152+
if ASTROPY_INSTALLED and minversion(astropy, "4.3"):
153+
154+
from astropy.units.physical import _name_physical_mapping
155+
for ptype in _name_physical_mapping.keys():
156+
val = f":ref:`:ref: '{ptype}' <astropy:{ptype}>`" # <= intersphinxed
157+
numpydoc_xref_aliases_astropy_physical_type[f"'{ptype}'"] = val
158+
159+
del ptype, val, _name_physical_mapping # cleanup namespace
160+
161+
162+
# Convenient collection of all of astropy's options for numpydoc xref.
163+
# In packages all the astropy additions can be turned on with
164+
# ``numpydoc_xref_aliases.update(numpydoc_xref_astropy_aliases)``
165+
# (if astropy is in the intersphinx mapping).
166+
numpydoc_xref_astropy_aliases = ChainMap( # important at the top
167+
numpydoc_xref_aliases_astropy_glossary,
168+
numpydoc_xref_aliases_astropy_physical_type
169+
)
170+
91171
# -- Project information ------------------------------------------------------
92172

93173
# There are two options for replacing |today|: either, you set today to some

0 commit comments

Comments
 (0)