Skip to content

Commit 078b4a4

Browse files
committed
MAINT: refactor metadata validation
Move to stand-alone function for clarity and to enable unit testing.
1 parent 8559f51 commit 078b4a4

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
@@ -618,13 +618,28 @@ def _string_or_strings(value: Any, name: str) -> List[str]:
618618
return config
619619

620620

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

624-
_ALLOWED_DYNAMIC_FIELDS: ClassVar[List[str]] = [
624+
allowed_dynamic_fields = [
625625
'version',
626626
]
627627

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

727742
# set version from meson.build if dynamic
728743
if 'version' in self._metadata.dynamic:
@@ -764,27 +779,6 @@ def _configure(self, reconfigure: bool = False) -> None:
764779

765780
self._run(['meson', 'setup', *setup_args])
766781

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