Skip to content

Commit 55caa7d

Browse files
committed
Make PyJWT an optional dependency
1 parent 483c021 commit 55caa7d

File tree

6 files changed

+14
-5
lines changed

6 files changed

+14
-5
lines changed

.github/actions/run-tests/action.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ runs:
3838
echo "::group::Installing dependencies"
3939
pip install -r dev_requirements.txt
4040
pip uninstall -y redis # uninstall Redis package installed via redis-entraid
41-
pip install -e . # install the working copy
41+
pip install -e .[jwt] # install the working copy
4242
if [ "${{inputs.parser-backend}}" == "hiredis" ]; then
4343
pip install "hiredis${{inputs.hiredis-version}}"
4444
echo "PARSER_BACKEND=$(echo "${{inputs.parser-backend}}_${{inputs.hiredis-version}}" | sed 's/[^a-zA-Z0-9]/_/g')" >> $GITHUB_ENV

pyproject.toml

+3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ ocsp = [
4949
"pyopenssl==20.0.1",
5050
"requests>=2.31.0",
5151
]
52+
jwt = [
53+
"PyJWT~=2.9.0",
54+
]
5255

5356
[project.urls]
5457
Changes = "https://github.com/redis/redis-py/releases"

redis/auth/token.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from abc import ABC, abstractmethod
22
from datetime import datetime, timezone
33

4-
import jwt
54
from redis.auth.err import InvalidTokenSchemaErr
65

76

@@ -81,6 +80,12 @@ class JWToken(TokenInterface):
8180
REQUIRED_FIELDS = {"exp"}
8281

8382
def __init__(self, token: str):
83+
try:
84+
import jwt
85+
except ImportError as ie:
86+
raise ImportError(
87+
f"The PyJWT library is required for {self.__class__.__name__}.",
88+
) from ie
8489
self._value = token
8590
self._decoded = jwt.decode(
8691
self._value,

tests/conftest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from unittest.mock import Mock
1111
from urllib.parse import urlparse
1212

13-
import jwt
1413
import pytest
1514
import redis
1615
from packaging.version import Version
@@ -615,6 +614,7 @@ def cache_key(request) -> CacheKey:
615614

616615

617616
def mock_identity_provider() -> IdentityProviderInterface:
617+
jwt = pytest.importorskip("jwt")
618618
mock_provider = Mock(spec=IdentityProviderInterface)
619619
token = {"exp": datetime.now(timezone.utc).timestamp() + 3600, "oid": "username"}
620620
encoded = jwt.encode(token, "secret", algorithm="HS256")

tests/test_asyncio/conftest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from enum import Enum
66
from typing import Union
77

8-
import jwt
98
import pytest
109
import pytest_asyncio
1110
import redis.asyncio as redis
@@ -247,6 +246,7 @@ async def mock_cluster_resp_slaves(create_redis, **kwargs):
247246

248247

249248
def mock_identity_provider() -> IdentityProviderInterface:
249+
jwt = pytest.importorskip("jwt")
250250
mock_provider = Mock(spec=IdentityProviderInterface)
251251
token = {"exp": datetime.now(timezone.utc).timestamp() + 3600, "oid": "username"}
252252
encoded = jwt.encode(token, "secret", algorithm="HS256")

tests/test_auth/test_token.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from datetime import datetime, timezone
22

3-
import jwt
43
import pytest
54
from redis.auth.err import InvalidTokenSchemaErr
65
from redis.auth.token import JWToken, SimpleToken
@@ -39,6 +38,8 @@ def test_simple_token(self):
3938
assert token.get_expires_at_ms() == -1
4039

4140
def test_jwt_token(self):
41+
jwt = pytest.importorskip("jwt")
42+
4243
token = {
4344
"exp": datetime.now(timezone.utc).timestamp() + 100,
4445
"iat": datetime.now(timezone.utc).timestamp(),

0 commit comments

Comments
 (0)