Type Stubs for pysaml2
types-pysaml2 provides PEP 561 compliant type stubs for the pysaml2 library, enabling static type checking tools like MyPy to validate code that uses pysaml2. It aims to provide accurate type annotations for the current state of pysaml2. Releases are generally infrequent, driven by bug fixes or significant changes in the underlying pysaml2 library.
Warnings
- gotcha types-pysaml2 does not contain any runtime code; it only provides type annotations. Ensure you have `pysaml2` installed as the actual runtime library. Without `pysaml2`, `types-pysaml2` serves no functional purpose beyond providing stubs for a non-existent package.
- gotcha Type hints might be incorrect or incomplete if the version of `types-pysaml2` does not sufficiently match the installed version of `pysaml2`. While `types-pysaml2` tries to stay current, significant API changes in `pysaml2` might not be immediately reflected.
- breaking The type hint for the `LogoutResult` tuple was corrected from 3 to 4 elements in v1.0.2 to accurately reflect `pysaml2`'s actual return signature. Code relying on the `LogoutResult` tuple's length for unpacking might be affected at type-checking time if it expects 3 elements.
- gotcha In versions prior to v1.0.1, the type hint for `Config.context` was incorrect. It was fixed in v1.0.1 to correctly indicate a string type, aligning with `pysaml2`'s implementation.
Install
-
pip install types-pysaml2
Imports
- Config
from saml2.config import Config
- Saml2Client
from saml2.client import Saml2Client
- Saml2Response
from saml2.response import Saml2Response
Quickstart
import os
from saml2.config import Config
from saml2.client import Saml2Client
def setup_saml_client(config_file_path: str) -> Saml2Client:
config = Config()
config.load_file(config_file_path)
client = Saml2Client(config)
return client
# Example usage (requires an actual saml2 config file)
# config_path = os.environ.get('SAML2_CONFIG_PATH', 'path/to/saml2/config.xml')
# if os.path.exists(config_path):
# saml_client = setup_saml_client(config_path)
# print(f"SAML2 client created with entity ID: {saml_client.config.entityid}")
# else:
# print(f"Warning: SAML2 config file not found at {config_path}. Quickstart not fully runnable without it.")
# To verify type checking, run mypy on this file after installing types-pysaml2 and pysaml2:
# pip install mypy pysaml2 types-pysaml2
# mypy your_script_name.py