Skip to content

Commit b3f7808

Browse files
Single source of truth for installation defaults
Co-Authored-By: Nicholas Bollweg <[email protected]>
1 parent 23852ca commit b3f7808

File tree

2 files changed

+40
-29
lines changed

2 files changed

+40
-29
lines changed

packages/pyodide-kernel/py/piplite/piplite/piplite.py

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,24 @@
2727
#: a cache of available packages
2828
_PIPLITE_INDICES = {}
2929

30-
#: don't fall back to pypi.org if a package is not found in _PIPLITE_URLS
31-
_PIPLITE_DISABLE_PYPI = False
32-
3330
#: a well-known file name respected by the rest of the build chain
3431
ALL_JSON = "/all.json"
3532

36-
#: default index URLs to use when no specific index URLs are provided
37-
_PIPLITE_DEFAULT_INDEX_URLS = None
33+
#: default arguments for piplite.install
34+
_PIPLITE_DEFAULT_INSTALL_ARGS = {
35+
"requirements": None,
36+
"keep_going": False,
37+
"deps": True,
38+
"credentials": None,
39+
"pre": False,
40+
"index_urls": None,
41+
"verbose": False,
42+
}
43+
44+
# Internal flags that affect package lookup behavior
45+
_PIPLITE_INTERNAL_FLAGS = {
46+
"disable_pypi": False, # don't fall back to pypi.org if package not found in _PIPLITE_URLS
47+
}
3848

3949

4050
class PiplitePyPIDisabled(ValueError):
@@ -97,7 +107,7 @@ async def _query_package(
97107
if pypi_json_from_index:
98108
return pypi_json_from_index
99109

100-
if _PIPLITE_DISABLE_PYPI:
110+
if _PIPLITE_INTERNAL_FLAGS["disable_pypi"]:
101111
raise PiplitePyPIDisabled(
102112
f"{name} could not be installed: PyPI fallback is disabled"
103113
)
@@ -122,28 +132,30 @@ async def _install(
122132
"""Invoke micropip.install with a patch to get data from local indexes"""
123133

124134
try:
125-
# Use default index URLs if none provided and defaults exist
126-
effective_index_urls = (
127-
index_urls if index_urls is not None else _PIPLITE_DEFAULT_INDEX_URLS
128-
)
135+
install_args = _PIPLITE_DEFAULT_INSTALL_ARGS.copy()
136+
137+
provided_args = {
138+
"requirements": requirements,
139+
"keep_going": keep_going,
140+
"deps": deps,
141+
"credentials": credentials,
142+
"pre": pre,
143+
"index_urls": index_urls,
144+
"verbose": verbose,
145+
}
146+
install_args.update({k: v for k, v in provided_args.items() if v is not None})
147+
129148

130149
if verbose:
131-
logger.info(f"Installing with index URLs: {effective_index_urls}")
150+
logger.info(f"Installing with arguments: {install_args}")
132151

133152
with patch("micropip.package_index.query_package", _query_package):
134-
return await micropip.install(
135-
requirements=requirements,
136-
keep_going=keep_going,
137-
deps=deps,
138-
credentials=credentials,
139-
pre=pre,
140-
index_urls=effective_index_urls,
141-
verbose=verbose,
142-
)
153+
return await micropip.install(**install_args)
154+
143155
except Exception as e:
144-
if effective_index_urls:
156+
if install_args.get("index_urls"):
145157
logger.error(
146-
f"Failed to install using index URLs {effective_index_urls}: {e}"
158+
f"Failed to install using index URLs {install_args['index_urls']}: {e}"
147159
)
148160
raise
149161

packages/pyodide-kernel/src/worker.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,15 @@ export class PyodideRemoteKernel {
8888

8989
const pythonConfig = [
9090
'import piplite.piplite',
91-
`piplite.piplite._PIPLITE_DISABLE_PYPI = ${disablePyPIFallback ? 'True' : 'False'}`,
91+
`piplite.piplite._PIPLITE_DEFAULT_INSTALL_ARGS = ${JSON.stringify({
92+
...pipliteInstallDefaultOptions,
93+
})}`,
94+
`piplite.piplite._PIPLITE_INTERNAL_FLAGS = ${JSON.stringify({
95+
disable_pypi: disablePyPIFallback,
96+
})}`,
9297
`piplite.piplite._PIPLITE_URLS = ${JSON.stringify(pipliteUrls)}`,
9398
];
9499

95-
if (pipliteInstallDefaultOptions?.indexUrls) {
96-
pythonConfig.push(
97-
`piplite.piplite._PIPLITE_DEFAULT_INDEX_URLS = ${JSON.stringify(pipliteInstallDefaultOptions.indexUrls)}`,
98-
);
99-
}
100-
101100
// get piplite early enough to impact pyodide-kernel dependencies
102101
await this._pyodide.runPythonAsync(pythonConfig.join('\n'));
103102
}

0 commit comments

Comments
 (0)