Skip to content

Commit 8f47b17

Browse files
authored
Add check for PackageNotFoundError when initializing _CLIENT_INFO (#1044)
1 parent c8ec052 commit 8f47b17

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

google/ads/googleads/client.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,12 @@
4545

4646
# Retrieve the version of this client library to be sent in the user-agent
4747
# information of API calls.
48-
_CLIENT_INFO = ClientInfo(client_library_version=metadata.version("google-ads"))
48+
try:
49+
_CLIENT_INFO = ClientInfo(
50+
client_library_version=metadata.version("google-ads")
51+
)
52+
except metadata.PackageNotFoundError:
53+
_CLIENT_INFO = ClientInfo()
4954

5055
# See options at grpc.github.io/grpc/core/group__grpc__arg__keys.html
5156
_GRPC_CHANNEL_OPTIONS = [

tests/client_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,16 @@ def test_load_http_proxy_from_string(self):
951951
use_cloud_org_for_api_access=None,
952952
)
953953

954+
def test_client_info_package_not_found(self):
955+
with mock.patch("google.ads.googleads.client.metadata.version") as mock_version:
956+
mock_version.side_effect = Client.metadata.PackageNotFoundError
957+
from importlib import reload
958+
reload(Client)
959+
self.assertIsInstance(Client._CLIENT_INFO, Client.ClientInfo)
960+
self.assertIsNone(Client._CLIENT_INFO.client_library_version)
961+
962+
reload(Client)
963+
954964

955965
class GoogleAdsClientTestFs(FileTestCase):
956966
"""Tests for the GoogleAdsClient class that require a fake filesystem."""

0 commit comments

Comments
 (0)