From 2ce0580068fba59756be0d59ecac831ff0391175 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20FERRY?= Date: Fri, 13 May 2022 09:38:02 +0200 Subject: [PATCH] fix(perf): optimize the code to get package class name --- logilab/common/deprecation.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/logilab/common/deprecation.py b/logilab/common/deprecation.py index fe2b89c..31d821f 100644 --- a/logilab/common/deprecation.py +++ b/logilab/common/deprecation.py @@ -169,17 +169,17 @@ def _get_package_name(python_object) -> Optional[str]: # instantiate abstract class 'Distribution' with abstract attributes # 'locate_file' and 'read_text' for distribution in importlib_metadata.Distribution().discover(): # type: ignore - if distribution.files: + # sometime distribution has a "name" attribute, sometime not + if distribution.files and hasattr(distribution, "name"): for file in distribution.files: - # sometime distribution has a "name" attribute, sometime not - if hasattr(distribution, "name"): - _cached_path_to_package[ - str(distribution.locate_file(file)) - ] = distribution.name - elif "name" in distribution.metadata: - _cached_path_to_package[ - str(distribution.locate_file(file)) - ] = distribution.metadata["name"] + _cached_path_to_package[str(distribution.locate_file(file))] = distribution.name + continue + + if distribution.files and "name" in distribution.metadata: + for file in distribution.files: + _cached_path_to_package[ + str(distribution.locate_file(file)) + ] = distribution.metadata["name"] try: return _cached_path_to_package.get( -- GitLab