Typing Stubs for Cryptography
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
- gotcha `types-cryptography` only provides type hints. You *must* install the actual `cryptography` library (`pip install cryptography`) for your code to run.
- gotcha Do not attempt to import symbols directly from `types_cryptography` or `cryptography-stubs`. This package is purely for type checkers and does not expose runtime objects. Imports should always be from `cryptography` itself.
- gotcha The version of `types-cryptography` might not always be perfectly in sync with the very latest release of `cryptography`. This can occasionally lead to minor type checking errors (e.g., missing attributes for newly added features) until the stubs are updated.
Install
-
pip install types-cryptography
Imports
- Fernet
from cryptography.fernet import Fernet
Quickstart
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