Colorama Typing Stubs
types-colorama provides PEP 561 compliant type stubs for the colorama library, enabling static type checking for colored terminal output. It is part of the typeshed project and aims to provide accurate annotations for colorama==0.4.*. Updates are frequent as it's part of the typeshed project, which releases stub packages to PyPI automatically, sometimes daily.
Warnings
- gotcha The `types-colorama` package aims to provide accurate type annotations for `colorama==0.4.*`. Using it with significantly different major or minor versions of the `colorama` runtime library may result in incorrect type checking errors or missed type issues due to API discrepancies.
- gotcha `types-colorama` is a stub-only package and provides no runtime functionality. It should not be imported or used directly in your application's code. Its sole purpose is to provide type hints for static analysis tools like MyPy or Pyright when you import and use the actual `colorama` library.
- gotcha For `colorama` to function correctly and display colored output on Windows terminals, `colorama.init()` (or `colorama.just_fix_windows_console()` for `colorama >= 0.4.6`) must be called at the start of your program. While `types-colorama` will correctly type check these calls, its omission in your runtime code will lead to plain text output on Windows, not a type error.
- gotcha Stubs are maintained in the typeshed repository and released frequently. While this generally means up-to-date types, occasional breaking changes in type definitions may occur between stub package versions, even if the runtime library's API has not changed. This can cause your type checker to report new errors.
Install
-
pip install types-colorama
Imports
- init
from colorama import init
- Fore
from colorama import Fore
- Back
from colorama import Back
- Style
from colorama import Style
Quickstart
import sys
from colorama import init, Fore, Style
# Initialize Colorama. For Windows, this is essential for ANSI escape codes.
# autoreset=True means that colors are automatically reset after each print statement.
init(autoreset=True)
print(Fore.RED + "This text is red!")
print(Fore.GREEN + "This text is green and will reset.")
print("This text is back to normal.")
# You can also use Style.RESET_ALL explicitly for fine-grained control
print(Fore.YELLOW + "Yellow text " + Style.BRIGHT + "and bright, then " + Style.RESET_ALL + "normal again.")
# To verify type checking, you could run mypy on this file:
# $ pip install mypy
# $ mypy your_script_name.py