Skip to content

Commit c8656bb

Browse files
committed
feat: support resolving deps for a non-host interpreter version
With this PR we prepare the dependency resolution machinery to support other than host python versions. This may make it possible to generate select statements based on the version but we target this usecase as it requires little code to be changed and once wired to the starlark code could be of use. Creating this PR for discussion so that we can decide on a suitable interface.
1 parent e58d2a2 commit c8656bb

File tree

2 files changed

+56
-5
lines changed

2 files changed

+56
-5
lines changed

python/pip_install/tools/wheel_installer/wheel.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,20 @@ def _as_int(value: Optional[Union[OS, Arch]]) -> int:
8484
return int(value.value)
8585

8686

87+
class MinorVersion(int):
88+
@classmethod
89+
def host(cls) -> "MinorVersion":
90+
version = platform.python_version()
91+
_, _, tail = version.partition(".")
92+
minor, _, _ = tail.partition(".")
93+
return cls(minor)
94+
95+
8796
@dataclass(frozen=True)
8897
class Platform:
8998
os: OS
9099
arch: Optional[Arch] = None
100+
minor_version: MinorVersion = MinorVersion.host()
91101

92102
@classmethod
93103
def all(cls, want_os: Optional[OS] = None) -> List["Platform"]:
@@ -235,11 +245,12 @@ def env_markers(self, extra: str) -> Dict[str, str]:
235245
"platform_system": self.platform_system,
236246
"platform_release": "", # unset
237247
"platform_version": "", # unset
248+
"python_version": f"3.{self.minor_version}",
249+
# FIXME @aignas 2024-01-14: is putting zero last a good idea?
250+
"implementation_version": f"3.{self.minor_version}.0",
251+
"python_full_version": f"3.{self.minor_version}.0",
238252
# we assume that the following are the same as the interpreter used to setup the deps:
239-
# "implementation_version": "X.Y.Z",
240253
# "implementation_name": "cpython"
241-
# "python_version": "X.Y",
242-
# "python_full_version": "X.Y.Z",
243254
# "platform_python_implementation: "CPython",
244255
}
245256

@@ -400,8 +411,9 @@ def _add_req(self, req: Requirement, extras: Set[str]) -> None:
400411
]
401412
)
402413
match_arch = "platform_machine" in marker_str
414+
match_version = "version" in marker_str
403415

404-
if not (match_os or match_arch):
416+
if not (match_os or match_arch or match_version):
405417
if any(req.marker.evaluate({"extra": extra}) for extra in extras):
406418
self._add(req.name, None)
407419
return
@@ -414,8 +426,10 @@ def _add_req(self, req: Requirement, extras: Set[str]) -> None:
414426

415427
if match_arch:
416428
self._add(req.name, plat)
417-
else:
429+
elif match_os:
418430
self._add(req.name, Platform(plat.os))
431+
elif match_version:
432+
self._add(req.name, None)
419433

420434
def build(self) -> FrozenDeps:
421435
return FrozenDeps(

python/pip_install/tools/wheel_installer/wheel_test.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,43 @@ def test_self_dependencies_can_come_in_any_order(self):
152152
self.assertEqual(["bar", "baz", "zdep"], got.deps)
153153
self.assertEqual({}, got.deps_select)
154154

155+
def test_can_get_deps_based_on_specific_python_version(self):
156+
requires_dist = [
157+
"bar",
158+
"baz; python_version < '3.8'",
159+
"posix_dep; os_name=='posix' and python_version >= '3.8'",
160+
]
161+
162+
py38_deps = wheel.Deps(
163+
"foo",
164+
requires_dist=requires_dist,
165+
platforms=[
166+
wheel.Platform(
167+
os=wheel.OS.linux, arch=wheel.Arch.x86_64, minor_version=8
168+
),
169+
],
170+
).build()
171+
py37_deps = wheel.Deps(
172+
"foo",
173+
requires_dist=requires_dist,
174+
platforms=[
175+
wheel.Platform(
176+
os=wheel.OS.linux, arch=wheel.Arch.x86_64, minor_version=7
177+
),
178+
],
179+
).build()
180+
181+
self.assertEqual(["bar", "baz"], py37_deps.deps)
182+
self.assertEqual({}, py37_deps.deps_select)
183+
self.assertEqual(["bar"], py38_deps.deps)
184+
self.assertEqual({"@platforms//os:linux": ["posix_dep"]}, py38_deps.deps_select)
185+
186+
187+
class MinorVersionTest(unittest.TestCase):
188+
def test_host(self):
189+
host = wheel.MinorVersion.host()
190+
self.assertIsNotNone(host)
191+
155192

156193
class PlatformTest(unittest.TestCase):
157194
def test_can_get_host(self):

0 commit comments

Comments
 (0)