Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make PyJWT an optional dependency #3518

Merged
merged 1 commit into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/actions/run-tests/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ runs:
echo "::group::Installing dependencies"
pip install -r dev_requirements.txt
pip uninstall -y redis # uninstall Redis package installed via redis-entraid
pip install -e . # install the working copy
pip install -e .[jwt] # install the working copy
if [ "${{inputs.parser-backend}}" == "hiredis" ]; then
pip install "hiredis${{inputs.hiredis-version}}"
echo "PARSER_BACKEND=$(echo "${{inputs.parser-backend}}_${{inputs.hiredis-version}}" | sed 's/[^a-zA-Z0-9]/_/g')" >> $GITHUB_ENV
Expand Down
14 changes: 7 additions & 7 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ Here's how to get started with your code contribution:

1. Create your own fork of redis-py
2. Do the changes in your fork
3.
*Create a virtualenv and install the development dependencies from the dev_requirements.txt file:*

a. python -m venv .venv
b. source .venv/bin/activate
c. pip install -r dev_requirements.txt
c. pip install -e .
3. Create a virtualenv and install the development dependencies from the dev_requirements.txt file:
```
python -m venv .venv
source .venv/bin/activate
pip install -r dev_requirements.txt
pip install -e .[jwt]
```

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/).
5. While developing, make sure the tests pass by running `invoke tests`
Expand Down
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ classifiers = [
]
dependencies = [
'async-timeout>=4.0.3; python_full_version<"3.11.3"',
"PyJWT~=2.9.0",
]

[project.optional-dependencies]
Expand All @@ -49,6 +48,9 @@ ocsp = [
"pyopenssl==20.0.1",
"requests>=2.31.0",
]
jwt = [
"PyJWT~=2.9.0",
]

[project.urls]
Changes = "https://github.com/redis/redis-py/releases"
Expand Down
7 changes: 6 additions & 1 deletion redis/auth/token.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from abc import ABC, abstractmethod
from datetime import datetime, timezone

import jwt
from redis.auth.err import InvalidTokenSchemaErr


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

def __init__(self, token: str):
try:
import jwt
except ImportError as ie:
raise ImportError(
f"The PyJWT library is required for {self.__class__.__name__}.",
) from ie
self._value = token
self._decoded = jwt.decode(
self._value,
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from unittest.mock import Mock
from urllib.parse import urlparse

import jwt
import pytest
import redis
from packaging.version import Version
Expand Down Expand Up @@ -615,6 +614,7 @@ def cache_key(request) -> CacheKey:


def mock_identity_provider() -> IdentityProviderInterface:
jwt = pytest.importorskip("jwt")
mock_provider = Mock(spec=IdentityProviderInterface)
token = {"exp": datetime.now(timezone.utc).timestamp() + 3600, "oid": "username"}
encoded = jwt.encode(token, "secret", algorithm="HS256")
Expand Down
2 changes: 1 addition & 1 deletion tests/test_asyncio/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from enum import Enum
from typing import Union

import jwt
import pytest
import pytest_asyncio
import redis.asyncio as redis
Expand Down Expand Up @@ -247,6 +246,7 @@ async def mock_cluster_resp_slaves(create_redis, **kwargs):


def mock_identity_provider() -> IdentityProviderInterface:
jwt = pytest.importorskip("jwt")
mock_provider = Mock(spec=IdentityProviderInterface)
token = {"exp": datetime.now(timezone.utc).timestamp() + 3600, "oid": "username"}
encoded = jwt.encode(token, "secret", algorithm="HS256")
Expand Down
3 changes: 2 additions & 1 deletion tests/test_auth/test_token.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from datetime import datetime, timezone

import jwt
import pytest
from redis.auth.err import InvalidTokenSchemaErr
from redis.auth.token import JWToken, SimpleToken
Expand Down Expand Up @@ -39,6 +38,8 @@ def test_simple_token(self):
assert token.get_expires_at_ms() == -1

def test_jwt_token(self):
jwt = pytest.importorskip("jwt")

token = {
"exp": datetime.now(timezone.utc).timestamp() + 100,
"iat": datetime.now(timezone.utc).timestamp(),
Expand Down
Loading