Skip to content

Commit fe3a156

Browse files
xiazhverabretambroseBret AmbrosesbSteveKhikeya
authored
Fix pkg resources deprecated (#638)
* Why was this using mqtt5 publish completion contract? (#632) Co-authored-by: Bret Ambrose <[email protected]> Co-authored-by: Steve Kim <[email protected]> * Update pkg_resources in mqtt_connection_builder.py * Update pkg_resources in mqtt5_client_builder.py * Add importlib metadata tests * remove import test --------- Co-authored-by: Bret Ambrose <[email protected]> Co-authored-by: Bret Ambrose <[email protected]> Co-authored-by: Steve Kim <[email protected]> Co-authored-by: hikeya <[email protected]> Co-authored-by: hikeya <[email protected]>
1 parent 2f04915 commit fe3a156

File tree

3 files changed

+134
-6
lines changed

3 files changed

+134
-6
lines changed

awsiot/mqtt5_client_builder.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,11 @@ def _get_metrics_str(current_username=""):
220220

221221
if _metrics_str is None:
222222
try:
223-
import pkg_resources
223+
import importlib.metadata
224224
try:
225-
version = pkg_resources.get_distribution("awsiotsdk").version
225+
version = importlib.metadata.version("awsiotsdk")
226226
_metrics_str = "SDK=PythonV2&Version={}".format(version)
227-
except pkg_resources.DistributionNotFound:
227+
except importlib.metadata.PackageNotFoundError:
228228
_metrics_str = "SDK=PythonV2&Version=dev"
229229
except BaseException:
230230
_metrics_str = ""

awsiot/mqtt_connection_builder.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,11 @@ def _get_metrics_str(current_username=""):
158158

159159
if _metrics_str is None:
160160
try:
161-
import pkg_resources
161+
import importlib.metadata
162162
try:
163-
version = pkg_resources.get_distribution("awsiotsdk").version
163+
version = importlib.metadata.version("awsiotsdk")
164164
_metrics_str = "SDK=PythonV2&Version={}".format(version)
165-
except pkg_resources.DistributionNotFound:
165+
except importlib.metadata.PackageNotFoundError:
166166
_metrics_str = "SDK=PythonV2&Version=dev"
167167
except BaseException:
168168
_metrics_str = ""

test/test_get_metrics.py

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0.
3+
4+
import unittest
5+
from unittest.mock import patch
6+
7+
8+
class TestImportlibMetadata(unittest.TestCase):
9+
"""Test that importlib.metadata is used instead of pkg_resources"""
10+
11+
def setUp(self):
12+
"""Reset the metrics string cache before each test"""
13+
# Reset the cached metrics string in both modules
14+
import awsiot.mqtt5_client_builder
15+
import awsiot.mqtt_connection_builder
16+
17+
# Reset the global _metrics_str variable
18+
awsiot.mqtt_connection_builder._metrics_str = None
19+
awsiot.mqtt5_client_builder._metrics_str = None
20+
21+
def test_metrics_string_generation_mqtt_connection_builder(self):
22+
"""Test that mqtt_connection_builder uses importlib.metadata for version detection"""
23+
from awsiot import mqtt_connection_builder
24+
25+
# Mock importlib.metadata.version to return a known version
26+
with patch("importlib.metadata.version") as mock_version:
27+
mock_version.return_value = "1.2.3"
28+
29+
# Call the function that uses version detection
30+
# We need to access the private function for testing
31+
result = mqtt_connection_builder._get_metrics_str("test_username")
32+
33+
# Verify that importlib.metadata.version was called
34+
mock_version.assert_called_once_with("awsiotsdk")
35+
36+
# Verify the result contains the expected format
37+
self.assertIn("SDK=PythonV2&Version=1.2.3", result)
38+
39+
def test_metrics_string_generation_mqtt5_client_builder(self):
40+
"""Test that mqtt5_client_builder uses importlib.metadata for version detection"""
41+
from awsiot import mqtt5_client_builder
42+
43+
# Mock importlib.metadata.version to return a known version
44+
with patch("importlib.metadata.version") as mock_version:
45+
mock_version.return_value = "1.2.3"
46+
47+
# Call the function that uses version detection
48+
# We need to access the private function for testing
49+
result = mqtt5_client_builder._get_metrics_str("test_username")
50+
51+
# Verify that importlib.metadata.version was called
52+
mock_version.assert_called_once_with("awsiotsdk")
53+
54+
# Verify the result contains the expected format
55+
self.assertIn("SDK=PythonV2&Version=1.2.3", result)
56+
57+
def test_package_not_found_handling_mqtt_connection_builder(self):
58+
"""Test that PackageNotFoundError is handled correctly in mqtt_connection_builder"""
59+
import importlib.metadata
60+
61+
from awsiot import mqtt_connection_builder
62+
63+
# Mock importlib.metadata.version to raise PackageNotFoundError
64+
with patch("importlib.metadata.version") as mock_version:
65+
mock_version.side_effect = importlib.metadata.PackageNotFoundError("Package not found")
66+
67+
# Call the function that uses version detection
68+
result = mqtt_connection_builder._get_metrics_str("test_username")
69+
70+
# Verify that the fallback version is used
71+
self.assertIn("SDK=PythonV2&Version=dev", result)
72+
73+
def test_package_not_found_handling_mqtt5_client_builder(self):
74+
"""Test that PackageNotFoundError is handled correctly in mqtt5_client_builder"""
75+
import importlib.metadata
76+
77+
from awsiot import mqtt5_client_builder
78+
79+
# Mock importlib.metadata.version to raise PackageNotFoundError
80+
with patch("importlib.metadata.version") as mock_version:
81+
mock_version.side_effect = importlib.metadata.PackageNotFoundError("Package not found")
82+
83+
# Call the function that uses version detection
84+
result = mqtt5_client_builder._get_metrics_str("test_username")
85+
86+
# Verify that the fallback version is used
87+
self.assertIn("SDK=PythonV2&Version=dev", result)
88+
89+
def test_general_exception_handling_mqtt_connection_builder(self):
90+
"""Test that general exceptions are handled correctly in mqtt_connection_builder"""
91+
from awsiot import mqtt_connection_builder
92+
93+
# Mock importlib.metadata.version to raise a general exception
94+
with patch("importlib.metadata.version") as mock_version:
95+
mock_version.side_effect = Exception("Some other error")
96+
97+
# Call the function that uses version detection
98+
result = mqtt_connection_builder._get_metrics_str("test_username")
99+
100+
# Verify that empty string is returned on general exception
101+
self.assertEqual(result, "")
102+
103+
def test_general_exception_handling_mqtt5_client_builder(self):
104+
"""Test that general exceptions are handled correctly in mqtt5_client_builder"""
105+
from awsiot import mqtt5_client_builder
106+
107+
# Mock importlib.metadata.version to raise a general exception
108+
with patch("importlib.metadata.version") as mock_version:
109+
mock_version.side_effect = Exception("Some other error")
110+
111+
# Call the function that uses version detection
112+
result = mqtt5_client_builder._get_metrics_str("test_username")
113+
114+
# Verify that empty string is returned on general exception
115+
self.assertEqual(result, "")
116+
117+
def test_no_pkg_resources_import(self):
118+
"""Test that pkg_resources is not imported in the modified files"""
119+
import awsiot.mqtt5_client_builder
120+
import awsiot.mqtt_connection_builder
121+
122+
# Check that pkg_resources is not in the module's globals
123+
self.assertNotIn("pkg_resources", awsiot.mqtt_connection_builder.__dict__)
124+
self.assertNotIn("pkg_resources", awsiot.mqtt5_client_builder.__dict__)
125+
126+
127+
if __name__ == "__main__":
128+
unittest.main()

0 commit comments

Comments
 (0)