Skip to content

Commit dbbe7a7

Browse files
authored
Make PyJWT an optional dependency (#3518)
1 parent 6f752b0 commit dbbe7a7

File tree

7 files changed

+21
-13
lines changed

7 files changed

+21
-13
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

CONTRIBUTING.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ Here's how to get started with your code contribution:
3232

3333
1. Create your own fork of redis-py
3434
2. Do the changes in your fork
35-
3.
36-
*Create a virtualenv and install the development dependencies from the dev_requirements.txt file:*
37-
38-
a. python -m venv .venv
39-
b. source .venv/bin/activate
40-
c. pip install -r dev_requirements.txt
41-
c. pip install -e .
35+
3. Create a virtualenv and install the development dependencies from the dev_requirements.txt file:
36+
```
37+
python -m venv .venv
38+
source .venv/bin/activate
39+
pip install -r dev_requirements.txt
40+
pip install -e .[jwt]
41+
```
4242
4343
4. If you need a development environment, run `invoke devenv`. Note: this relies on docker-compose to build environments, and assumes that you have a version supporting [docker profiles](https://docs.docker.com/compose/profiles/).
4444
5. While developing, make sure the tests pass by running `invoke tests`

pyproject.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ classifiers = [
3737
]
3838
dependencies = [
3939
'async-timeout>=4.0.3; python_full_version<"3.11.3"',
40-
"PyJWT~=2.9.0",
4140
]
4241

4342
[project.optional-dependencies]
@@ -49,6 +48,9 @@ ocsp = [
4948
"pyopenssl==20.0.1",
5049
"requests>=2.31.0",
5150
]
51+
jwt = [
52+
"PyJWT~=2.9.0",
53+
]
5254

5355
[project.urls]
5456
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)