diff --git a/sdks/python/apache_beam/io/requestresponse.py b/sdks/python/apache_beam/io/requestresponse.py index 213a1a3fc7ea..e53fa07471af 100644 --- a/sdks/python/apache_beam/io/requestresponse.py +++ b/sdks/python/apache_beam/io/requestresponse.py @@ -35,7 +35,6 @@ from typing import TypeVar from typing import Union -import redis from google.api_core.exceptions import TooManyRequests import apache_beam as beam @@ -46,6 +45,11 @@ from apache_beam.transforms.util import BatchElements from apache_beam.utils import retry +try: + import redis +except ImportError: + redis = None + RequestT = TypeVar('RequestT') ResponseT = TypeVar('ResponseT') @@ -689,6 +693,11 @@ def __init__( self._kwargs = kwargs if kwargs else {} self._source_caller = None + if redis is None: + raise ImportError( + 'Failed to import redis. You can ensure it is ' + 'installed by installing the redis beam extra') + def get_read(self): """get_read returns a PTransform for reading from the cache.""" ensure_coders_exist(self._request_coder) diff --git a/sdks/python/setup.py b/sdks/python/setup.py index 074d64ae8921..289433f9ea5b 100644 --- a/sdks/python/setup.py +++ b/sdks/python/setup.py @@ -402,7 +402,6 @@ def get_portability_package_data(): 'protobuf>=3.20.3,<7.0.0.dev0,!=4.0.*,!=4.21.*,!=4.22.0,!=4.23.*,!=4.24.*', # pylint: disable=line-too-long 'python-dateutil>=2.8.0,<3', 'pytz>=2018.3', - 'redis>=5.0.0,<6', 'requests>=2.32.4,<3.0.0', 'sortedcontainers>=2.4.0', 'typing-extensions>=3.7.0', @@ -588,6 +587,7 @@ def get_portability_package_data(): 'torch==2.8.0+cpu', 'transformers>=4.28.0,<4.56.0' ], + 'redis': ['redis>=5.0.0,<6'], 'tft': [ 'tensorflow_transform>=1.14.0,<1.15.0' # tensorflow-transform requires dill, but doesn't set dill as a diff --git a/sdks/python/tox.ini b/sdks/python/tox.ini index 7d84ca7a2c62..da0932728b20 100644 --- a/sdks/python/tox.ini +++ b/sdks/python/tox.ini @@ -33,7 +33,7 @@ pip_pre = True # allow apps that support color to use it. passenv=TERM,CLOUDSDK_CONFIG,DOCKER_*,TESTCONTAINERS_*,TC_*,ALLOYDB_PASSWORD # Set [] options for pip installation of apache-beam tarball. -extras = test,dataframe,tfrecord,yaml +extras = test,dataframe,redis,tfrecord,yaml # Don't warn that these commands aren't installed. allowlist_externals = false @@ -97,7 +97,7 @@ install_command = {envbindir}/python.exe {envbindir}/pip.exe install --retries 1 list_dependencies_command = {envbindir}/python.exe {envbindir}/pip.exe freeze [testenv:py{310,311,312,313}-cloud] -; extras = test,gcp,interactive,dataframe,aws,azure +; extras = test,gcp,interactive,dataframe,aws,azure,redis extras = test,gcp,interactive,dataframe,aws,azure commands = python apache_beam/examples/complete/autocomplete_test.py @@ -173,7 +173,7 @@ setenv = TC_SLEEP_TIME = {env:TC_SLEEP_TIME:1} # NOTE: we could add ml_test to increase the collected code coverage metrics, but it would make the suite slower. -extras = test,gcp,interactive,dataframe,aws +extras = test,gcp,interactive,dataframe,aws,redis commands = bash {toxinidir}/scripts/run_pytest.sh {envname} "{posargs}" "--cov-report=xml --cov=. --cov-append"