Typing Stubs for docutils
types-docutils provides high-quality type hints and stub files for the docutils library, enabling static type checkers like MyPy to validate code that uses docutils. It's part of the typeshed project, which maintains external type stubs for various popular Python packages. The current version is 0.22.3.20260408, with releases tied to updates in typeshed.
Warnings
- gotcha types-docutils only provides type stubs, not the docutils library itself. You must install the actual 'docutils' package separately for your code to run.
- gotcha types-docutils is meant for static analysis (e.g., with MyPy) and does not need to be imported directly in your runtime code. Its presence in your environment is sufficient for type checkers.
- gotcha The versioning scheme for typeshed stubs includes both the library version and a date component (e.g., 0.22.3.20260408). This can sometimes lead to confusion regarding direct compatibility with specific docutils release versions.
- gotcha This package requires Python 3.10 or higher. Using it with older Python versions might lead to installation failures or incorrect type checking behavior.
Install
-
pip install types-docutils
Imports
- publish_string
from docutils.core import publish_string
Quickstart
import os
from docutils.core import publish_string
# Example RST document
rst_document: str = """
.. role:: red
:red:`This is red text.`
.. admonition:: Note
This is an admonition block.
"""
# publish_string is a core docutils function to process RST
# With types-docutils installed, type checkers can validate the arguments and return type.
html_output: str = publish_string(
source=rst_document,
writer_name="html",
settings_overrides={'initial_header_level': 2}
)
print("Generated HTML snippet (first 200 chars):\n", html_output[:200])
# To see type checking in action, save this to 'app.py'
# and run 'mypy app.py' after 'pip install types-docutils docutils mypy'.
# Introduce a deliberate error, e.g., 'writer_name=123', and mypy will flag it.