Skip to content

Commit 3e05fbf

Browse files
committed
feat(pypi) Handle local version in requirement parsing
1 parent 155efce commit 3e05fbf

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

examples/bzlmod/MODULE.bazel.lock

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

python/private/pypi/parse_requirements.bzl

+10-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ load(":index_sources.bzl", "index_sources")
3232
load(":parse_requirements_txt.bzl", "parse_requirements_txt")
3333
load(":whl_target_platforms.bzl", "select_whls")
3434

35+
def extract_version(entry):
36+
"""Extract the version part from the requirement string."""
37+
version_start = entry.find("==")
38+
if version_start != -1:
39+
# Extract everything after '==' until the next space or end of the string
40+
version = entry[version_start + 2:].split(" ")[0]
41+
return version
42+
return None
43+
3544
def parse_requirements(
3645
ctx,
3746
*,
@@ -92,7 +101,7 @@ def parse_requirements(
92101
# are returned as just the base package name. e.g., `foo[bar]` results
93102
# in an entry like `("foo", "foo[bar] == 1.0 ...")`.
94103
requirements_dict = {
95-
normalize_name(entry[0]): entry
104+
(entry[0], extract_version(entry[1])): entry
96105
for entry in sorted(
97106
parse_result.requirements,
98107
# Get the longest match and fallback to original WORKSPACE sorting,

tests/pypi/parse_requirements/parse_requirements_tests.bzl

+47
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ foo==0.0.3 --hash=sha256:deadbaaf
6666
"requirements_windows": """\
6767
foo[extra]==0.0.2 --hash=sha256:deadbeef
6868
bar==0.0.1 --hash=sha256:deadb00f
69+
""",
70+
"requirements_different_package_version": """\
71+
foo==0.0.1+local --hash=sha256:deadbeef
72+
foo==0.0.1 --hash=sha256:deadb00f
6973
""",
7074
}
7175

@@ -382,6 +386,49 @@ def _test_env_marker_resolution(env):
382386

383387
_tests.append(_test_env_marker_resolution)
384388

389+
# <list [struct(distribution = "foo", extra_pip_args = [], is_exposed = True, requirement_line = "foo==0.0.1 --hash=sha256:deadb00f", sdist = None, srcs = struct(requirement = "foo==0.0.1", shas = ["deadb00f"], version = "0.0.1"), target_platforms = ["linux_x86_64"], whls = []), struct(distribution = "foo", extra_pip_args = [], is_exposed = True, requirement_line = "foo==0.0.1+local --hash=sha256:deadbeef", sdist = None, srcs = struct(requirement = "foo==0.0.1+local", shas = ["deadbeef"], version = "0.0.1+local"), target_platforms = ["linux_x86_64"], whls = [])]>
390+
def _test_different_package_version(env):
391+
got = parse_requirements(
392+
ctx = _mock_ctx(),
393+
requirements_by_platform = {
394+
"requirements_different_package_version": ["linux_x86_64"],
395+
},
396+
)
397+
env.expect.that_dict(got).contains_exactly({
398+
"foo": [
399+
struct(
400+
distribution = "foo",
401+
extra_pip_args = [],
402+
requirement_line = "foo==0.0.1 --hash=sha256:deadb00f",
403+
srcs = struct(
404+
requirement = "foo==0.0.1",
405+
shas = ["deadb00f"],
406+
version = "0.0.1",
407+
),
408+
target_platforms = ["linux_x86_64"],
409+
whls = [],
410+
sdist = None,
411+
is_exposed = True,
412+
),
413+
struct(
414+
distribution = "foo",
415+
extra_pip_args = [],
416+
requirement_line = "foo==0.0.1+local --hash=sha256:deadbeef",
417+
srcs = struct(
418+
requirement = "foo==0.0.1+local",
419+
shas = ["deadbeef"],
420+
version = "0.0.1+local",
421+
),
422+
target_platforms = ["linux_x86_64"],
423+
whls = [],
424+
sdist = None,
425+
is_exposed = True,
426+
),
427+
],
428+
})
429+
430+
_tests.append(_test_different_package_version)
431+
385432
def parse_requirements_test_suite(name):
386433
"""Create the test suite.
387434

0 commit comments

Comments
 (0)