Typing stubs for Babel
types-babel provides PEP 561 type stubs for the babel package. It allows type-checking tools like MyPy, Pyright, and PyCharm to analyze code that uses the babel library. The current version is 2.11.0.15. As part of the Typeshed project, updates to these stubs are frequent, driven by community contributions and are released to PyPI up to once a day when changes are made.
Warnings
- breaking The `babel` package itself now includes its own type annotations starting from version 2.12.1. If you are using `babel` version 2.12.1 or newer, you should uninstall `types-babel` to avoid conflicts and ensure accurate type checking, as the native types are preferred.
- gotcha Version compatibility is crucial. The version number of `types-babel` (e.g., `2.11.0.x`) corresponds to the version of the `babel` runtime package it stubs (e.g., `babel==2.11.*`). Using mismatched versions can lead to incorrect or incomplete type checking results.
- gotcha `types-babel` is currently marked as a 'partial' stub package. This means that not all modules, classes, or functions within the `babel` library may have complete type annotations. Type checkers might infer `Any` for unannotated parts, potentially missing errors.
- gotcha Typeshed, the source for `types-babel`, supports specific Python versions (currently 3.10 to 3.14). Ensure your Python environment is compatible with both the `types-babel` stubs and the `babel` runtime library. For example, `babel` 2.15 and newer requires Python 3.8 or newer.
Install
-
pip install types-babel
Imports
- format_date
from babel.dates import format_date
Quickstart
from datetime import date
from babel.dates import format_date
def main():
today = date(2026, 4, 12)
# types-babel provides type information for functions like format_date
formatted_today: str = format_date(today, locale='en_US')
print(f"Today is: {formatted_today}")
# Example of a type error (uncomment to see mypy in action):
# invalid_type: int = format_date(today, locale='en_US')
if __name__ == '__main__':
main()
# To run type checking, install mypy and babel:
# pip install babel mypy
# Then, run: mypy your_script_name.py