docutils-stubs
docutils-stubs provides PEP 561 type stubs for the docutils library, enabling static type checking tools like MyPy to verify code that uses docutils. It helps catch type-related errors before runtime. The current version is 0.0.22, with releases typically following updates to the underlying docutils library or bug fixes.
Common errors
-
ModuleNotFoundError: No module named 'docutils'
cause You have installed `docutils-stubs` but not the actual `docutils` library.fixRun `pip install docutils` in addition to `pip install docutils-stubs`. -
ImportError: cannot import name 'publish_string' from 'docutils_stubs.core'
cause Attempting to import directly from the stub package `docutils_stubs` instead of the original `docutils` library.fixChange your import statement to `from docutils.core import publish_string`. Stub packages are not meant for runtime imports. -
error: Module 'docutils.core' has no attribute 'missing_function' [attr-defined]
cause The `docutils-stubs` package might be outdated or does not include type information for a specific, potentially new or non-standard, attribute/function in `docutils`.fixEnsure both `docutils` and `docutils-stubs` are up-to-date (`pip install --upgrade docutils docutils-stubs`). If the issue persists for a standard feature, consider reporting it on the `docutils-stubs` GitHub page.
Warnings
- gotcha Installing `docutils-stubs` does NOT install `docutils`. You must install `docutils` separately for your code to run.
- gotcha Do not import directly from `docutils_stubs`. Stub packages are meant for type checkers, not for runtime imports.
- gotcha Type stubs might occasionally lag behind the `docutils` library itself. If you're using a very new feature or a pre-release version of `docutils`, the stubs might not yet include type information for it.
Install
-
pip install docutils-stubs
Imports
- publish_string
from docutils_stubs.core import publish_string
from docutils.core import publish_string
- RSTParser
from docutils_stubs.parsers.rst import Parser as RSTParser
from docutils.parsers.rst import Parser as RSTParser
Quickstart
from docutils.core import publish_string
rst_content = """
My Document
===========
This is a paragraph.
* A list item
* Another item
"""
# docutils-stubs provides type hints for this call
html_output = publish_string(
source=rst_content,
writer_name='html'
)
print(html_output[:100] + '...') # Print a snippet of the generated HTML