Typing Stubs for Python-Markdown

3.10.2.20260211 · active · verified Sun Apr 05

types-markdown is a type stub package that provides static type annotations for the popular `markdown` library (Python-Markdown). It enables static type checkers like MyPy to validate code that uses the `markdown` library, improving code quality and catching potential errors before runtime. This package is automatically generated and released from the `typeshed` project and follows a versioning scheme tied to the `markdown` runtime library version.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the `markdown` library with the `types-markdown` stubs implicitly providing type hints. The `TYPE_CHECKING` block shows how type checkers benefit from the stubs, ensuring correct usage of the `markdown` library's API.

import markdown
from typing import TYPE_CHECKING

# The actual Markdown library is imported for runtime use
# types-markdown provides the type hints for this 'markdown' module

markdown_text = """
# Hello, Markdown!

This is a **bold** paragraph with an [inline link](https://example.com).

- List item 1
- List item 2
"""

# Convert Markdown to HTML
html_output: str = markdown.markdown(markdown_text)

print(html_output)

if TYPE_CHECKING:
    # Example of type checking a function from the markdown library
    # This part only runs during type checking, not runtime
    from markdown import Markdown
    md_parser: Markdown = markdown.Markdown(extensions=['fenced_code'])
    typed_html: str = md_parser.convert(markdown_text)
    # The type checker would ensure 'convert' returns a string and 'Markdown' is correctly instantiated

view raw JSON →