代码之家  ›  专栏  ›  技术社区  ›  Mad Physicist

如何通过sphinx链接到Python 3文档中的typecontextmanager标签

  •  2
  • Mad Physicist  · 技术社区  · 7 年前

    https://docs.python.org/3/library/stdtypes.html#typecontextmanager . 我正在尝试使用 :ref:

    我跑了 python -m sphinx.ext.intersphinx https://docs.python.org/3/objects.inv . 结果表明,除其他外:

    ...
    std:label
        23acks                    Acknowledgements             : whatsnew/2.3.html#acks
        23section-other           Other Changes and Fixes      : whatsnew/2.3.html#section-other
    ...
        typebytearray             Bytearray Objects            : library/stdtypes.html#typebytearray
        typebytes                 Bytes Objects                : library/stdtypes.html#typebytes
        typecontextmanager        Context Manager Types        : library/stdtypes.html#typecontextmanager
        typeiter                  Iterator Types               : library/stdtypes.html#typeiter
        typememoryview            Memory Views                 : library/stdtypes.html#typememoryview
    ...
    

    intersphinx_mapping

    intersphinx_mapping = {
        'python': ('https://docs.python.org/3', None),
    }
    

    我使用以下指令:

    :ref:`context manager <python:typecontextmanager>`
    

    这似乎指向了正确的标签,但我得到了以下警告:

    WARNING: undefined label: python:typecontextmanager (if the link has no caption the label must precede a section header)
    

    :参考: 被字符串替换 context manager

    我错过了什么?

    注1

    :ref:`with <python:with>` ,应该指向 https://docs.python.org/3/reference/compound_stmts.html#with std:label

         with                       The with statement          : reference/compound_stmts.html#with
    

    可能不是百分之百相关,但我可以链接到 :py:meth:`~contextmanager.__enter__`

    1 回复  |  直到 7 年前
        1
  •  3
  •   Steve Piercy    7 年前

    这两种都适合我。

    :ref:`python:typecontextmanager`
    :ref:`typecontextmanager <python:typecontextmanager>`
    

    另外,我最近在金字塔的文档中添加了上下文管理器 Glossary ,我认为这很好地解释了它们是什么。以下是reST来源:

    :ref:`With Statement Context Managers <python:context-managers>`
    :ref:`with <python:with>`