Skip to content

Commit 51ddc7d

Browse files
committed
MAINT: simplify optional dependencies handling
Remove one layer of indirection to make code easier to follow. Extend the tests to verify that the returned requirement strings are in the correct format.
1 parent 57cb47c commit 51ddc7d

File tree

2 files changed

+18
-21
lines changed

2 files changed

+18
-21
lines changed

mesonpy/__init__.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,6 @@
8585
_MESON_REQUIRED_VERSION = '0.63.3' # keep in sync with the version requirement in pyproject.toml
8686

8787

88-
class _depstr:
89-
"""Namespace that holds the requirement strings for dependencies we *might*
90-
need at runtime. Having them in one place makes it easier to update.
91-
"""
92-
patchelf = 'patchelf >= 0.11.0'
93-
ninja = f'ninja >= {_NINJA_REQUIRED_VERSION}'
94-
95-
9688
def _init_colors() -> Dict[str, str]:
9789
"""Detect if we should be using colors in the output. We will enable colors
9890
if running in a TTY, and no environment variable overrides it. Setting the
@@ -1017,12 +1009,13 @@ def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
10171009

10181010

10191011
@_pyproject_hook
1020-
def get_requires_for_build_sdist(
1021-
config_settings: Optional[Dict[str, str]] = None,
1022-
) -> List[str]:
1012+
def get_requires_for_build_sdist(config_settings: Optional[Dict[str, str]] = None) -> List[str]:
1013+
dependencies = []
1014+
10231015
if os.environ.get('NINJA') is None and _env_ninja_command() is None:
1024-
return [_depstr.ninja]
1025-
return []
1016+
dependencies.append(f'ninja >= {_NINJA_REQUIRED_VERSION}')
1017+
1018+
return dependencies
10261019

10271020

10281021
@_pyproject_hook
@@ -1042,10 +1035,10 @@ def get_requires_for_build_wheel(config_settings: Optional[Dict[str, str]] = Non
10421035
dependencies = []
10431036

10441037
if os.environ.get('NINJA') is None and _env_ninja_command() is None:
1045-
dependencies.append(_depstr.ninja)
1038+
dependencies.append(f'ninja >= {_NINJA_REQUIRED_VERSION}')
10461039

10471040
if sys.platform.startswith('linux') and not shutil.which('patchelf'):
1048-
dependencies.append(_depstr.patchelf)
1041+
dependencies.append('patchelf >= 0.11.0')
10491042

10501043
return dependencies
10511044

tests/test_pep517.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from typing import List
1010

11+
import packaging.requirements
1112
import pytest
1213

1314
import mesonpy
@@ -41,15 +42,18 @@ def run(cmd: List[str], *args: object, **kwargs: object) -> subprocess.Completed
4142

4243
expected = set()
4344

44-
ninja_available = ninja is not None and [int(x) for x in ninja.split('.')] >= [1, 8, 2]
45-
46-
if not ninja_available:
47-
expected |= {mesonpy._depstr.ninja}
45+
if ninja is None or mesonpy._parse_version_string(ninja) < (1, 8, 2):
46+
expected.add('ninja')
4847

4948
if system_patchelf is None and sys.platform.startswith('linux'):
50-
expected |= {mesonpy._depstr.patchelf}
49+
expected.add('patchelf')
50+
51+
requirements = mesonpy.get_requires_for_build_wheel()
52+
53+
# Check that the requirement strings are in the correct format.
54+
names = {packaging.requirements.Requirement(x).name for x in requirements}
5155

52-
assert set(mesonpy.get_requires_for_build_wheel()) == expected
56+
assert names == expected
5357

5458

5559
def test_invalid_config_settings(capsys, package_pure, tmp_path_session):

0 commit comments

Comments
 (0)