Skip to content

Commit 92a0e8d

Browse files
dnicolodirgommers
authored andcommitted
ENH: detect missing version information in meson.build when required
When pyproject.toml does not contain a "project" section, or when the "version" field is declared as dynamic, the package version is retrieved from the one specified in the project() call in meson.build. However, providing a version in meson.build is optional. Detect when the version is missing and report an appropriate error.
1 parent 274d72a commit 92a0e8d

File tree

6 files changed

+51
-2
lines changed

6 files changed

+51
-2
lines changed

mesonpy/__init__.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -734,10 +734,18 @@ def __init__( # noqa: C901
734734
self._metadata = Metadata.from_pyproject(pyproject, self._source_dir)
735735
# set version from meson.build if version is declared as dynamic
736736
if 'version' in self._metadata.dynamic:
737-
self._metadata.version = packaging.version.Version(self._meson_version)
737+
version = self._meson_version
738+
if version == 'undefined':
739+
raise pyproject_metadata.ConfigurationError(
740+
'Field "version" declared as dynamic but version is not defined in meson.build')
741+
self._metadata.version = packaging.version.Version(version)
738742
else:
739743
# if project section is missing, use minimal metdata from meson.build
740-
self._metadata = Metadata(name=self._meson_name, version=packaging.version.Version(self._meson_version))
744+
name, version = self._meson_name, self._meson_version
745+
if version == 'undefined':
746+
raise pyproject_metadata.ConfigurationError(
747+
'Section "project" missing in pyproject.toml and version is not defined in meson.build')
748+
self._metadata = Metadata(name=name, version=packaging.version.Version(version))
741749

742750
# verify that we are running on a supported interpreter
743751
if self._metadata.requires_python:
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# SPDX-FileCopyrightText: 2023 The meson-python developers
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
project('missing-dynamic-version')
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# SPDX-FileCopyrightText: 2023 The meson-python developers
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
[build-system]
6+
build-backend = 'mesonpy'
7+
requires = ['meson-python']
8+
9+
[project]
10+
name = 'missing-dynamic-version'
11+
dynamic = ['version']
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# SPDX-FileCopyrightText: 2023 The meson-python developers
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
project('missing-meson-version')
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# SPDX-FileCopyrightText: 2023 The meson-python developers
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
[build-system]
6+
build-backend = 'mesonpy'
7+
requires = ['meson-python']

tests/test_project.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,19 @@ def test_missing_version(package_missing_version):
6464
with mesonpy.Project.with_temp_working_dir():
6565
pass
6666

67+
68+
def test_missing_meson_version(package_missing_meson_version):
69+
with pytest.raises(pyproject_metadata.ConfigurationError, match='Section "project" missing in pyproject.toml'):
70+
with mesonpy.Project.with_temp_working_dir():
71+
pass
72+
73+
74+
def test_missing_dynamic_version(package_missing_dynamic_version):
75+
with pytest.raises(pyproject_metadata.ConfigurationError, match='Field "version" declared as dynamic but'):
76+
with mesonpy.Project.with_temp_working_dir():
77+
pass
78+
79+
6780
def test_user_args(package_user_args, tmp_path, monkeypatch):
6881
project_run = mesonpy.Project._run
6982
cmds = []

0 commit comments

Comments
 (0)