From 4bfbf1761deb6d6a556b83693a4371def0e88239 Mon Sep 17 00:00:00 2001 From: euisuh Date: Fri, 24 Jul 2026 14:00:09 +0900 Subject: [PATCH] fix: avoid writing None AWS region to environment --- .../manually_maintained/cohere_aws/client.py | 11 ++++++----- tests/test_aws_client_unit.py | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/cohere/manually_maintained/cohere_aws/client.py b/src/cohere/manually_maintained/cohere_aws/client.py index 84d0da0ae..898d92f05 100644 --- a/src/cohere/manually_maintained/cohere_aws/client.py +++ b/src/cohere/manually_maintained/cohere_aws/client.py @@ -3,18 +3,19 @@ import tarfile import tempfile import time +import typing from typing import Any, Dict, List, Optional, Union +from ..lazy_aws_deps import lazy_boto3, lazy_botocore, lazy_sagemaker +from .chat import Chat, StreamingChat from .classification import Classification, Classifications from .embeddings import Embeddings from .error import CohereError from .generation import Generations, StreamingGenerations -from .chat import Chat, StreamingChat +from .mode import Mode from .rerank import Reranking from .summary import Summary -from .mode import Mode -import typing -from ..lazy_aws_deps import lazy_boto3, lazy_botocore, lazy_sagemaker + class Client: def __init__( @@ -27,7 +28,7 @@ def __init__( `aws configure set region us-west-2` or override it with `region_name` parameter. """ self.mode = mode - if os.environ.get('AWS_DEFAULT_REGION') is None: + if os.environ.get('AWS_DEFAULT_REGION') is None and aws_region is not None: os.environ['AWS_DEFAULT_REGION'] = aws_region if self.mode == Mode.SAGEMAKER: diff --git a/tests/test_aws_client_unit.py b/tests/test_aws_client_unit.py index 94e584922..0ff760a80 100644 --- a/tests/test_aws_client_unit.py +++ b/tests/test_aws_client_unit.py @@ -128,6 +128,22 @@ def test_default_mode_is_sagemaker(self) -> None: sig = inspect.signature(Client.__init__) self.assertEqual(sig.parameters["mode"].default, Mode.SAGEMAKER) + def test_missing_region_does_not_write_none_to_environment(self) -> None: + """No aws_region + unset AWS_DEFAULT_REGION should not crash before boto3 resolves defaults.""" + mock_boto3 = MagicMock() + mock_sagemaker = MagicMock() + + with patch("cohere.manually_maintained.cohere_aws.client.lazy_boto3", return_value=mock_boto3), \ + patch("cohere.manually_maintained.cohere_aws.client.lazy_sagemaker", return_value=mock_sagemaker), \ + patch.dict(os.environ, {}, clear=True): + + from cohere.manually_maintained.cohere_aws.client import Client + + Client() + + self.assertNotIn("AWS_DEFAULT_REGION", os.environ) + self.assertEqual(mock_boto3.client.call_args_list[0].kwargs["region_name"], None) + class TestEmbedV4Params(unittest.TestCase): """Fix 3: embed() should accept output_dimension and embedding_types,