Skip to content

Commit 280584e

Browse files
committed
MAINT: refactor metadata validation
Move to stand-alone function for clarity and to enable unit testing.
1 parent 20f2879 commit 280584e

File tree

2 files changed

+22
-28
lines changed

2 files changed

+22
-28
lines changed

mesonpy/__init__.py

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -615,13 +615,28 @@ def _string_or_strings(value: Any, name: str) -> List[str]:
615615
return config
616616

617617

618-
class Project():
619-
"""Meson project wrapper to generate Python artifacts."""
618+
def _validate_metadata(metadata: pyproject_metadata.StandardMetadata) -> None:
619+
"""Validate package metadata."""
620620

621-
_ALLOWED_DYNAMIC_FIELDS: ClassVar[List[str]] = [
621+
allowed_dynamic_fields = [
622622
'version',
623623
]
624624

625+
# check for unsupported dynamic fields
626+
unsupported_dynamic = {key for key in metadata.dynamic if key not in allowed_dynamic_fields}
627+
if unsupported_dynamic:
628+
s = ', '.join(f'"{x}"' for x in unsupported_dynamic)
629+
raise ConfigError(f'unsupported dynamic metadata fields: {s}')
630+
631+
# check if we are running on an unsupported interpreter
632+
if metadata.requires_python:
633+
metadata.requires_python.prereleases = True
634+
if platform.python_version().rstrip('+') not in metadata.requires_python:
635+
raise ConfigError(f'building with Python {platform.python_version()}, version {metadata.requires_python} required')
636+
637+
638+
class Project():
639+
"""Meson project wrapper to generate Python artifacts."""
625640
def __init__(
626641
self,
627642
source_dir: Path,
@@ -719,7 +734,7 @@ def __init__(
719734
'{yellow}{bold}! Using Meson to generate the project metadata '
720735
'(no `project` section in pyproject.toml){reset}'.format(**_STYLES)
721736
)
722-
self._validate_metadata()
737+
_validate_metadata(self._metadata)
723738

724739
# set version from meson.build if dynamic
725740
if 'version' in self._metadata.dynamic:
@@ -761,27 +776,6 @@ def _configure(self, reconfigure: bool = False) -> None:
761776

762777
self._run(['meson', 'setup', *setup_args])
763778

764-
def _validate_metadata(self) -> None:
765-
"""Check the pyproject.toml metadata and see if there are any issues."""
766-
767-
# check for unsupported dynamic fields
768-
unsupported_dynamic = {
769-
key for key in self._metadata.dynamic
770-
if key not in self._ALLOWED_DYNAMIC_FIELDS
771-
}
772-
if unsupported_dynamic:
773-
s = ', '.join(f'"{x}"' for x in unsupported_dynamic)
774-
raise MesonBuilderError(f'Unsupported dynamic fields: {s}')
775-
776-
# check if we are running on an unsupported interpreter
777-
if self._metadata.requires_python:
778-
self._metadata.requires_python.prereleases = True
779-
if platform.python_version().rstrip('+') not in self._metadata.requires_python:
780-
raise MesonBuilderError(
781-
f'Unsupported Python version {platform.python_version()}, '
782-
f'expected {self._metadata.requires_python}'
783-
)
784-
785779
@cached_property
786780
def _wheel_builder(self) -> _WheelBuilder:
787781
return _WheelBuilder(

tests/test_project.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ def test_version(package):
4545

4646

4747
def test_unsupported_dynamic(package_unsupported_dynamic):
48-
with pytest.raises(mesonpy.MesonBuilderError, match='Unsupported dynamic fields: "dependencies"'):
48+
with pytest.raises(mesonpy.ConfigError, match='unsupported dynamic metadata fields: "dependencies"'):
4949
with mesonpy.Project.with_temp_working_dir():
5050
pass
5151

5252

5353
def test_unsupported_python_version(package_unsupported_python_version):
54-
with pytest.raises(mesonpy.MesonBuilderError, match=(
55-
f'Unsupported Python version {platform.python_version()}, expected ==1.0.0'
54+
with pytest.raises(mesonpy.ConfigError, match=(
55+
f'building with Python {platform.python_version()}, version ==1.0.0 required'
5656
)):
5757
with mesonpy.Project.with_temp_working_dir():
5858
pass

0 commit comments

Comments
 (0)