Typing Stubs for Bleach

6.3.0.20260408 · active · verified Sat Apr 11

types-bleach provides static type checking stubs for the 'bleach' HTML sanitizing library. It enables type checkers like MyPy or Pyright to validate usage of `bleach` functions and classes without providing any runtime functionality itself. As part of typeshed, these packages are automatically released, potentially up to once a day, reflecting updates to the underlying library's API or improvements in type accuracy.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the 'bleach' library. When 'types-bleach' is installed, a static type checker will use its stubs to provide type validation and autocompletion for `bleach` functions like `clean()` and `linkify()`.

import bleach

# Example usage of bleach with type hints
def process_html(user_input: str) -> str:
    allowed_tags = ['a', 'p', 'b', 'i']
    allowed_attrs = {'a': ['href', 'title']}
    
    # The type checker will use types-bleach stubs to validate this call
    cleaned_html: str = bleach.clean(
        user_input,
        tags=allowed_tags,
        attributes=allowed_attrs,
        strip=True
    )
    return cleaned_html

html_input = '<p>Hello, <script>alert("XSS!");</script><a href="http://example.com">world</a>!</p>'
output = process_html(html_input)
print(output)
# Expected output with type checking: <p>Hello, <a href="http://example.com">world</a>!</p>

view raw JSON →