Skip to content

Commit 26ec1cc

Browse files
committed
refactor(pypi): pre-parse requirements and hoist argparse calls out of platform loop
Address review feedback: pre-parse requirements once per file instead of per-entry per-platform, hoist argparse calls outside the platform loop, and move platforms.get(plat) outside the entry loop.
1 parent fbf7e5a commit 26ec1cc

1 file changed

Lines changed: 19 additions & 25 deletions

File tree

python/private/pypi/parse_requirements.bzl

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -107,38 +107,32 @@ def parse_requirements(
107107
tokenized_options.append(p)
108108

109109
pip_args = tokenized_options + extra_pip_args
110-
for plat in plats:
111-
filtered_entries = []
112-
for entry in parse_result.requirements:
113-
requirement_line = entry[1]
114110

115-
# Evaluate markers inline at parse time, so we only keep
116-
# entries whose markers match the current platform.
117-
if ";" in requirement_line:
118-
plat_env = platforms.get(plat)
119-
if not plat_env:
120-
continue
111+
# Parse the index URL from the requirement files once per file
112+
index_url = argparse.index_url(pip_args, index_url)
113+
extra_index_urls = argparse.extra_index_url(pip_args, [])
114+
if argparse.platform(pip_args, []):
115+
# No use of downloader if the user specifies "--platform" pip arg. This means that
116+
# they intend to use pip to download the wheels
117+
#
118+
# TODO @aignas 2026-04-11: consider removing this line in the next major release
119+
# (3.0).
120+
get_index_urls = None
121+
122+
# Pre-parse requirements once per file to avoid redundant parsing in loops
123+
parsed_reqs = [(entry, requirement(entry[1])) for entry in parse_result.requirements]
121124

122-
req = requirement(requirement_line)
123-
if req.marker and not evaluate(req.marker, env = plat_env.env):
124-
continue
125+
for plat in plats:
126+
plat_env = platforms.get(plat)
125127

128+
filtered_entries = []
129+
for entry, req in parsed_reqs:
130+
if req.marker and (not plat_env or not evaluate(req.marker, env = plat_env.env)):
131+
continue
126132
filtered_entries.append(entry)
127133
requirements[plat] = filtered_entries
128134
options[plat] = pip_args
129135

130-
# Parse the index URL from the requirement files
131-
index_url = argparse.index_url(pip_args, index_url)
132-
extra_index_urls = argparse.extra_index_url(pip_args, [])
133-
platform = argparse.platform(pip_args, [])
134-
if platform:
135-
# No use of downloader if the user specifies "--platform" pip arg. This means that
136-
# they intend to use pip to download the wheels
137-
#
138-
# TODO @aignas 2026-04-11: consider removing this line in the next major release
139-
# (3.0).
140-
get_index_urls = None
141-
142136
requirements_by_platform = {}
143137
for plat, parse_results in requirements.items():
144138
# Replicate a surprising behavior that WORKSPACE builds allowed:

0 commit comments

Comments
 (0)