Skip to content

Commit 7d76243

Browse files
renanivolrafeei
andauthored
Use importlib.metadata first to avoid deprecation warnings (#878)
* Use importlib.metadata first to avoid deprecation warnings * Use get distribution name of module before fetching its version * Add support for Python versions < 3.9 * Fix conditional for packages_distributions * Linter fixes * Remove fixture in favor of test skips --------- Co-authored-by: Lalleh Rafeei <[email protected]>
1 parent dc87bd3 commit 7d76243

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

newrelic/common/package_version_utils.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,23 @@ def int_or_str(value):
7070
def _get_package_version(name):
7171
module = sys.modules.get(name, None)
7272
version = None
73+
74+
# importlib was introduced into the standard library starting in Python3.8.
75+
if "importlib" in sys.modules and hasattr(sys.modules["importlib"], "metadata"):
76+
try:
77+
# In Python3.10+ packages_distribution can be checked for as well
78+
if hasattr(sys.modules["importlib"].metadata, "packages_distributions"): # pylint: disable=E1101
79+
distributions = sys.modules["importlib"].metadata.packages_distributions() # pylint: disable=E1101
80+
distribution_name = distributions.get(name, name)
81+
else:
82+
distribution_name = name
83+
84+
version = sys.modules["importlib"].metadata.version(distribution_name) # pylint: disable=E1101
85+
if version not in NULL_VERSIONS:
86+
return version
87+
except Exception:
88+
pass
89+
7390
for attr in VERSION_ATTRS:
7491
try:
7592
version = getattr(module, attr, None)
@@ -84,15 +101,6 @@ def _get_package_version(name):
84101
except Exception:
85102
pass
86103

87-
# importlib was introduced into the standard library starting in Python3.8.
88-
if "importlib" in sys.modules and hasattr(sys.modules["importlib"], "metadata"):
89-
try:
90-
version = sys.modules["importlib"].metadata.version(name) # pylint: disable=E1101
91-
if version not in NULL_VERSIONS:
92-
return version
93-
except Exception:
94-
pass
95-
96104
if "pkg_resources" in sys.modules:
97105
try:
98106
version = sys.modules["pkg_resources"].get_distribution(name).version

tests/agent_unittests/test_package_version_utils.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,19 @@
2424
get_package_version_tuple,
2525
)
2626

27+
# Notes:
28+
# importlib.metadata was a provisional addition to the std library in PY38 and PY39
29+
# while pkg_resources was deprecated.
30+
# importlib.metadata is no longer provisional in PY310+. It added some attributes
31+
# such as distribution_packages and removed pkg_resources.
32+
2733
IS_PY38_PLUS = sys.version_info[:2] >= (3, 8)
34+
IS_PY310_PLUS = sys.version_info[:2] >= (3,10)
2835
SKIP_IF_NOT_IMPORTLIB_METADATA = pytest.mark.skipif(not IS_PY38_PLUS, reason="importlib.metadata is not supported.")
2936
SKIP_IF_IMPORTLIB_METADATA = pytest.mark.skipif(
3037
IS_PY38_PLUS, reason="importlib.metadata is preferred over pkg_resources."
3138
)
39+
SKIP_IF_NOT_PY310_PLUS = pytest.mark.skipif(not IS_PY310_PLUS, reason="These features were added in 3.10+")
3240

3341

3442
@pytest.fixture(scope="function", autouse=True)
@@ -38,8 +46,10 @@ def patched_pytest_module(monkeypatch):
3846
monkeypatch.delattr(pytest, attr)
3947

4048
yield pytest
49+
4150

42-
51+
# This test only works on Python 3.7
52+
@SKIP_IF_IMPORTLIB_METADATA
4353
@pytest.mark.parametrize(
4454
"attr,value,expected_value",
4555
(
@@ -58,6 +68,8 @@ def test_get_package_version(attr, value, expected_value):
5868
delattr(pytest, attr)
5969

6070

71+
# This test only works on Python 3.7
72+
@SKIP_IF_IMPORTLIB_METADATA
6173
def test_skips_version_callables():
6274
# There is no file/module here, so we monkeypatch
6375
# pytest instead for our purposes
@@ -72,6 +84,8 @@ def test_skips_version_callables():
7284
delattr(pytest, "version_tuple")
7385

7486

87+
# This test only works on Python 3.7
88+
@SKIP_IF_IMPORTLIB_METADATA
7589
@pytest.mark.parametrize(
7690
"attr,value,expected_value",
7791
(
@@ -97,6 +111,13 @@ def test_importlib_metadata():
97111
assert version not in NULL_VERSIONS, version
98112

99113

114+
@SKIP_IF_NOT_PY310_PLUS
115+
@validate_function_called("importlib.metadata", "packages_distributions")
116+
def test_mapping_import_to_distribution_packages():
117+
version = get_package_version("pytest")
118+
assert version not in NULL_VERSIONS, version
119+
120+
100121
@SKIP_IF_IMPORTLIB_METADATA
101122
@validate_function_called("pkg_resources", "get_distribution")
102123
def test_pkg_resources_metadata():

0 commit comments

Comments
 (0)