Type Stubs for PyJWT
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
-
error: Duplicate module named 'jwt' (or similar type conflict errors)
cause Using `types-pyjwt` simultaneously with `PyJWT` version 2.0.0 or newer, which ships with its own type annotations. Type checkers detect two sources of type information for the same module.fixUninstall `types-pyjwt` if your `PyJWT` version is 2.0.0 or higher: `pip uninstall types-pyjwt`. -
error: No type hints found for package 'jwt' (or 'module 'jwt' has no attribute 'encode' (or 'decode')' with type checker)
cause Attempting to type-check `PyJWT` code without any type information. This occurs if `PyJWT < 2.0.0` is installed and `types-pyjwt` is missing, or if `types-pyjwt` is installed but the type checker isn't configured correctly to find stubs.fixFor `PyJWT < 2.0.0`, install `types-pyjwt`: `pip install types-pyjwt`. For `PyJWT >= 2.0.0`, ensure `types-pyjwt` is *not* installed and that your type checker recognizes `py.typed` packages (most modern type checkers do by default).
Warnings
- breaking The `PyJWT` library itself includes type annotations starting from version 2.0.0. If you are using `PyJWT` version 2.0.0 or newer, you MUST uninstall `types-pyjwt` to avoid type conflicts, incorrect type checking results, or outdated stub definitions.
- deprecated `types-pyjwt` has not been updated since mid-2021 (version 1.7.1) and is effectively deprecated due to `PyJWT`'s self-typing. Further type improvements or fixes for `PyJWT` are now made directly within the `PyJWT` project.
- gotcha When `types-pyjwt` is installed, type checkers may prefer its stubs over inline annotations in `PyJWT < 2.0.0`. However, for `PyJWT >= 2.0.0`, the presence of `types-pyjwt` can cause confusing errors or `mypy` to complain about duplicate definitions.
Install
-
pip install types-pyjwt
Imports
- jwt
import jwt from jwt import PyJWT, decode, encode
Quickstart
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.