types-channels is a stub package providing type hints for the `channels` library, which enables asynchronous communication for Django. It allows static type checkers like MyPy to understand the types used within `channels` code, improving code quality and maintainability. It is part of the `typeshed` project and is currently at version 4.3.0.20260408, with releases frequently updated to track upstream `channels` and `typeshed` changes.
pip install types-channelsVerified import paths — ran on the pinned version, not inferred.
To use `types-channels`, install it alongside the `channels` library and a type checker like MyPy (`pip install channels types-channels mypy`). Then, simply write your `channels` code with type annotations. MyPy will automatically pick up the stubs from `types-channels` when checking your code. For instance, save the example consumer above as `my_consumer.py` and run `mypy my_consumer.py` in your terminal. If correctly configured, MyPy should report no errors, validating your type hints based on the installed stubs.
Always import symbols from the actual `channels` library (e.g., `from channels.routing import ProtocolTypeRouter`), not `types_channels`.
Ensure `types-channels` and `channels` are reasonably compatible. Check `typeshed` changelogs or `channels` release notes for guidance on compatibility if you encounter type errors. Consider pinning versions (e.g., `channels<5` for current stubs).
Verify MyPy's `follow_imports` setting. The default `normal` is usually sufficient. If MyPy reports 'Library 'channels' has no type information available', ensure `types-channels` is installed in the environment MyPy is checking.