Typing Stubs for html5lib

1.1.11.20260408 · active · verified Thu Apr 09

This package provides static type checking stubs for the `html5lib` library, allowing type checkers like MyPy to validate usage of `html5lib` APIs. It's part of the `typeshed` project, which continuously maintains and releases type stubs for many popular Python libraries. New versions are released frequently, often coinciding with upstream library updates or Python version changes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic usage of the `html5lib` library. When `types-html5lib` is installed, a static type checker (like MyPy) will use these stubs to provide type validation and autocompletion for `html5lib`'s APIs, without any explicit import statements for the stub package itself.

import html5lib
from html5lib.treebuilders import getTreeBuilder
import io
from typing import TextIO

# The types-html5lib package provides static type checking for html5lib.
# You use html5lib normally, and a type checker (like mypy) will use the stubs
# to validate your code.

# Example: Parsing HTML from a string
html_string: str = "<html><body><h1>Hello World</h1><p>This is a test.</p></body></html>"

# Initialize the parser with a tree builder (e.g., 'dom' for a DOM-like tree)
parser = html5lib.HTMLParser(tree=getTreeBuilder("dom"))

# Parse the HTML string
document = parser.parse(html_string)

# A type checker using types-html5lib would know the methods available on 'document'
# based on the 'dom' tree builder. For example, for a DOM tree:
if hasattr(document, 'getElementsByTagName'):
    h1_elements = document.getElementsByTagName('h1')
    if h1_elements:
        print(f"First H1 tag content: {h1_elements[0].firstChild.nodeValue}")

# Example: Parsing from a file-like object
html_file_like: TextIO = io.StringIO("<html><head><title>Registry Entry</title></head><body></body></html>")
document_from_file = parser.parse(html_file_like)

if hasattr(document_from_file, 'getElementsByTagName'):
    title_elements = document_from_file.getElementsByTagName('title')
    if title_elements:
        print(f"Document title from file: {title_elements[0].firstChild.nodeValue}")

print("html5lib parsing completed with type-checking support provided by types-html5lib.")

view raw JSON →