Typing Stubs for webencodings

0.5.0.20260408 · active · verified Thu Apr 09

types-webencodings provides static type annotations (stubs) for the 'webencodings' library. Maintained by the Typeshed project, it enables type checkers like Mypy and Pyright to perform static analysis, type inference, and provide autocompletion for code that uses 'webencodings'. These stub packages are automatically released by Typeshed's internal machinery, typically up to once a day, to keep pace with changes in the runtime libraries.

Warnings

Install

Imports

Quickstart

This example demonstrates how to use the 'webencodings' library with type hints. After installing both `webencodings` and `types-webencodings`, a type checker like Mypy will use the stubs to validate the type annotations in the code. The `lookup` function and `Encoding` type are imported directly from `webencodings`, not `types_webencodings`.

import sys
from webencodings import lookup, Encoding

def get_encoding_name(label: str) -> str | None:
    """Looks up an encoding label and returns its canonical name, if found."""
    encoding: Encoding | None = lookup(label)
    if encoding:
        return encoding.name
    return None

if __name__ == "__main__":
    # Example usage
    utf8_name = get_encoding_name("utf-8")
    print(f"'utf-8' canonical name: {utf8_name}")

    unknown_name = get_encoding_name("not-an-encoding")
    print(f"'not-an-encoding' canonical name: {unknown_name}")

    # To type-check this code, save it as `main.py` and run:
    # pip install mypy webencodings types-webencodings
    # mypy main.py

view raw JSON →