Typing Stubs for MarkupSafe
This is a PEP 561 type stub package providing external type annotations for the MarkupSafe library. It enables static type-checking tools like MyPy, PyCharm, or Pyright to analyze code that uses MarkupSafe. The current version is 1.1.10. These stub packages are automatically released by the Typeshed project, ensuring they follow typing specification standards.
Warnings
- breaking The `MarkupSafe` library itself includes type annotations since version 2.0. If you are using `MarkupSafe` 2.0 or newer, you MUST uninstall `types-MarkupSafe` to avoid conflicts and ensure correct type checking, as `types-MarkupSafe` is intended for `MarkupSafe` versions older than 2.0.
- gotcha Ensure the version of `types-markupsafe` you install is compatible with your installed `markupsafe` version. The `types-markupsafe` version number (excluding the last part) aims to match the `markupsafe` version it provides stubs for. `types-markupsafe 1.1.x` is for `markupsafe 1.1.x`.
- gotcha `types-markupsafe` is a stub-only package and provides no runtime functionality. It should not be imported directly in your application code, nor does it expose any objects or functions at runtime. Its sole purpose is to be consumed by static type checkers.
Install
-
pip install types-markupsafe
Imports
- Markup
from markupsafe import Markup
- escape
from markupsafe import escape
Quickstart
# 1. Install MarkupSafe (if not already present)
pip install MarkupSafe==1.1.1
# 2. Install types-markupsafe (for MarkupSafe versions < 2.0)
pip install types-markupsafe
# 3. Create a Python file (e.g., app.py) to use MarkupSafe
# Note: This quickstart is for MarkupSafe < 2.0
from markupsafe import escape, Markup
def process_user_input(user_input: str) -> Markup:
escaped_input = escape(user_input)
return Markup(f"<p>You entered: {escaped_input}</p>")
html_output = process_user_input("<script>alert('xss')</script>")
print(html_output)
# 4. Run a type checker (e.g., MyPy)
# pip install mypy
# mypy app.py