{"id":4299,"library":"types-chardet","title":"Type Stubs for Chardet","description":"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.","status":"active","version":"5.0.4.6","language":"en","source_language":"en","source_url":"https://github.com/python/typeshed/tree/main/stubs/chardet","tags":["type stubs","typing","chardet","encoding detection","typeshed"],"install":[{"cmd":"pip install types-chardet","lang":"bash","label":"Install types-chardet"}],"dependencies":[{"reason":"This package provides type stubs for the 'chardet' library. The 'chardet' library itself must be installed to provide the runtime functionality. The latest 'chardet' versions (7.x) require Python 3.10+.","package":"chardet","optional":false}],"imports":[{"symbol":"detect","correct":"from chardet import detect"},{"symbol":"UniversalDetector","correct":"from chardet import UniversalDetector"}],"quickstart":{"code":"import chardet\n\ndef analyze_encoding(data: bytes) -> dict:\n    \"\"\"Detects the encoding of a byte string.\"\"\"\n    result = chardet.detect(data)\n    print(f\"Detected encoding: {result['encoding']} with confidence {result['confidence']}\")\n    return result\n\n# Example usage with bytes data\nsample_data = \"München ist die Hauptstadt Bayerns.\".encode(\"windows-1252\")\nanalysis = analyze_encoding(sample_data)\n\n# Without types-chardet, a type checker might not flag if you passed\n# a 'str' instead of 'bytes' to chardet.detect. With it, it would.\n# try_wrong_data = analyze_encoding(\"This is a string\") # Mypy/Pyright would flag this as an error\n","lang":"python","description":"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."},"warnings":[{"fix":"Uninstall `types-chardet` using `pip uninstall types-chardet` if your `chardet` version is 5.1.0 or newer. Rely on the inline types provided by `chardet` itself.","message":"The `chardet` library, starting from version 5.1.0, includes its own inline type annotations. If you are using `chardet>=5.1.0`, you should uninstall `types-chardet` to avoid potential conflicts with the package's native type information.","severity":"breaking","affected_versions":"chardet >= 5.1.0"},{"fix":"Ensure `chardet` is listed as a runtime dependency and installed alongside `types-chardet`.","message":"`types-chardet` provides only type stubs; it does not install the actual `chardet` runtime library. For your code to run, `chardet` must be installed separately (e.g., `pip install chardet`).","severity":"gotcha","affected_versions":"All versions"},{"fix":"For optimal compatibility, consider pinning `types-chardet` with the same major.minor version range as `chardet` (e.g., `chardet>=5.0,<5.1` and `types-chardet>=5.0,<5.1`) or relying on the latest available stubs, understanding that stubs might occasionally lag behind or precede runtime changes.","message":"The versioning of `types-chardet` follows Typeshed's policy, where the first three parts align with the `chardet` version it stubs (e.g., `5.0.4` for `chardet` 5.0.x), followed by a Typeshed-specific identifier. Direct version pinning between `chardet` and `types-chardet` can be complex and might sometimes lead to conflicts or missed type improvements.","severity":"gotcha","affected_versions":"All versions"},{"fix":"If upgrading to `chardet` 7.x, ensure your project's Python version is 3.10 or newer. Review `chardet`'s release notes for any subtle behavioral changes, although the API for basic detection (`chardet.detect()`) remains consistent.","message":"`chardet` underwent a significant rewrite in version 7.x, introducing substantial performance improvements, new features (like MIME type detection), and dropping support for older Python versions (now requiring Python 3.10+). While the public API remained largely compatible, be aware of the underlying changes and Python version requirements if migrating to `chardet` 7.x.","severity":"gotcha","affected_versions":"chardet < 7.0.0"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}