Typing Stubs for Mypy Extensions
This package provides static type checking stubs for the `mypy-extensions` library. It enables type checkers like Mypy to understand the types defined in `mypy-extensions`, such as `LiteralString`, `TypedDict`, and `NoReturn`, particularly when those features are not yet available in the standard `typing` module for the target Python version. The current version is 1.1.0.20260408, released as part of the `typeshed` project, with releases generally aligned with `typeshed` updates.
Common errors
-
ModuleNotFoundError: No module named 'mypy_extensions'
cause You have installed 'types-mypy-extensions' (the stub package) but not the actual runtime library 'mypy-extensions'. Stubs do not provide runtime functionality.fixInstall the runtime package: `pip install mypy-extensions`. -
ModuleNotFoundError: No module named 'types_mypy_extensions'
cause Stub packages like 'types-mypy-extensions' are not meant for direct import in Python code. They are consumed by type checkers.fixImport symbols from the actual runtime library, e.g., `from mypy_extensions import LiteralString`. The `types-mypy-extensions` package is solely for type checking tools like Mypy.
Warnings
- gotcha This is a stub-only package. It provides type annotations for type checkers but does not contain any runtime code. You must install the actual 'mypy-extensions' package separately for your code to run successfully.
- gotcha The 'types-mypy-extensions' package itself requires Python >= 3.10 to install and use. This means your development/type-checking environment must run Python 3.10 or newer, even if your target runtime environment uses an older Python version with 'mypy-extensions'.
- gotcha Many features provided by 'mypy-extensions' (e.g., TypedDict, NoReturn, LiteralString) are now part of the standard `typing` module in modern Python versions (e.g., TypedDict in 3.8+, NoReturn in 3.6+, LiteralString in 3.11+). Using `mypy-extensions` might be unnecessary or lead to confusion if your project primarily targets these newer Python versions.
Install
-
pip install types-mypy-extensions
Imports
- LiteralString
from types_mypy_extensions import LiteralString
from mypy_extensions import LiteralString
- TypedDict
from types_mypy_extensions import TypedDict
from mypy_extensions import TypedDict
Quickstart
from mypy_extensions import LiteralString
def process_query(query: LiteralString) -> None:
print(f"Executing safe query: {query}")
def get_user_input() -> str:
# Simulate user input which is not guaranteed to be a literal string
return 'SELECT * FROM users; DROP TABLE users;' # Malicious input
# This is a safe literal string
safe_query: LiteralString = "SELECT id, name FROM users WHERE active = TRUE;"
process_query(safe_query)
# If type checked by mypy (with types-mypy-extensions installed),
# the following line is expected to fail because get_user_input() returns 'str', not 'LiteralString'.
# mypy will detect the type mismatch, preventing potential SQL injection if 'query' was used directly.
# Uncommenting the line below and running mypy should show a type error.
# process_query(get_user_input())
print("Run 'mypy your_file.py' to see type checking results.")