Skip to content

Commit af976ef

Browse files
committed
ENH: allow to specify editable-verbose in pyproject.toml
Build config settings passed via the build front-end take precedence over the value configured via the tool.meson-python.editable-verbose setting in pyproject.toml. For this to work, the editable-verbose config setting is extended to take an optional boolean argument: "true" or "1" are true values, "false" or "0" are false, anything else is invalid. For backward compatibility, the empty string (no argument passed to the config setting) is treated as true.
1 parent 3205db1 commit af976ef

File tree

3 files changed

+72
-7
lines changed

3 files changed

+72
-7
lines changed

mesonpy/__init__.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,7 @@ def _string_or_path(value: Any, name: str) -> str:
603603
'exclude': _strings,
604604
'include': _strings,
605605
}),
606+
'editable-verbose': _bool,
606607
})
607608

608609
table = pyproject.get('tool', {}).get('meson-python', {})
@@ -617,16 +618,24 @@ def _string(value: Any, name: str) -> str:
617618
raise ConfigError(f'Only one value for "{name}" can be specified')
618619
return value
619620

620-
def _bool(value: Any, name: str) -> bool:
621-
return True
621+
def _empty_or_bool(value: Any, name: str) -> bool:
622+
if isinstance(value, bool):
623+
return value
624+
if isinstance(value, str):
625+
if value == 'false' or value == '0':
626+
return False
627+
# For bakward compatibility, treat a missing value as True.
628+
if value == '' or value == 'true' or value == '1':
629+
return True
630+
raise ConfigError(f'Invalid value for "{name}": {value!r}')
622631

623632
def _string_or_strings(value: Any, name: str) -> List[str]:
624633
return list([value,] if isinstance(value, str) else value)
625634

626635
options = {
627636
'builddir': _string,
628637
'build-dir': _string,
629-
'editable-verbose': _bool,
638+
'editable-verbose': _empty_or_bool,
630639
'dist-args': _string_or_strings,
631640
'setup-args': _string_or_strings,
632641
'compile-args': _string_or_strings,
@@ -667,11 +676,10 @@ def __init__(
667676
source_dir: Path,
668677
build_dir: Path,
669678
meson_args: Optional[MesonArgs] = None,
670-
editable_verbose: bool = False,
679+
editable_verbose: Optional[bool] = None,
671680
) -> None:
672681
self._source_dir = pathlib.Path(source_dir).absolute()
673682
self._build_dir = pathlib.Path(build_dir).absolute()
674-
self._editable_verbose = editable_verbose
675683
self._meson_native_file = self._build_dir / 'meson-python-native-file.ini'
676684
self._meson_cross_file = self._build_dir / 'meson-python-cross-file.ini'
677685
self._meson_args: MesonArgs = collections.defaultdict(list)
@@ -685,6 +693,10 @@ def __init__(
685693
for key, value in pyproject_config.get('args', {}).items():
686694
self._meson_args[key].extend(value)
687695

696+
# editable-verbose setting from build options takes precedence over
697+
# setting in pyproject.toml
698+
self._editable_verbose = editable_verbose if editable_verbose is not None else pyproject_config.get('editable-verbose')
699+
688700
# meson arguments from the command line take precedence over
689701
# arguments from the configuration file thus are added later
690702
if meson_args:
@@ -1114,7 +1126,7 @@ def _project(config_settings: Optional[Dict[Any, Any]] = None) -> Iterator[Proje
11141126
meson_args = typing.cast('MesonArgs', {name: settings.get(f'{name}-args', []) for name in _MESON_ARGS_KEYS})
11151127
source_dir = os.path.curdir
11161128
build_dir = settings.get('build-dir')
1117-
editable_verbose = bool(settings.get('editable-verbose'))
1129+
editable_verbose = settings.get('editable-verbose')
11181130

11191131
with contextlib.ExitStack() as ctx:
11201132
if build_dir is None:

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def meson_fatal_warnings():
200200
mpatch = pytest.MonkeyPatch()
201201
mesonpy_project_init = mesonpy.Project.__init__
202202

203-
def __init__(self, source_dir, build_dir, meson_args=None, editable_verbose=False):
203+
def __init__(self, source_dir, build_dir, meson_args=None, editable_verbose=None):
204204
if pathlib.Path(source_dir).absolute().name not in {
205205

206206
# The CMake subproject emits ``WARNING: CMake Toolchain:

tests/test_project.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import mesonpy
2525

26+
from mesonpy._util import chdir
2627
from .conftest import MESON_VERSION, in_git_repo_context, metadata, package_dir
2728

2829

@@ -410,3 +411,55 @@ def test_ios_project(package_simple, monkeypatch, multiarch, tmp_path):
410411
assert "\nsystem = 'ios'\n" in cross_config
411412
assert f"\nc = '{arch}-apple-{subsystem}-clang'\n" in cross_config
412413
assert f"\nsubsystem = '{subsystem}'\n" in cross_config
414+
415+
416+
@pytest.mark.parametrize('verbose', ['true', 'false'])
417+
def test_editable_verbose_pyproject(tmp_path, verbose):
418+
tmp_path.joinpath('pyproject.toml').write_text(textwrap.dedent(f'''
419+
[build-system]
420+
build-backend = 'mesonpy'
421+
requires = ['meson-python']
422+
423+
[project]
424+
name = 'test'
425+
version = '1.0.0'
426+
427+
[tool.meson-python]
428+
editable-verbose = {verbose}
429+
'''), encoding='utf8')
430+
431+
tmp_path.joinpath('meson.build').write_text(textwrap.dedent('''
432+
project('test')
433+
'''), encoding='utf8')
434+
435+
project = mesonpy.Project(tmp_path, tmp_path / 'build')
436+
assert project._editable_verbose == (verbose == 'true')
437+
438+
439+
@pytest.mark.parametrize('settings,expected', [
440+
({}, True),
441+
({'editable-verbose': ''}, True),
442+
({'editable-verbose': 'true'}, True),
443+
({'editable-verbose': 'false'}, False),
444+
], ids=['', '-Ceditable-verbose', '-Ceditbale-verbose=true', '-Ceditable-verbose=false'])
445+
def test_editable_verbose_settings(tmp_path, settings, expected):
446+
tmp_path.joinpath('pyproject.toml').write_text(textwrap.dedent('''
447+
[build-system]
448+
build-backend = 'mesonpy'
449+
requires = ['meson-python']
450+
451+
[project]
452+
name = 'test'
453+
version = '1.0.0'
454+
455+
[tool.meson-python]
456+
editable-verbose = true
457+
'''), encoding='utf8')
458+
459+
tmp_path.joinpath('meson.build').write_text(textwrap.dedent('''
460+
project('test')
461+
'''), encoding='utf8')
462+
463+
with chdir(tmp_path):
464+
with mesonpy._project(settings) as project:
465+
assert project._editable_verbose == expected

0 commit comments

Comments
 (0)