Typing Stubs for Mypy Extensions

1.1.0.20260408 · active · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

This example demonstrates how to use `LiteralString` from `mypy_extensions` to enforce type safety against non-literal strings. The `types-mypy-extensions` package provides the necessary type stubs for a type checker like Mypy to correctly analyze these type hints.

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.")

view raw JSON →