Skip to content

Commit ae5c6d6

Browse files
dnicolodirgommers
authored andcommitted
BUG: fix handling of missing "version" metadata field
This locally fixes a bug in pyproject-metadata. Fixes #454.
1 parent b94ddbd commit ae5c6d6

File tree

4 files changed

+48
-5
lines changed

4 files changed

+48
-5
lines changed

mesonpy/__init__.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,36 @@ class MesonBuilderError(Error):
219219
"""Error when building the Meson package."""
220220

221221

222+
class Metadata(pyproject_metadata.StandardMetadata):
223+
# The class method from the pyproject_metadata base class is not
224+
# typed in a subclassing friendly way, thus annotations to ignore
225+
# typing are needed.
226+
227+
@classmethod
228+
def from_pyproject(cls, data: Mapping[str, Any], project_dir: Path) -> Metadata: # type: ignore[override]
229+
metadata = super().from_pyproject(data, project_dir)
230+
231+
# Check for missing version field.
232+
if not metadata.version and 'version' not in metadata.dynamic:
233+
raise pyproject_metadata.ConfigurationError(
234+
'Required "project.version" field is missing and not declared as dynamic')
235+
236+
return metadata # type: ignore[return-value]
237+
238+
# Local fix for a bug in pyproject-metadata. See
239+
# https://github.com/mesonbuild/meson-python/issues/454
240+
def _update_dynamic(self, value: Any) -> None:
241+
if value and 'version' in self.dynamic:
242+
self.dynamic.remove('version')
243+
244+
222245
class _WheelBuilder():
223246
"""Helper class to build wheels from projects."""
224247

225248
def __init__(
226249
self,
227250
project: Project,
228-
metadata: Optional[pyproject_metadata.StandardMetadata],
251+
metadata: Optional[Metadata],
229252
source_dir: pathlib.Path,
230253
build_dir: pathlib.Path,
231254
sources: Dict[str, Dict[str, Any]],
@@ -621,7 +644,7 @@ class Project():
621644
_ALLOWED_DYNAMIC_FIELDS: ClassVar[List[str]] = [
622645
'version',
623646
]
624-
_metadata: pyproject_metadata.StandardMetadata
647+
_metadata: Metadata
625648

626649
def __init__( # noqa: C901
627650
self,
@@ -712,10 +735,9 @@ def __init__( # noqa: C901
712735

713736
# package metadata
714737
if 'project' in pyproject:
715-
self._metadata = pyproject_metadata.StandardMetadata.from_pyproject(pyproject, self._source_dir)
738+
self._metadata = Metadata.from_pyproject(pyproject, self._source_dir)
716739
else:
717-
self._metadata = pyproject_metadata.StandardMetadata(
718-
name=self._meson_name, version=packaging.version.Version(self._meson_version))
740+
self._metadata = Metadata(name=self._meson_name, version=packaging.version.Version(self._meson_version))
719741
self._validate_metadata()
720742

721743
# set version from meson.build if dynamic
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-version', version: '1.0.0')
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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-version'

tests/test_project.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
else:
1616
import tomllib
1717

18+
import pyproject_metadata
1819
import pytest
1920

2021
import mesonpy
@@ -60,6 +61,11 @@ def test_unsupported_python_version(package_unsupported_python_version):
6061
pass
6162

6263

64+
def test_missing_version(package_missing_version):
65+
with pytest.raises(pyproject_metadata.ConfigurationError, match='Required "project.version" field is missing'):
66+
with mesonpy.Project.with_temp_working_dir():
67+
pass
68+
6369
def test_user_args(package_user_args, tmp_path, monkeypatch):
6470
project_run = mesonpy.Project._run
6571
cmds = []

0 commit comments

Comments
 (0)