Typing Stubs for CommonMark

0.9.2.20250330 · active · verified Fri Apr 17

types-commonmark provides official typing stubs for the `commonmark` Python library, enabling static type checkers like MyPy and Pyright to analyze `commonmark` code with full type information. This package is part of the `typeshed` project. The current version is 0.9.2.20250330, with releases typically tied to `commonmark` updates or `typeshed` maintenance cycles.

Common errors

Warnings

Install

Imports

Quickstart

To leverage `types-commonmark`, install both `commonmark` and `types-commonmark`. Your type checker (e.g., MyPy) will automatically use the installed stubs to provide type safety and auto-completion for `commonmark` functions and classes. The example demonstrates typical `commonmark` usage where `types-commonmark` adds static type checking benefits without changing the runtime code. Run this code with `commonmark` installed, then run a type checker like `mypy` on the file to see the stubs in action.

import commonmark
from commonmark.node import Node

def process_markdown(text: str) -> str:
    """Parses markdown and renders it to HTML."""
    parser = commonmark.Parser()
    renderer = commonmark.HtmlRenderer()

    # The type checker (e.g., MyPy) will use types-commonmark
    # to understand that 'root_node' is a Node object, and 'render' returns str.
    root_node: Node = parser.parse(text)
    html_output: str = renderer.render(root_node)
    return html_output

markdown_text = "# Hello, Markdown!\n\nThis is some *formatted* text."
html = process_markdown(markdown_text)
print(html)
# Expected output (simplified example based on commonmark rendering):
# <h1>Hello, Markdown!</h1>
# <p>This is some <em>formatted</em> text.</p>

view raw JSON →