Skip to content

Commit 23ac4df

Browse files
committed
Add note about Sphinx name resolution for Python objects.
Almost nobody knows that Sphinx normally pays no attention to your import statements.
1 parent 4197822 commit 23ac4df

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

restructuredtext/style.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,38 @@ 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+
.. 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.
388+
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.
390+
359391
Namespace resolution
360392
""""""""""""""""""""
361393

0 commit comments

Comments
 (0)