Skip to content

Commit 8ef07e6

Browse files
committed
Make the note on Sphinx Python name resolution more prominent.
Almost nobody knows that Sphinx normally pays no attention to your import statements, and I didn't even notice the existing text on this when I went to add new text on it.
1 parent 4197822 commit 8ef07e6

1 file changed

Lines changed: 30 additions & 6 deletions

File tree

restructuredtext/style.rst

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -356,13 +356,37 @@ Objects can be referenced with these roles:
356356
- ``:py:data:`pkg.mod.VARIABLE``` to reference a module-level variable ``VARIABLE`` in ``pkg.mod``.
357357
- ``:py:const:`pkg.mod.CONSTANT``` to reference a module-level *constant* ``CONSTANT`` in ``pkg.mod``.
358358

359-
Namespace resolution
360-
""""""""""""""""""""
359+
.. note::
360+
Types in docstrings do *not* respect imports in the file, and instead are resolved using `Sphinx's own target-resolution rules <https://www.sphinx-doc.org/en/master/usage/domains/python.html#target-resolution>`__.
361+
In other words,
362+
363+
.. code-block::
364+
365+
from collections.abc import Sequence
366+
367+
def run_stuff(stuff):
368+
"""Run some stuff.
369+
370+
Parameters
371+
----------
372+
stuff : `Sequence` [`str`]
373+
Stuff to run.
374+
375+
does not work, but
376+
377+
.. code-block::
378+
379+
from collections.abc import Sequence
380+
381+
def run_stuff(stuff):
382+
"""Run some stuff.
383+
384+
Parameters
385+
----------
386+
stuff : `~collections.abc.Sequence` [`str`]
387+
Stuff to run.
361388
362-
In these examples, the full namespace of each Python object is specified.
363-
In some contexts, Sphinx may be able to identify the reference object without the full namespace.
364-
For example in class docstrings, references to methods or attributes in the same class can be made by name alone.
365-
See the `Sphinx documentation <http://sphinx-doc.org/domains.html#cross-referencing-python-objects>`_ for more details on object resolution.
389+
does. See the link above for how to make relative links to types; the syntax is not the same as Python's, and it operates on the documentation hierarchy, not the module hierarchy.
366390

367391
.. _rst-cpp-links:
368392

0 commit comments

Comments
 (0)