Skip to content

Commit 1539684

Browse files
authored
Merge pull request #601 from scipp/conda-package-metadata
Fallback for metadata of conda packages
2 parents 6e0e549 + e2e9ea1 commit 1539684

File tree

2 files changed

+44
-11
lines changed

2 files changed

+44
-11
lines changed

src/scippneutron/metadata/_model.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,9 @@ def from_package_metadata(cls, package_name: str) -> Software:
255255
:
256256
A Software instance.
257257
"""
258-
from importlib.metadata import version
259-
260258
return cls(
261259
name=package_name,
262-
version=version(package_name),
260+
version=_deduce_package_version(package_name),
263261
url=_deduce_package_source_url(package_name),
264262
doi=None,
265263
)
@@ -277,10 +275,39 @@ def compact_repr(self) -> str:
277275
return self.name_version
278276

279277

278+
def _deduce_package_version(package_name: str) -> str | None:
279+
from importlib.metadata import PackageNotFoundError, version
280+
281+
try:
282+
return version(package_name)
283+
except PackageNotFoundError:
284+
# Either the package is not installed or has no metadata.
285+
from importlib import import_module
286+
287+
try:
288+
package = import_module(package_name)
289+
except ModuleNotFoundError as e:
290+
raise e from None
291+
292+
try:
293+
return package.__version__
294+
except AttributeError:
295+
raise RuntimeError(
296+
f"Package '{package_name}' has no metadata and no "
297+
f"__version__ attribute. Specify the version manually."
298+
) from None
299+
300+
280301
def _deduce_package_source_url(package_name: str) -> str | None:
281-
from importlib.metadata import metadata
302+
from importlib.metadata import PackageNotFoundError, metadata
303+
304+
try:
305+
meta = metadata(package_name)
306+
except PackageNotFoundError:
307+
# Either the package is not installed or has no metadata.
308+
return None
282309

283-
if not (urls := metadata(package_name).get_all("project-url")):
310+
if not (urls := meta.get_all("project-url")):
284311
return None
285312

286313
try:

tests/metadata/metadata_model_test.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Copyright (c) 2025 Scipp contributors (https://github.com/scipp)
33

44

5+
import pytest
56
import scipp as sc
67
import scippnexus as snx
78
from dateutil.parser import parse as parse_datetime
@@ -44,10 +45,15 @@ def test_software_from_from_package_metadata_first_party() -> None:
4445

4546
def test_software_from_from_package_metadata_third_party() -> None:
4647
software = metadata.Software.from_package_metadata('scipp')
47-
expected = metadata.Software(
48-
name='scipp',
49-
version=sc.__version__,
50-
url='https://github.com/scipp/scipp',
51-
doi=None, # Cannot be deduced
48+
assert software.name == 'scipp'
49+
assert software.version == sc.__version__
50+
assert software.url in (
51+
'https://github.com/scipp/scipp', # properly deduced
52+
None, # fallback for our conda packages
5253
)
53-
assert software == expected
54+
assert software.doi is None
55+
56+
57+
def test_software_from_from_package_metadata_fails_when_package_not_installed() -> None:
58+
with pytest.raises(ModuleNotFoundError):
59+
metadata.Software.from_package_metadata('not-a-package')

0 commit comments

Comments
 (0)