diff --git a/poetry.lock b/poetry.lock index 86da213c..c1549cbf 100644 --- a/poetry.lock +++ b/poetry.lock @@ -617,4 +617,4 @@ testing = ["big-O", "flake8 (<5)", "jaraco.functools", "jaraco.itertools", "more [metadata] lock-version = "2.0" python-versions = "^3.7" -content-hash = "b4532919b23bee37192a1c99efa8c592c96efeb03ddacace5752618f8830f006" +content-hash = "ee8f5b0e98442ce6d388a3c3c8a351c57ffa28c84d2c2d4a5a854c9b0ce2be07" diff --git a/pyproject.toml b/pyproject.toml index 61bf95d6..d795d6e4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,8 +34,6 @@ momento-wire-types = "^0.106.0" grpcio = "^1.46.0" # note if you bump this presigned url test need be updated pyjwt = "^2.4.0" -# Need a lower bound of 4 to be compatible with python 3.7 flake8 -importlib-metadata = { version = ">=4", python = "<3.8" } [tool.poetry.group.test.dependencies] pytest = "^7.1.3" @@ -111,11 +109,6 @@ module = [ ] disallow_any_expr = false -[[tool.mypy.overrides]] -# this file has mypy incompatibilities between py37 and later versions -module = "momento.internal._utilities._momento_version" -ignore_errors = true - [[tool.mypy.overrides]] # reading an enum in a protobuf generated class module = "momento.internal._utilities._permissions" diff --git a/src/momento/internal/_utilities/__init__.py b/src/momento/internal/_utilities/__init__.py index 7845d6f3..60f2e606 100644 --- a/src/momento/internal/_utilities/__init__.py +++ b/src/momento/internal/_utilities/__init__.py @@ -16,6 +16,5 @@ _validate_topic_name, _validate_ttl, ) -from ._momento_version import momento_version from ._python_runtime_version import PYTHON_RUNTIME_VERSION from ._time import _timedelta_to_ms diff --git a/src/momento/internal/_utilities/_momento_version.py b/src/momento/internal/_utilities/_momento_version.py deleted file mode 100644 index c8129d62..00000000 --- a/src/momento/internal/_utilities/_momento_version.py +++ /dev/null @@ -1,37 +0,0 @@ -"""Detect the version of momento installed. - -This module is used to detect the version of momento installed. It is used -internally to add the version to the gRPC headers. -""" - -momento_version = "" -DEFAULT_MOMENTO_VERSION = "1.21.2" - - -def set_momento_version(version: str) -> None: - """Set the version of momento installed. - - Args: - version: The version of momento installed. - """ - global momento_version - momento_version = version - - -try: - # For python < 3.8 - import importlib_metadata - - set_momento_version(importlib_metadata.Distribution.from_name("momento").version) # type: ignore[no-untyped-call,misc] -except ModuleNotFoundError: - # Fall back to setting to the version manually - set_momento_version(DEFAULT_MOMENTO_VERSION) -except ImportError: - # For python >= 3.8 - from importlib.metadata import PackageNotFoundError, version # type: ignore[import] - - try: - set_momento_version(version("momento")) - except PackageNotFoundError: - # Fall back to setting to the version manually - set_momento_version(DEFAULT_MOMENTO_VERSION) diff --git a/src/momento/internal/aio/_scs_grpc_manager.py b/src/momento/internal/aio/_scs_grpc_manager.py index cc432b21..066a7bf4 100644 --- a/src/momento/internal/aio/_scs_grpc_manager.py +++ b/src/momento/internal/aio/_scs_grpc_manager.py @@ -15,7 +15,7 @@ from momento.config.auth_configuration import AuthConfiguration from momento.config.transport.transport_strategy import StaticGrpcConfiguration from momento.errors.exceptions import ConnectionException -from momento.internal._utilities import PYTHON_RUNTIME_VERSION, ClientType, momento_version +from momento.internal._utilities import PYTHON_RUNTIME_VERSION, ClientType from momento.internal._utilities._channel_credentials import ( channel_credentials_from_root_certs_or_default, ) @@ -38,8 +38,6 @@ class _ControlGrpcManager: """Internal gRPC control manager.""" - version = momento_version - def __init__(self, configuration: Configuration, credential_provider: CredentialProvider): self._secure_channel = grpc.aio.secure_channel( target=credential_provider.control_endpoint, @@ -62,8 +60,6 @@ def async_stub(self) -> control_client.ScsControlStub: class _DataGrpcManager: """Internal gRPC data manager.""" - version = momento_version - def __init__(self, configuration: Configuration, credential_provider: CredentialProvider): self._logger = logs.logger self._secure_channel = grpc.aio.secure_channel( @@ -139,8 +135,6 @@ def async_stub(self) -> cache_client.ScsStub: class _PubsubGrpcManager: """Internal gRPC pubsub manager.""" - version = momento_version - def __init__(self, configuration: TopicConfiguration, credential_provider: CredentialProvider): # NOTE: This is hard-coded for now but we may want to expose it via TopicConfiguration in the future, as we do with some of the other clients. grpc_config = StaticGrpcConfiguration(deadline=timedelta(milliseconds=1100)) @@ -162,8 +156,6 @@ def async_stub(self) -> pubsub_client.PubsubStub: class _PubsubGrpcStreamManager: """Internal gRPC pubsub stream manager.""" - version = momento_version - def __init__(self, configuration: TopicConfiguration, credential_provider: CredentialProvider): # NOTE: This is hard-coded for now but we may want to expose it via TopicConfiguration in the future, as we do with some of the other clients. grpc_config = StaticGrpcConfiguration(deadline=timedelta(milliseconds=1100)) @@ -185,8 +177,6 @@ def async_stub(self) -> pubsub_client.PubsubStub: class _TokenGrpcManager: """Internal gRPC token manager.""" - version = momento_version - def __init__(self, configuration: AuthConfiguration, credential_provider: CredentialProvider): self._secure_channel = grpc.aio.secure_channel( target=credential_provider.token_endpoint, @@ -209,9 +199,11 @@ def async_stub(self) -> token_client.TokenStub: def _interceptors( auth_token: str, client_type: ClientType, retry_strategy: Optional[RetryStrategy] = None ) -> list[grpc.aio.ClientInterceptor]: + from momento import __version__ as momento_version + headers = [ Header("authorization", auth_token), - Header("agent", f"python:{client_type.value}:{_ControlGrpcManager.version}"), + Header("agent", f"python:{client_type.value}:{momento_version}"), Header("runtime-version", f"python {PYTHON_RUNTIME_VERSION}"), ] return list( @@ -226,9 +218,12 @@ def _interceptors( def _stream_interceptors(auth_token: str, client_type: ClientType) -> list[grpc.aio.UnaryStreamClientInterceptor]: + # This is a workaround to avoid circular imports. + from momento import __version__ as momento_version + headers = [ Header("authorization", auth_token), - Header("agent", f"python:{client_type.value}:{_PubsubGrpcStreamManager.version}"), + Header("agent", f"python:{client_type.value}:{momento_version}"), Header("runtime-version", f"python {PYTHON_RUNTIME_VERSION}"), ] return [AddHeaderStreamingClientInterceptor(headers)] diff --git a/src/momento/internal/synchronous/_scs_grpc_manager.py b/src/momento/internal/synchronous/_scs_grpc_manager.py index 03a233ca..624a2f58 100644 --- a/src/momento/internal/synchronous/_scs_grpc_manager.py +++ b/src/momento/internal/synchronous/_scs_grpc_manager.py @@ -16,7 +16,7 @@ from momento.config.auth_configuration import AuthConfiguration from momento.config.transport.transport_strategy import StaticGrpcConfiguration from momento.errors.exceptions import ConnectionException -from momento.internal._utilities import PYTHON_RUNTIME_VERSION, ClientType, momento_version +from momento.internal._utilities import PYTHON_RUNTIME_VERSION, ClientType from momento.internal._utilities._channel_credentials import ( channel_credentials_from_root_certs_or_default, ) @@ -37,8 +37,6 @@ class _ControlGrpcManager: """Internal gRPC control mananger.""" - version = momento_version - def __init__(self, configuration: Configuration, credential_provider: CredentialProvider): self._secure_channel = grpc.secure_channel( target=credential_provider.control_endpoint, @@ -63,8 +61,6 @@ def stub(self) -> control_client.ScsControlStub: class _DataGrpcManager: """Internal gRPC data mananger.""" - version = momento_version - def __init__(self, configuration: Configuration, credential_provider: CredentialProvider): self._logger = logs.logger self._secure_channel = grpc.secure_channel( @@ -156,8 +152,6 @@ def stub(self) -> cache_client.ScsStub: class _PubsubGrpcManager: """Internal gRPC pubsub manager.""" - version = momento_version - def __init__(self, configuration: TopicConfiguration, credential_provider: CredentialProvider): # NOTE: This is hard-coded for now but we may want to expose it via TopicConfiguration in the future, as we do with some of the other clients. grpc_config = StaticGrpcConfiguration(deadline=timedelta(milliseconds=1100)) @@ -182,8 +176,6 @@ def stub(self) -> pubsub_client.PubsubStub: class _PubsubGrpcStreamManager: """Internal gRPC pubsub stream manager.""" - version = momento_version - def __init__(self, configuration: TopicConfiguration, credential_provider: CredentialProvider): # NOTE: This is hard-coded for now but we may want to expose it via TopicConfiguration in the future, as we do with some of the other clients. grpc_config = StaticGrpcConfiguration(deadline=timedelta(milliseconds=1100)) @@ -208,8 +200,6 @@ def stub(self) -> pubsub_client.PubsubStub: class _TokenGrpcManager: """Internal gRPC token manager.""" - version = momento_version - def __init__(self, configuration: AuthConfiguration, credential_provider: CredentialProvider): self._secure_channel = grpc.secure_channel( target=credential_provider.token_endpoint, @@ -234,9 +224,11 @@ def stub(self) -> token_client.TokenStub: def _interceptors( auth_token: str, client_type: ClientType, retry_strategy: Optional[RetryStrategy] = None ) -> list[grpc.UnaryUnaryClientInterceptor]: + from momento import __version__ as momento_version + headers = [ Header("authorization", auth_token), - Header("agent", f"python:{client_type.value}:{_ControlGrpcManager.version}"), + Header("agent", f"python:{client_type.value}:{momento_version}"), Header("runtime-version", f"python {PYTHON_RUNTIME_VERSION}"), ] return list( @@ -247,9 +239,12 @@ def _interceptors( def _stream_interceptors(auth_token: str, client_type: ClientType) -> list[grpc.UnaryStreamClientInterceptor]: + # This is here to avoid circular imports + from momento import __version__ as momento_version + headers = [ Header("authorization", auth_token), - Header("agent", f"python:{client_type.value}:{_PubsubGrpcStreamManager.version}"), + Header("agent", f"python:{client_type.value}:{momento_version}"), Header("runtime-version", f"python {PYTHON_RUNTIME_VERSION}"), ] return [AddHeaderStreamingClientInterceptor(headers)] diff --git a/tests/momento/internal/_utilities/test_momento_version.py b/tests/momento/internal/_utilities/test_momento_version.py index 455d9002..7249a6cc 100644 --- a/tests/momento/internal/_utilities/test_momento_version.py +++ b/tests/momento/internal/_utilities/test_momento_version.py @@ -1,4 +1,4 @@ -from momento.internal._utilities import momento_version +from momento import __version__ as momento_version def test_momento_version() -> None: