Typing Stubs for webencodings
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
- gotcha Installing `types-webencodings` does not provide the runtime functionality of the `webencodings` library. It only provides type stubs for static analysis. You must install `webencodings` separately if you intend to run the code.
- gotcha Do not attempt to import symbols directly from `types_webencodings` (e.g., `from types_webencodings import lookup`). Type stub packages are not designed to be imported at runtime. Imports should always target the actual runtime library, `webencodings`.
- gotcha The versioning scheme for `types-*` packages from Typeshed is `X.Y.Z.YYYYMMDD`. The `X.Y.Z` typically corresponds to the version of the runtime package (`webencodings`) the stubs are targeting, while `YYYYMMDD` indicates the release date of the stub package from Typeshed. Incompatibility can occur if the stub package version `X.Y.Z` significantly diverges from your installed `webencodings` version.
- breaking While Typeshed aims to minimize breaking changes, any update to a stub package can introduce changes that might cause your code to fail type checking, even if the runtime behavior of 'webencodings' hasn't changed. This is inherent to the nature of static typing.
Install
-
pip install types-webencodings
Imports
- lookup
from webencodings import lookup
- Encoding
from webencodings import Encoding
Quickstart
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