From 64ec53b89e2303d3a11b7806229778f180d113f4 Mon Sep 17 00:00:00 2001 From: TimPansino Date: Wed, 18 Oct 2023 21:34:06 +0000 Subject: [PATCH] [Mega-Linter] Apply linters fixes --- newrelic/common/package_version_utils.py | 13 +++++++------ tests/agent_unittests/test_package_version_utils.py | 4 ++-- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/newrelic/common/package_version_utils.py b/newrelic/common/package_version_utils.py index d2470e8bcd..68320b897f 100644 --- a/newrelic/common/package_version_utils.py +++ b/newrelic/common/package_version_utils.py @@ -22,11 +22,12 @@ _package_version_cache = {} _package_version_cache_lock = Lock() + def _cache_package_versions(wrapped): """ Threadsafe implementation of caching for _get_package_version. - - Python 2.7 does not have the @functools.cache decorator, and + + Python 2.7 does not have the @functools.cache decorator, and must be reimplemented with support for clearing the cache. """ @@ -34,14 +35,14 @@ def _cache_package_versions(wrapped): def _wrapper(name): if name in _package_version_cache: return _package_version_cache[name] - + with _package_version_cache_lock: if name in _package_version_cache: return _package_version_cache[name] version = _package_version_cache[name] = wrapped(name) return version - + def cache_clear(): """Cache clear function to mimic @functools.cache""" with _package_version_cache_lock: @@ -113,7 +114,7 @@ def _get_package_version(name): if "importlib" in sys.modules and hasattr(sys.modules["importlib"], "metadata"): try: # In Python3.10+ packages_distribution can be checked for as well - if hasattr(sys.modules["importlib"].metadata, "packages_distributions"): # pylint: disable=E1101 + if hasattr(sys.modules["importlib"].metadata, "packages_distributions"): # pylint: disable=E1101 distributions = sys.modules["importlib"].metadata.packages_distributions() # pylint: disable=E1101 distribution_name = distributions.get(name, name) else: @@ -145,4 +146,4 @@ def _get_package_version(name): if version not in NULL_VERSIONS: return version except Exception: - pass \ No newline at end of file + pass diff --git a/tests/agent_unittests/test_package_version_utils.py b/tests/agent_unittests/test_package_version_utils.py index bdcb588542..5ed689ea2a 100644 --- a/tests/agent_unittests/test_package_version_utils.py +++ b/tests/agent_unittests/test_package_version_utils.py @@ -20,9 +20,9 @@ from newrelic.common.package_version_utils import ( NULL_VERSIONS, VERSION_ATTRS, + _get_package_version, get_package_version, get_package_version_tuple, - _get_package_version, ) # Notes: @@ -32,7 +32,7 @@ # such as distribution_packages and removed pkg_resources. IS_PY38_PLUS = sys.version_info[:2] >= (3, 8) -IS_PY310_PLUS = sys.version_info[:2] >= (3,10) +IS_PY310_PLUS = sys.version_info[:2] >= (3, 10) SKIP_IF_NOT_IMPORTLIB_METADATA = pytest.mark.skipif(not IS_PY38_PLUS, reason="importlib.metadata is not supported.") SKIP_IF_IMPORTLIB_METADATA = pytest.mark.skipif( IS_PY38_PLUS, reason="importlib.metadata is preferred over pkg_resources."