Skip to content

Exclude the physical-type aliases from the default numpydoc xref aliases - #103

Merged
astrofrog merged 2 commits into
astropy:mainfrom
astrofrog:fix-physical-type-xref-collision
Jun 24, 2026
Merged

Exclude the physical-type aliases from the default numpydoc xref aliases#103
astrofrog merged 2 commits into
astropy:mainfrom
astrofrog:fix-physical-type-xref-collision

Conversation

@astrofrog

Copy link
Copy Markdown
Member

This is to fix astropy issue #19868: in the
rendered simple_norm docs, the 'power' option of stretch : {'linear', 'sqrt', 'power', ...} is turned into a hyperlink to the power physical type. It's a meaningless link — 'power' there is a literal option value, not a unit physical type.

This happens because sphinx_astropy/conf/v2.py builds numpydoc_xref_aliases_astropy_physical_type with keys that are quoted strings ('power', 'energy', 'time', ...) and folds them into the default numpydoc_xref_astropy_aliases.

Numpydoc gives a quoted string a single meaning regardless of context, but the
common context is an enum of literal options:

stretch : {'linear', 'sqrt', 'power', ...}

Here 'power' is a literal choice, not a type — but it's the same token the alias is keyed on, so alias substitution rewrites it into a cross-reference. About a dozen physical-type names collide with ordinary option words (area,
energy, force, length, mass, power, pressure, speed, time, volume, ...), so any {...} enum using one of those words gets a spurious link.

These aliases were added as an opt-in in #40. The intended use was a parameter whose type is a physical quantity, written as a quoted name — e.g. length : 'length' linking to astropy's physical-types docs. The original comment documents them as something packages turn on explicitly with numpydoc_xref_aliases.update(numpydoc_xref_astropy_aliases) — but glossary and physical-type aliases were fused into a single ChainMap, so a package couldn't take the safe glossary aliases without also pulling in the colliding physical-type ones.

They also appear never to have actually worked properly anyway: the alias value has a double :ref: (:ref::ref: '{ptype}' <...>``) since the very first commit (this is also fixed here)

The fix is to drop numpydoc_xref_aliases_astropy_physical_type from the default numpydoc_xref_astropy_aliases ChainMap. It stays defined and documented as an explicit opt-in for packages that genuinely want physical-type cross-refs (having it opt-in was the original intent):

numpydoc_xref_aliases.update(numpydoc_xref_aliases_astropy_physical_type)

The glossary aliases remain on by default — they're all hyphenated -like terms (time-like, etc.) that don't collide with literal option values, so they're safe(r).

This also fixes the double :ref:, so the opt-in path now renders a correct link instead of literal :ref: 'power' text.

In future we could consider not automagically changing 'power' to a ref but having some kind of namespace like physical_type:power in docstrings to avoid any ambiguities.

…ses so quoted option values such as 'power' are no longer turned into spurious cross-references, leaving them available as an explicit opt-in and fixing the malformed :ref: value so that opt-in renders correctly

@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.

Thank you!

Comment thread CHANGES.md Outdated

@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.

At least for the problem I saw, this PR seems to fix it. Yay!

astropy/astropy#19891

Co-authored-by: P. L. Lim <2090236+pllim@users.noreply.github.com>
@astrofrog
astrofrog marked this pull request as ready for review June 9, 2026 22:31
@astrofrog
astrofrog merged commit 3fc682b into astropy:main Jun 24, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants