Skip to content

Commit 2faad4a

Browse files
committed
ENH: revise error messages for consistency
1 parent 14bf8a0 commit 2faad4a

File tree

3 files changed

+33
-48
lines changed

3 files changed

+33
-48
lines changed

mesonpy/__init__.py

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ def __init__( # noqa: C901
587587
if archflags is not None:
588588
arch, *other = filter(None, (x.strip() for x in archflags.split('-arch')))
589589
if other:
590-
raise ConfigError(f'multi-architecture builds are not supported but $ARCHFLAGS={archflags!r}')
590+
raise ConfigError(f'Multi-architecture builds are not supported but $ARCHFLAGS={archflags!r}')
591591
macver, _, nativearch = platform.mac_ver()
592592
if arch != nativearch:
593593
x = self._env.setdefault('_PYTHON_HOST_PLATFORM', f'macosx-{macver}-{arch}')
@@ -676,10 +676,7 @@ def _get_config_key(self, key: str) -> Any:
676676
value: Any = self._config
677677
for part in f'tool.meson-python.{key}'.split('.'):
678678
if not isinstance(value, Mapping):
679-
raise ConfigError(
680-
f'Found unexpected value in `{part}` when looking for '
681-
f'config key `tool.meson-python.{key}` (`{value}`)'
682-
)
679+
raise ConfigError(f'Configuration entry "tool.meson-python.{key}" should be a TOML table not {type(value)}')
683680
value = value.get(part, {})
684681
return value
685682

@@ -734,29 +731,28 @@ def _validate_metadata(self) -> None:
734731
if key not in self._ALLOWED_DYNAMIC_FIELDS
735732
}
736733
if unsupported_dynamic:
737-
raise MesonBuilderError('Unsupported dynamic fields: {}'.format(
738-
', '.join(unsupported_dynamic)),
739-
)
734+
s = ', '.join(f'"{x}"' for x in unsupported_dynamic)
735+
raise MesonBuilderError(f'Unsupported dynamic fields: {s}')
740736

741737
# check if we are running on an unsupported interpreter
742738
if self._metadata.requires_python:
743739
self._metadata.requires_python.prereleases = True
744740
if platform.python_version().rstrip('+') not in self._metadata.requires_python:
745741
raise MesonBuilderError(
746-
f'Unsupported Python version `{platform.python_version()}`, '
747-
f'expected `{self._metadata.requires_python}`'
742+
f'Unsupported Python version {platform.python_version()}, '
743+
f'expected {self._metadata.requires_python}'
748744
)
749745

750746
def _check_for_unknown_config_keys(self, valid_args: Mapping[str, Collection[str]]) -> None:
751747
config = self._config.get('tool', {}).get('meson-python', {})
752748

753749
for key, valid_subkeys in config.items():
754750
if key not in valid_args:
755-
raise ConfigError(f'Unknown configuration key: tool.meson-python.{key}')
751+
raise ConfigError(f'Unknown configuration key "tool.meson-python.{key}"')
756752

757753
for subkey in valid_args[key]:
758754
if subkey not in valid_subkeys:
759-
raise ConfigError(f'Unknown configuration key: tool.meson-python.{key}.{subkey}')
755+
raise ConfigError(f'Unknown configuration key "tool.meson-python.{key}.{subkey}"')
760756

761757
@cached_property
762758
def _wheel_builder(self) -> _WheelBuilder:
@@ -978,10 +974,10 @@ def _project(config_settings: Optional[Dict[Any, Any]]) -> Iterator[Project]:
978974
builddir_value = config_settings.get('builddir', {})
979975
if len(builddir_value) > 0:
980976
if len(builddir_value) != 1:
981-
raise ConfigError('Specified multiple values for `builddir`, only one is allowed')
977+
raise ConfigError('Only one value for configuration entry "builddir" can be specified')
982978
builddir = builddir_value[0]
983979
if not isinstance(builddir, str):
984-
raise ConfigError(f'Config option `builddir` should be a string (found `{type(builddir)}`)')
980+
raise ConfigError(f'Configuration entry "builddir" should be a string not {type(builddir)}')
985981
else:
986982
builddir = None
987983

@@ -992,14 +988,8 @@ def _validate_string_collection(key: str) -> None:
992988
for item in config_settings.get(key, ())
993989
)))
994990
if problematic_items:
995-
raise ConfigError(
996-
f'Config option `{key}` should only contain string items, but '
997-
'contains the following parameters that do not meet this criteria:' +
998-
''.join((
999-
f'\t- {item} (type: {type(item)})'
1000-
for item in problematic_items
1001-
))
1002-
)
991+
s = ', '.join(f'"{item}" ({type(item)})' for item in problematic_items)
992+
raise ConfigError(f'Configuration entries for "{key}" must be strings but contain: {s}')
1003993

1004994
meson_args_keys = typing_get_args(MesonArgsKeys)
1005995
meson_args_cli_keys = tuple(f'{key}-args' for key in meson_args_keys)
@@ -1010,10 +1000,10 @@ def _validate_string_collection(key: str) -> None:
10101000
import difflib
10111001
matches = difflib.get_close_matches(key, known_keys, n=3)
10121002
if len(matches):
1013-
postfix = f'Did you mean one of: {matches}'
1003+
alternatives = ' or '.join(f'"{match}"' for match in matches)
1004+
raise ConfigError(f'Unknown configuration entry "{key}". Did you mean {alternatives}?')
10141005
else:
1015-
postfix = 'There are no close valid keys.'
1016-
raise ConfigError(f'Unknown config setting: {key!r}. {postfix}')
1006+
raise ConfigError(f'Unknown configuration entry "{key}"')
10171007

10181008
for key in meson_args_cli_keys:
10191009
_validate_string_collection(key)

tests/test_pep517.py

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,19 @@ def run(cmd: List[str], *args: object, **kwargs: object) -> subprocess.Completed
5656
assert set(mesonpy.get_requires_for_build_wheel()) == expected
5757

5858

59-
def test_invalid_config_settings(package_pure, tmp_path_session):
60-
raises_error = pytest.raises(mesonpy.ConfigError, match="Unknown config setting: 'invalid'")
61-
62-
with raises_error:
63-
mesonpy.build_sdist(tmp_path_session, {'invalid': ()})
64-
with raises_error:
65-
mesonpy.build_wheel(tmp_path_session, {'invalid': ()})
66-
67-
68-
def test_invalid_config_settings_suggest(package_pure, tmp_path_session):
69-
raises_error = pytest.raises(
70-
mesonpy.ConfigError,
71-
match='Did you mean one of: .*setup-args'
72-
)
73-
74-
with raises_error:
75-
mesonpy.build_sdist(tmp_path_session, {'setup_args': ()})
76-
77-
with raises_error:
78-
mesonpy.build_wheel(tmp_path_session, {'setup_args': ()})
59+
def test_invalid_config_settings(capsys, package_pure, tmp_path_session):
60+
for method in mesonpy.build_sdist, mesonpy.build_wheel:
61+
with pytest.raises(SystemExit):
62+
method(tmp_path_session, {'invalid': ()})
63+
out, err = capsys.readouterr()
64+
assert out.splitlines()[-1].endswith(
65+
'Unknown configuration entry "invalid"')
66+
67+
68+
def test_invalid_config_settings_suggest(capsys, package_pure, tmp_path_session):
69+
for method in mesonpy.build_sdist, mesonpy.build_wheel:
70+
with pytest.raises(SystemExit):
71+
method(tmp_path_session, {'setup_args': ()})
72+
out, err = capsys.readouterr()
73+
assert out.splitlines()[-1].endswith(
74+
'Unknown configuration entry "setup_args". Did you mean "setup-args" or "dist-args"?')

tests/test_project.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,14 @@ def test_version(package):
3737

3838

3939
def test_unsupported_dynamic(package_unsupported_dynamic):
40-
with pytest.raises(mesonpy.MesonBuilderError, match='Unsupported dynamic fields: dependencies'):
40+
with pytest.raises(mesonpy.MesonBuilderError, match='Unsupported dynamic fields: "dependencies"'):
4141
with mesonpy.Project.with_temp_working_dir():
4242
pass
4343

4444

4545
def test_unsupported_python_version(package_unsupported_python_version):
4646
with pytest.raises(mesonpy.MesonBuilderError, match=(
47-
f'Unsupported Python version `{platform.python_version()}`, '
48-
'expected `==1.0.0`'
47+
f'Unsupported Python version {platform.python_version()}, expected ==1.0.0'
4948
)):
5049
with mesonpy.Project.with_temp_working_dir():
5150
pass

0 commit comments

Comments
 (0)