Skip to content

Commit 6e2b951

Browse files
committed
MAINT: refactor metadata validation
Move to stand-alone function for clarity and to enable unit testing.
1 parent 1ff8aa0 commit 6e2b951

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

724724

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

728-
_ALLOWED_DYNAMIC_FIELDS: ClassVar[List[str]] = [
728+
allowed_dynamic_fields = [
729729
'version',
730730
]
731731

732+
# check for unsupported dynamic fields
733+
unsupported_dynamic = {key for key in metadata.dynamic if key not in allowed_dynamic_fields}
734+
if unsupported_dynamic:
735+
s = ', '.join(f'"{x}"' for x in unsupported_dynamic)
736+
raise ConfigError(f'unsupported dynamic metadata fields: {s}')
737+
738+
# check if we are running on an unsupported interpreter
739+
if metadata.requires_python:
740+
metadata.requires_python.prereleases = True
741+
if platform.python_version().rstrip('+') not in metadata.requires_python:
742+
raise ConfigError(f'building with Python {platform.python_version()}, version {metadata.requires_python} required')
743+
744+
745+
class Project():
746+
"""Meson project wrapper to generate Python artifacts."""
732747
def __init__(
733748
self,
734749
source_dir: Path,
@@ -825,7 +840,7 @@ def __init__(
825840
'{yellow}{bold}! Using Meson to generate the project metadata '
826841
'(no `project` section in pyproject.toml){reset}'.format(**_STYLES)
827842
)
828-
self._validate_metadata()
843+
_validate_metadata(self._metadata)
829844

830845
# set version from meson.build if dynamic
831846
if 'version' in self._metadata.dynamic:
@@ -866,27 +881,6 @@ def _configure(self, reconfigure: bool = False) -> None:
866881

867882
self._run(['meson', 'setup', *setup_args])
868883

869-
def _validate_metadata(self) -> None:
870-
"""Check the pyproject.toml metadata and see if there are any issues."""
871-
872-
# check for unsupported dynamic fields
873-
unsupported_dynamic = {
874-
key for key in self._metadata.dynamic
875-
if key not in self._ALLOWED_DYNAMIC_FIELDS
876-
}
877-
if unsupported_dynamic:
878-
s = ', '.join(f'"{x}"' for x in unsupported_dynamic)
879-
raise MesonBuilderError(f'Unsupported dynamic fields: {s}')
880-
881-
# check if we are running on an unsupported interpreter
882-
if self._metadata.requires_python:
883-
self._metadata.requires_python.prereleases = True
884-
if platform.python_version().rstrip('+') not in self._metadata.requires_python:
885-
raise MesonBuilderError(
886-
f'Unsupported Python version {platform.python_version()}, '
887-
f'expected {self._metadata.requires_python}'
888-
)
889-
890884
@cached_property
891885
def _wheel_builder(self) -> _WheelBuilder:
892886
return _WheelBuilder(

tests/test_project.py

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

3737

3838
def test_unsupported_dynamic(package_unsupported_dynamic):
39-
with pytest.raises(mesonpy.MesonBuilderError, match='Unsupported dynamic fields: "dependencies"'):
39+
with pytest.raises(mesonpy.ConfigError, match='unsupported dynamic metadata fields: "dependencies"'):
4040
with mesonpy.Project.with_temp_working_dir():
4141
pass
4242

4343

4444
def test_unsupported_python_version(package_unsupported_python_version):
45-
with pytest.raises(mesonpy.MesonBuilderError, match=(
46-
f'Unsupported Python version {platform.python_version()}, expected ==1.0.0'
45+
with pytest.raises(mesonpy.ConfigError, match=(
46+
f'building with Python {platform.python_version()}, version ==1.0.0 required'
4747
)):
4848
with mesonpy.Project.with_temp_working_dir():
4949
pass

0 commit comments

Comments
 (0)