Skip to content

Commit b96b9c6

Browse files
dnicolodirgommers
authored andcommitted
MAINT: refactor metadata validation
1 parent 62ede94 commit b96b9c6

File tree

2 files changed

+20
-37
lines changed

2 files changed

+20
-37
lines changed

mesonpy/__init__.py

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@
5656

5757

5858
if typing.TYPE_CHECKING: # pragma: no cover
59-
from typing import (
60-
Any, Callable, ClassVar, DefaultDict, List, Literal, Optional, Sequence, TextIO, Tuple, Type, TypeVar, Union
61-
)
59+
from typing import Any, Callable, DefaultDict, List, Literal, Optional, Sequence, TextIO, Tuple, Type, TypeVar, Union
6260

6361
from mesonpy._compat import Iterator, ParamSpec, Path
6462

@@ -233,6 +231,12 @@ def from_pyproject(cls, data: Mapping[str, Any], project_dir: Path) -> Metadata:
233231
raise pyproject_metadata.ConfigurationError(
234232
'Required "project.version" field is missing and not declared as dynamic')
235233

234+
# Check for unsupported dynamic fields.
235+
unsupported_dynamic = {key for key in metadata.dynamic if key not in {'version', }}
236+
if unsupported_dynamic:
237+
fields = ', '.join(f'"{x}"' for x in unsupported_dynamic)
238+
raise pyproject_metadata.ConfigurationError(f'Unsupported dynamic fields: {fields}')
239+
236240
return metadata # type: ignore[return-value]
237241

238242
# Local fix for a bug in pyproject-metadata. See
@@ -641,11 +645,6 @@ def _string_or_strings(value: Any, name: str) -> List[str]:
641645
class Project():
642646
"""Meson project wrapper to generate Python artifacts."""
643647

644-
_ALLOWED_DYNAMIC_FIELDS: ClassVar[List[str]] = [
645-
'version',
646-
]
647-
_metadata: Metadata
648-
649648
def __init__( # noqa: C901
650649
self,
651650
source_dir: Path,
@@ -736,13 +735,20 @@ def __init__( # noqa: C901
736735
# package metadata
737736
if 'project' in pyproject:
738737
self._metadata = Metadata.from_pyproject(pyproject, self._source_dir)
738+
# set version from meson.build if version is declared as dynamic
739+
if 'version' in self._metadata.dynamic:
740+
self._metadata.version = packaging.version.Version(self._meson_version)
739741
else:
742+
# if project section is missing, use minimal metdata from meson.build
740743
self._metadata = Metadata(name=self._meson_name, version=packaging.version.Version(self._meson_version))
741-
self._validate_metadata()
742744

743-
# set version from meson.build if dynamic
744-
if 'version' in self._metadata.dynamic:
745-
self._metadata.version = packaging.version.Version(self._meson_version)
745+
# verify that we are running on a supported interpreter
746+
if self._metadata.requires_python:
747+
self._metadata.requires_python.prereleases = True
748+
if platform.python_version().rstrip('+') not in self._metadata.requires_python:
749+
raise MesonBuilderError(
750+
f'Package requires Python version {self._metadata.requires_python}, '
751+
f'running on {platform.python_version()}')
746752

747753
# limited API
748754
self._limited_api = pyproject_config.get('limited-api', False)
@@ -784,27 +790,6 @@ def _configure(self, reconfigure: bool = False) -> None:
784790

785791
self._run(['meson', 'setup', *setup_args])
786792

787-
def _validate_metadata(self) -> None:
788-
"""Check the pyproject.toml metadata and see if there are any issues."""
789-
790-
# check for unsupported dynamic fields
791-
unsupported_dynamic = {
792-
key for key in self._metadata.dynamic
793-
if key not in self._ALLOWED_DYNAMIC_FIELDS
794-
}
795-
if unsupported_dynamic:
796-
s = ', '.join(f'"{x}"' for x in unsupported_dynamic)
797-
raise MesonBuilderError(f'Unsupported dynamic fields: {s}')
798-
799-
# check if we are running on an unsupported interpreter
800-
if self._metadata.requires_python:
801-
self._metadata.requires_python.prereleases = True
802-
if platform.python_version().rstrip('+') not in self._metadata.requires_python:
803-
raise MesonBuilderError(
804-
f'Unsupported Python version {platform.python_version()}, '
805-
f'expected {self._metadata.requires_python}'
806-
)
807-
808793
@cached_property
809794
def _wheel_builder(self) -> _WheelBuilder:
810795
return _WheelBuilder(

tests/test_project.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,13 @@ def test_version(package):
4848

4949

5050
def test_unsupported_dynamic(package_unsupported_dynamic):
51-
with pytest.raises(mesonpy.MesonBuilderError, match='Unsupported dynamic fields: "dependencies"'):
51+
with pytest.raises(pyproject_metadata.ConfigurationError, match='Unsupported dynamic fields: "dependencies"'):
5252
with mesonpy.Project.with_temp_working_dir():
5353
pass
5454

5555

5656
def test_unsupported_python_version(package_unsupported_python_version):
57-
with pytest.raises(mesonpy.MesonBuilderError, match=(
58-
f'Unsupported Python version {platform.python_version()}, expected ==1.0.0'
59-
)):
57+
with pytest.raises(mesonpy.MesonBuilderError, match='Package requires Python version ==1.0.0'):
6058
with mesonpy.Project.with_temp_working_dir():
6159
pass
6260

0 commit comments

Comments
 (0)