Type Stubs for PyJWT

1.7.1 · deprecated · verified Thu Apr 16

types-pyjwt is a PEP 561 compliant type stub package providing static type annotations for the PyJWT library. It enables type-checking tools like MyPy, Pyright, and PyCharm to perform static analysis on code using PyJWT. The package itself is auto-generated from the community-maintained typeshed repository. As of PyPI version 1.7.1, it has not seen updates since mid-2021, as the upstream PyJWT library started including its own type annotations from version 2.0.0 onwards.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a basic usage of PyJWT with type hints. When types-pyjwt is installed alongside PyJWT (specifically PyJWT versions prior to 2.0.0), a static type checker would use the stubs from `types-pyjwt` to validate the types in this code. For PyJWT versions 2.0.0 and newer, PyJWT includes its own `py.typed` file and inline annotations, making `types-pyjwt` unnecessary and potentially problematic.

import jwt
from typing import Dict, Any

def create_and_decode_token(payload: Dict[str, Any], secret: str) -> Dict[str, Any]:
    # This example assumes PyJWT < 2.0.0 would use types-pyjwt for type hints.
    # For PyJWT >= 2.0.0, the types are bundled.
    encoded_jwt = jwt.encode(payload, secret, algorithm="HS256")
    decoded_payload = jwt.decode(encoded_jwt, secret, algorithms=["HS256"])
    return decoded_payload

if __name__ == "__main__":
    my_payload = {"user_id": 123, "name": "test_user"}
    jwt_secret = "super-secret-key"
    
    # Run a type checker like MyPy on this file to see types-pyjwt in action (if PyJWT < 2.0.0)
    # Or, if PyJWT >= 2.0.0, to see PyJWT's bundled types.
    result = create_and_decode_token(my_payload, jwt_secret)
    print(f"Decoded payload: {result}")
    # Expected output for type checking (e.g., with mypy): No issues if types are correctly applied.

view raw JSON →