Skip to content

Commit b21b02c

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

File tree

2 files changed

+23
-31
lines changed

2 files changed

+23
-31
lines changed

mesonpy/__init__.py

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@
5757

5858

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

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

@@ -618,13 +616,28 @@ def _string_or_strings(value: Any, name: str) -> List[str]:
618616
return config
619617

620618

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

624-
_ALLOWED_DYNAMIC_FIELDS: ClassVar[List[str]] = [
622+
allowed_dynamic_fields = [
625623
'version',
626624
]
627625

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

727740
# set version from meson.build if dynamic
728741
if 'version' in self._metadata.dynamic:
@@ -764,27 +777,6 @@ def _configure(self, reconfigure: bool = False) -> None:
764777

765778
self._run(['meson', 'setup', *setup_args])
766779

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-
788780
@cached_property
789781
def _wheel_builder(self) -> _WheelBuilder:
790782
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)