Skip to content

Commit 4bebb11

Browse files
authored
fix(sdk): improve package name detection with type hints and null safety (#2618)
1 parent 694601e commit 4bebb11

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

packages/traceloop-sdk/traceloop/sdk/utils/package_check.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
1-
from importlib.metadata import distributions
1+
from importlib.metadata import Distribution, distributions
22

33

4-
def _get_package_name(dist):
5-
# Try both 'Name' and 'name' keys to handle different metadata formats
6-
if hasattr(dist, 'metadata') and dist.metadata is not None:
7-
for key in ('Name', 'name'):
8-
try:
9-
return dist.metadata[key].lower()
10-
except (KeyError, AttributeError):
11-
continue
4+
def _get_package_name(dist: Distribution) -> str | None:
5+
try:
6+
return dist.name.lower()
7+
except (KeyError, AttributeError):
8+
return None
129

13-
# If metadata is missing or neither key exists, use the distribution name directly
14-
return dist.name.lower()
1510

16-
17-
installed_packages = {_get_package_name(dist) for dist in distributions()}
11+
installed_packages = {name for dist in distributions() if (name := _get_package_name(dist)) is not None}
1812

1913

2014
def is_package_installed(package_name: str) -> bool:

0 commit comments

Comments
 (0)