Type Stubs for Chardet

5.0.4.6 · active · verified Sat Apr 11

types-chardet is a PEP 561 type stub package that provides static type information for the `chardet` library. It allows type checkers like Mypy, Pyright, or PyCharm to perform static analysis and detect type-related errors in code that uses `chardet` for character encoding detection, without affecting the runtime behavior of the application. This package is maintained as part of the broader Typeshed project. The current version is 5.0.4.6, and it follows the release cadence of the Typeshed project, which releases updates for third-party stubs regularly.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic usage of the `chardet` library. When `types-chardet` is installed, a static type checker will be able to verify the types of arguments and return values, such as ensuring that `chardet.detect()` is called with `bytes` data.

import chardet

def analyze_encoding(data: bytes) -> dict:
    """Detects the encoding of a byte string."""
    result = chardet.detect(data)
    print(f"Detected encoding: {result['encoding']} with confidence {result['confidence']}")
    return result

# Example usage with bytes data
sample_data = "München ist die Hauptstadt Bayerns.".encode("windows-1252")
analysis = analyze_encoding(sample_data)

# Without types-chardet, a type checker might not flag if you passed
# a 'str' instead of 'bytes' to chardet.detect. With it, it would.
# try_wrong_data = analyze_encoding("This is a string") # Mypy/Pyright would flag this as an error

view raw JSON →