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

[rst] Improve unreferenced footnote warnings #12730

Merged

Conversation

chrisjsewell
Copy link
Member

@chrisjsewell chrisjsewell commented Aug 4, 2024

The current UnreferencedFootnotesDetector transform (added in 01cb3a7) is untested and misses warnings for a number of cases.

In this PR I change how the detection works, by moving the transform to run just after the docutils Footnote resolution transform; https://github.com/live-clones/docutils/blob/266f359408130ca25f6f161453140e73f169408a/docutils/docutils/transforms/references.py#L410,
then simply check for any footnotes without "back-references".
I believe this is simpler to reason about and is no longer trying to pre-empt the resolution.

In the newly added test for this transform, if you compare to the previous transform, you will see that it now picks up previously missed unreferenced footnote (for symbols and labelled auto-numbered footnotes)

source/index.rst:8: WARNING: Duplicate explicit target name: "2". [docutils]
source/index.rst:13: WARNING: Duplicate explicit target name: "label1". [docutils]
source/index.rst:9: WARNING: Footnote [3] is not referenced. [ref.footnote]
+ source/index.rst:10: WARNING: Footnote [*] is not referenced. [ref.footnote]
+ source/index.rst:11: WARNING: Footnote [#] is not referenced. [ref.footnote]

edit: the failures against Docutils HEAD appear unrelated to this PR

@@ -294,23 +295,40 @@ class UnreferencedFootnotesDetector(SphinxTransform):
Detect unreferenced footnotes and emit warnings
"""

default_priority = 200
default_priority = Footnotes.default_priority + 1
Copy link
Member

Choose a reason for hiding this comment

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

Could this change of priority be breaking extensions that would have relied on the absolute priority?

I'm worried that some extensions could have fixed their internal footnotes by adding a priority UnreferencedFootnotesDetector.default_priority - 1 which would now hit the same priority as the footnotes transformation.

Copy link
Member Author

@chrisjsewell chrisjsewell Aug 4, 2024

Choose a reason for hiding this comment

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

Well, I think there would be a very slim chance of that 😅, and obviously this transform doesn't actually make any changes to the doctree so it won't have any indirect effect on other transforms.

If you want, I can change the existing transform to a "no-op", then add this as an additional new transform?

It's of note, that I'm doing this as a result of executablebooks/MyST-Parser#931; so that I can remove ad-hoc fixes in myst-parser and just let sphinx handle warnings

Copy link
Member

@picnixz picnixz Aug 5, 2024

Choose a reason for hiding this comment

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

No let's not overcomplicate the things (i.e. keep what you've written, it's ok). But add a CHANGELOG entry for that by saying that the priority has been changed (I don't know if we have it, but a table with the transformations and post-transformations priorities would be nice...).

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 know if we have it, but a table with the transformations and post-transformations priorities would be nice...

Nope I don't believe so, it's on the TODO list 😄

sphinx/transforms/__init__.py Show resolved Hide resolved
Copy link
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

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

Other than the CHANGELOG entry, it's good for me. I'm travelling this week so I won't be active (neither here nor on CPython) but I may reply to some questions in the evenings (EUW time).

@chrisjsewell chrisjsewell merged commit 05cc39d into sphinx-doc:master Aug 5, 2024
18 of 19 checks passed
@chrisjsewell chrisjsewell deleted the improve-unref-footnote-warnings branch August 5, 2024 08:28
@AA-Turner
Copy link
Member

AA-Turner commented Aug 5, 2024

@chrisjsewell quickly on commit style -- please avoid '[blah]' in favour of either unprefixed or prefix with a component (e.g. 'intersphinx:').

I didn't get a chance to review this before merge, but will have a look at some pont.

A

@chrisjsewell
Copy link
Member Author

please avoid '[blah]' in favour of either unprefixed or prefix with a component (e.g. 'intersphinx:').

yep no problem, perhaps we could add a little more to https://www.sphinx-doc.org/en/master/internals/contributing.html#contribute-code to suggest best practices for such things.
Previously I have created a reasonably comprehensive guide for myst-parser etc, but obviously the one for sphinx would be a bit different 😄 https://github.com/executablebooks/.github/blob/master/CONTRIBUTING.md#opening-a-pull-request

chrisjsewell added a commit to executablebooks/MyST-Parser that referenced this pull request Aug 5, 2024
Footnotes are now parsed similar to the corresponding restructuredtext, in that resolution (between definitions and references) and ordering is now deferred to transforms on the doctree.

This allows for the proper interaction with other docutils/sphinx transforms, including those that perform translations.

In addition, an upstream improvement to unreferenced footnote definitions is also added here: sphinx-doc/sphinx#12730, so that unreferenced and duplicate definitions are correctly warned about, e.g.:

```
source/index.md:1: WARNING: Footnote [1] is not referenced. [ref.footnote]
source/index.md:4: WARNING: Duplicate footnote definition found for label: 'a' [ref.footnote]
```

It is of note that warnings for references with no corresponding definitions are deferred to docutils to handle, e.g. for `[^a]` with no definition:

```
source/index.md:1: ERROR: Too many autonumbered footnote references: only 0 corresponding footnotes available. [docutils]
source/index.md:1: ERROR: Unknown target name: "a". [docutils]
```

These warning messages are a little obscure, and it would be ideal that one clear warning was emitted for the issue.
However, it is non-trivial in this extension; to both suppress the existing warnings, and then replace them with a better one,
so for now we don't do it here, and ideally this would be improved upstream in docutils.
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 5, 2024
@AA-Turner AA-Turner added this to the 8.1.x milestone Oct 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants