Typing Stubs for Cryptography

3.3.23.2 · active · verified Thu Apr 09

This package provides static type checking stubs for the `cryptography` library, enabling tools like MyPy to verify type correctness in Python code that uses `cryptography`. It is part of the broader Typeshed project, which maintains a repository of type stubs for many popular Python libraries. As of this entry, the current version is 3.3.23.2, with updates typically following `cryptography` releases.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a basic usage of the `cryptography` library. `types-cryptography` is installed to allow static type checkers like MyPy to verify the type correctness of this code. After installing `cryptography`, `types-cryptography`, and `mypy`, you can run `mypy <your_script_name>.py` to check for type errors.

import os
from cryptography.fernet import Fernet

# Example usage of cryptography, for which types-cryptography provides stubs
key = Fernet.generate_key()
f = Fernet(key)

message: bytes = b"my secret data"
encrypted_message: bytes = f.encrypt(message)
decrypted_message: bytes = f.decrypt(encrypted_message)

print(f"Original: {message}")
print(f"Encrypted: {encrypted_message}")
print(f"Decrypted: {decrypted_message}")

# To type-check this file:
# 1. pip install cryptography types-cryptography mypy
# 2. mypy your_script_name.py

view raw JSON →