A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix(wasm): keep version specifiers when installing script dependencies (#10871)
Luke Changb720175a3b44
diff --git a/docs/guides/lint_rules/rules/incompatible_package.md b/docs/guides/lint_rules/rules/incompatible_package.mdindex b1d088063b..337faa8869 100644--- a/docs/guides/lint_rules/rules/incompatible_package.md+++ b/docs/guides/lint_rules/rules/incompatible_package.md@@ -6,12 +6,16 @@ MW003: Packages in the dependency tree incompatible with WASM.## What it does-Reads the notebook's PEP 723 `dependencies`, **filters out requirements whose-PEP 508 markers exclude Emscripten** (for example-`torch; sys_platform != 'emscripten'`), walks the transitive dependency tree-via installed metadata, then queries PyPI's JSON API to check whether each-package has a `py3-none-any`, `emscripten`, or `wasm32` wheel available.-Packages only in pyodide-lock.json are also accepted.+Reads the notebook's PEP 723 `dependencies`, filters out requirements+whose PEP 508 markers exclude Emscripten (for example+`torch; sys_platform != 'emscripten'`), walks their transitive+dependency tree via installed metadata, then queries PyPI's JSON API+to check whether each package has a `py3-none-any`, `emscripten`, or+`wasm32` wheel available. Packages only in pyodide-lock.json are also accepted.++For a package that Pyodide ships, the version in pyodide-lock.json is+the only version the browser can install. If the notebook's specifier+excludes that version, the rule reports the mismatch.## Why is this bad?@@ -19,6 +23,12 @@ Pyodide can only install pure-Python wheels via micropip, or packagesthat are pre-built in the Pyodide distribution. Packages with onlyplatform-specific native wheels will fail to install in the browser.+A specifier that excludes the Pyodide build cannot be honored there.+`marimo export html-wasm` rewrites it to the lockfile version, so the+pin in the source notebook is misleading; in the browser without that+rewrite, micropip rejects the whole install, including every other+dependency listed alongside it.+## Examples**Problematic:**@@ -26,6 +36,13 @@ platform-specific native wheels will fail to install in the browser.import jax # jaxlib (transitive dep) has only native wheels```+**Problematic:**+```python+# /// script+# dependencies = ["numpy==1.26.0"] # Pyodide ships numpy 2.4.3+# ///+```+**Not flagged:**```pythonimport numpy # Native, but pre-built in Pyodidediff --git a/frontend/src/core/wasm/__tests__/bootstrap.test.ts b/frontend/src/core/wasm/__tests__/bootstrap.test.tsindex bfac451c6d..d71ed2f349 100644--- a/frontend/src/core/wasm/__tests__/bootstrap.test.ts+++ b/frontend/src/core/wasm/__tests__/bootstrap.test.ts@@ -3,7 +3,7 @@import { loadPyodide } from "pyodide";import type { PyodideInterface } from "pyodide";import { beforeEach, describe, expect, it, vi } from "vitest";-import { DefaultWasmController } from "../worker/bootstrap";+import { DefaultWasmController, requirementName } from "../worker/bootstrap";vi.mock("pyodide", () => ({ loadPyodide: vi.fn() }));vi.mock("../worker/getMarimoWheel", () => ({@@ -61,3 +61,35 @@ describe("WASM runtime sources", () => {expect(controller.requirePyodide).toBe(pyodide);});});++describe("requirementName", () => {+ it("returns the distribution name for a bare requirement", () => {+ expect(requirementName("pandas")).toBe("pandas");+ });++ it.each([+ ["pandas==2.1.0", "pandas"],+ ["pandas>=2", "pandas"],+ ["pandas<=2", "pandas"],+ ["pandas~=2.1", "pandas"],+ ["pandas!=2.0", "pandas"],+ ["nltools===0.6.0.dev2", "nltools"],+ ])("cuts the specifier off %s", (requirement, expected) => {+ expect(requirementName(requirement)).toBe(expected);+ });++ it("cuts extras and markers", () => {+ expect(requirementName("rich[jupyter]>=13")).toBe("rich");+ expect(requirementName('cowsay==6.1; sys_platform == "emscripten"')).toBe(+ "cowsay",+ );+ });++ it("cuts a URL requirement at the name", () => {+ expect(requirementName("pkg @ https://example.com/pkg.whl")).toBe("pkg");+ });++ it("tolerates surrounding whitespace", () => {+ expect(requirementName(" pandas >= 2 ")).toBe("pandas");+ });+});diff --git a/frontend/src/core/wasm/worker/bootstrap.ts b/frontend/src/core/wasm/worker/bootstrap.tsindex 3afeb809b5..a504ebf45d 100644--- a/frontend/src/core/wasm/worker/bootstrap.ts+++ b/frontend/src/core/wasm/worker/bootstrap.ts@@ -30,6 +30,17 @@ type SessionResources = [// 3.b Install from micropip// 4. Initialize the notebook+/**+ * The distribution name of a PEP 508 requirement: "pandas>=2" -> "pandas".+ *+ * Extras, markers and URL forms are all cut at their first delimiter, which is+ * enough for the "already installed" lookups this is used for.+ */+export function requirementName(requirement: string): string {+ // Trim first: a leading space would otherwise split into an empty name.+ return requirement.trim().split(/[[<>=!~;@\s]/)[0];+}+export class DefaultWasmController implements WasmController {protected pyodide: PyodideInterface | null = null;private packageLoadQueue = Promise.resolve();@@ -314,18 +325,26 @@ export class DefaultWasmController implements WasmController {});loadSpan.end();- // Load from micropip+ // Load from micropip.+ //+ // `imports` are PEP 508 requirements and may carry a version specifier+ // ("pandas>=2", "nltools==0.6.0.dev2"). micropip wants them whole, but the+ // "do we already have this?" checks below are keyed by name, so compare on+ // the name and install the requirement.loadSpan = t.startSpan("micropip.install");const missingPackages = imports.filter(- (pkg) => !pyodide.loadedPackages[pkg],+ (pkg) => !pyodide.loadedPackages[requirementName(pkg)],);if (missingPackages.length > 0) {+ const byName = missingPackages.map(+ (pkg) => [requirementName(pkg), pkg] as const,+ );await pyodide.runPythonAsync(`import micropipimport sys# Filter out builtins- missing = [p for p in ${JSON.stringify(missingPackages)} if p not in sys.modules]+ missing = [req for name, req in ${JSON.stringify(byName)} if name not in sys.modules]if len(missing) > 0:print("Loading from micropip:", missing)await micropip.install(missing)diff --git a/marimo/_export/file.py b/marimo/_export/file.pyindex 36d82c167e..ea24fca990 100644--- a/marimo/_export/file.py+++ b/marimo/_export/file.py@@ -251,7 +251,11 @@ async def export_wasm()resolved = config.get_config()- code = app.to_py()+ # Pin Pyodide-bundled deps to the lockfile version, as the executed+ # export does: micropip installs those builds regardless of the+ # header's specifier, and rejects the whole install when the two+ # disagree.+ code = pin_pep723_dependencies_for_wasm(app.to_py(), request.path)code, options = await _prepare_wasm_export(code, request)result = Exporter().export_as_wasm(diff --git a/marimo/_lint/rules/wasm/incompatible_packages.py b/marimo/_lint/rules/wasm/incompatible_packages.pyindex ece4a966a3..c543289f02 100644--- a/marimo/_lint/rules/wasm/incompatible_packages.py+++ b/marimo/_lint/rules/wasm/incompatible_packages.py@@ -12,6 +12,8 @@ from marimo._lint.diagnostic import Diagnostic, Severityfrom marimo._lint.rules.base import LintRuleif TYPE_CHECKING:+ from packaging.requirements import Requirement+from marimo._lint.context import RuleContext@@ -20,15 +22,15 @@ def _normalize_name(name: str) -> str:return re.sub(r"[-_.]+", "-", name).lower()-def _get_pyodide_packages() -> set[str] | None:- """Fetch the set of normalized package names available in Pyodide."""+def _get_pyodide_versions() -> dict[str, str] | None:+ """Fetch {normalized package name: version} for packages Pyodide ships."""try:from marimo._pyodide.pyodide_constraints import (fetch_pyodide_package_versions,)versions = fetch_pyodide_package_versions()- return {_normalize_name(name) for name in versions}+ return {_normalize_name(name): v for name, v in versions.items()}except Exception:return None@@ -70,8 +72,11 @@ def _has_wasm_compatible_wheel(package_name: str) -> bool:return False-def _get_notebook_deps(ctx: RuleContext) -> set[str] | None:- """Extract PEP 723 dependencies from the notebook's script metadata."""+_DEP_NAME_RE = re.compile(r"^([A-Za-z0-9]([A-Za-z0-9._-]*[A-Za-z0-9])?)")+++def _get_notebook_deps(ctx: RuleContext) -> list[str] | None:+ """PEP 723 dependencies that apply on Emscripten, as written."""if not ctx.notebook.filename:return None@@ -83,20 +88,39 @@ def _get_notebook_deps(ctx: RuleContext) -> set[str] | None:reader = PyProjectReader.from_filename(ctx.notebook.filename)deps = filter_requirements_for_emscripten(reader.dependencies)- if not deps:- return None- names: set[str] = set()- for dep in deps:- match = re.match(- r"^([A-Za-z0-9]([A-Za-z0-9._-]*[A-Za-z0-9])?)", dep- )- if match:- names.add(_normalize_name(match.group(1)))- return names+ return deps or Noneexcept Exception:return None+def _parse_requirement(dep: str) -> Requirement | None:+ from packaging.requirements import InvalidRequirement, Requirement++ try:+ return Requirement(dep)+ except InvalidRequirement:+ return None+++def _dep_name(dep: str) -> str | None:+ """Normalized distribution name of a dependency string, if parseable."""+ req = _parse_requirement(dep)+ if req is not None:+ return _normalize_name(req.name)+ match = _DEP_NAME_RE.match(dep)+ return _normalize_name(match.group(1)) if match else None+++def _lock_satisfies(req: Requirement, lock_version: str) -> bool:+ """Whether the Pyodide build satisfies the requirement's specifier."""+ from packaging.version import InvalidVersion++ try:+ return req.specifier.contains(lock_version, prereleases=True)+ except InvalidVersion:+ return True++def _resolve_dep_tree(root_deps: set[str]) -> set[str]:"""Walk installed metadata to resolve the transitive dependency tree."""installed: dict[str, importlib.metadata.Distribution] = {}@@ -121,9 +145,7 @@ def _resolve_dep_tree(root_deps: set[str]) -> set[str]:for req in requires:if "extra ==" in req:continue- match = re.match(- r"^([A-Za-z0-9]([A-Za-z0-9._-]*[A-Za-z0-9])?)", req- )+ match = _DEP_NAME_RE.match(req)if match:dep_name = _normalize_name(match.group(1))if dep_name not in resolved:@@ -138,14 +160,22 @@ class IncompatiblePackagesRule(LintRule):This rule resolves the notebook's PEP 723 dependency tree and checkseach package against PyPI for WASM-compatible wheels. Catchestransitive dependencies like `jaxlib` (pulled in by `jax`) that- have no pure-python or emscripten wheel on PyPI.+ have no pure-python or emscripten wheel on PyPI. It also checks+ version specifiers on packages Pyodide ships against the build in+ the Pyodide lockfile.## What it does- Reads the notebook's PEP 723 `dependencies`, walks their transitive+ Reads the notebook's PEP 723 `dependencies`, filters out requirements+ whose PEP 508 markers exclude Emscripten (for example+ `torch; sys_platform != 'emscripten'`), walks their transitivedependency tree via installed metadata, then queries PyPI's JSON API- to check whether each package has a `py3-none-any` or emscripten- wheel available. Packages only in pyodide-lock.json are also accepted.+ to check whether each package has a `py3-none-any`, `emscripten`, or+ `wasm32` wheel available. Packages only in pyodide-lock.json are also accepted.++ For a package that Pyodide ships, the version in pyodide-lock.json is+ the only version the browser can install. If the notebook's specifier+ excludes that version, the rule reports the mismatch.## Why is this bad?@@ -153,6 +183,12 @@ class IncompatiblePackagesRule(LintRule):that are pre-built in the Pyodide distribution. Packages with onlyplatform-specific native wheels will fail to install in the browser.+ A specifier that excludes the Pyodide build cannot be honored there.+ `marimo export html-wasm` rewrites it to the lockfile version, so the+ pin in the source notebook is misleading; in the browser without that+ rewrite, micropip rejects the whole install, including every other+ dependency listed alongside it.+## Examples**Problematic:**@@ -160,6 +196,13 @@ class IncompatiblePackagesRule(LintRule):import jax # jaxlib (transitive dep) has only native wheels```+ **Problematic:**+ ```python+ # /// script+ # dependencies = ["numpy==1.26.0"] # Pyodide ships numpy 2.4.3+ # ///+ ```+**Not flagged:**```pythonimport numpy # Native, but pre-built in Pyodide@@ -170,9 +213,19 @@ class IncompatiblePackagesRule(LintRule):import requests # Pure Python wheel on PyPI```+ **Not flagged (PEP 723 metadata):**+ ```python+ # /// script+ # dependencies = ["jax; sys_platform != 'emscripten'"]+ # ///+ # jax is excluded from WASM checks via PEP 508 marker+ ```+## References- https://pyodide.org/en/stable/usage/packages-in-pyodide.html+ - https://peps.python.org/pep-0508/ (environment markers)+ - https://peps.python.org/pep-0783/ (Emscripten wheels on PyPI)"""code = "MW003"@@ -182,15 +235,47 @@ class IncompatiblePackagesRule(LintRule):fixable = Falseasync def check(self, ctx: RuleContext) -> None:- pyodide_packages = _get_pyodide_packages()- if pyodide_packages is None:+ pyodide_versions = _get_pyodide_versions()+ if pyodide_versions is None:return- notebook_deps = _get_notebook_deps(ctx)- if notebook_deps is None:+ deps = _get_notebook_deps(ctx)+ if deps is None:return- notebook_deps.discard("marimo")+ notebook_deps: set[str] = set()+ for dep in deps:+ name = _dep_name(dep)+ if name is None or name == "marimo":+ continue+ notebook_deps.add(name)++ lock_version = pyodide_versions.get(name)+ req = _parse_requirement(dep)+ if (+ lock_version is None+ or req is None+ or not req.specifier+ or _lock_satisfies(req, lock_version)+ ):+ continue+ await ctx.add_diagnostic(+ Diagnostic(+ message=(+ f"{req.name}{req.specifier} cannot be satisfied in "+ f"WASM: Pyodide ships {req.name} {lock_version}, "+ "and `marimo export html-wasm` pins it to that "+ "version."+ ),+ line=1,+ column=0,+ fix=(+ f"Change the specifier to {req.name}=={lock_version} "+ "or remove it."+ ),+ )+ )+if not notebook_deps:return@@ -199,7 +284,7 @@ class IncompatiblePackagesRule(LintRule):incompatible: list[str] = []for pkg in sorted(dep_tree):# In Pyodide's lockfile — definitely available.- if pkg in pyodide_packages:+ if pkg in pyodide_versions:continue# Check PyPI for a WASM-compatible wheel.diff --git a/marimo/_pyodide/pyodide_session.py b/marimo/_pyodide/pyodide_session.pyindex 813735bad4..edbc404d7e 100644--- a/marimo/_pyodide/pyodide_session.py+++ b/marimo/_pyodide/pyodide_session.py@@ -190,7 +190,6 @@ class PyodideSession:try:from marimo._runtime.packages.utils import (filter_requirements_for_emscripten,- strip_requirement_name,)reader = PyProjectReader.from_script(code)@@ -199,7 +198,14 @@ class PyodideSession:)if len(script_deps) > 0:- return [strip_requirement_name(dep) for dep in script_deps]+ # Returned as written, version specifiers included. These are+ # handed to micropip, which resolves them; dropping the+ # specifier does not merely lose precision, it can select a+ # different release. A pin is the only thing that selects a+ # pre-release, so an unpinned name resolves to the newest+ # *stable* version instead -- the opposite of what the notebook+ # asked for.+ return [dep.strip() for dep in script_deps]except Exception as e:LOGGER.warning("Error parsing script metadata: %s", e)diff --git a/marimo/_utils/inline_script_metadata.py b/marimo/_utils/inline_script_metadata.pyindex 2848c9960d..2d94078a30 100644--- a/marimo/_utils/inline_script_metadata.py+++ b/marimo/_utils/inline_script_metadata.py@@ -309,29 +309,46 @@ def with_pinned_dependencies(def pin_pep723_dependencies_for_wasm(code: str, path: MarimoPath) -> str:"""Pin a notebook's PEP 723 deps for embedding in a WASM HTML export.- For each top-level dep also shipped in the Pyodide lockfile, pin to the- *lockfile* version — that's what micropip will install in the browser,- so embedding any other pin would be a lie that breaks the export. If- the lockfile fetch fails, degrade to pinning the locally installed- version (best-effort; the WASM runtime falls back to its bundled- lockfile). Also warns about top-level dependencies that aren't shipped- in pyodide-lock — those may fail to install via micropip. The lock- kind is "resolved" when we ran inside the html-wasm sandbox, otherwise- "observed".+ Every top-level dependency that Pyodide also ships is pinned to the+ *lockfile* version. That build is what micropip installs in the browser+ whatever the header asks for, and a header that asks for a different+ version makes micropip reject the whole install. Dependencies Pyodide+ does not ship keep their specifier as written; micropip resolves those+ from PyPI. Pins come from the header alone, so the exporting machine's+ environment does not matter. If the lockfile fetch fails, degrade to+ pinning the locally installed version (best-effort; the WASM runtime+ falls back to its bundled lockfile). Also warns about top-level+ dependencies that aren't shipped in pyodide-lock -- those may fail to+ install via micropip. The lock kind is "resolved" when we ran inside+ the html-wasm sandbox, otherwise "observed"."""- from importlib.metadata import distributions-from marimo._pyodide.pyodide_constraints import (fetch_pyodide_package_versions,normalize_package_name,)- installed: dict[str, str] = {}- for dist in distributions():- name = dist.metadata["Name"]- version = dist.version- if name and version:- installed[normalize_package_name(name)] = version+ # Top-level deps that apply in the browser, keyed by canonical name.+ # Deps whose markers exclude Emscripten are never installed there, so+ # they are left exactly as written.+ header_names: dict[str, str] = {}+ try:+ from marimo._runtime.packages.utils import (+ filter_requirements_for_emscripten,+ )++ pyproject = PyProjectReader.from_filename(path.absolute_name)+ for dep in filter_requirements_for_emscripten(pyproject.dependencies):+ match = _DEP_NAME_RE.match(dep.strip())+ if match is None:+ continue+ bare_name = match.group("name").split("[", 1)[0]+ canonical = normalize_package_name(bare_name)+ # marimo itself is installed by the runtime, not from the+ # header; its version there has nothing to do with the lock.+ if canonical != "marimo":+ header_names[canonical] = bare_name+ except Exception as e:+ LOGGER.debug("Could not read PEP 723 dependencies: %s", e)pyodide_versions: dict[str, str] = {}try:@@ -339,53 +356,44 @@ def pin_pep723_dependencies_for_wasm(code: str, path: MarimoPath) -> str:normalize_package_name(n): vfor n, v in fetch_pyodide_package_versions().items()}- # Pin to lockfile versions for names the browser can actually- # install. Restricting to installed names keeps the pin set small- # and noise-free (an irrelevant pin for an unused dep is harmless- # but unnecessary churn).pins = {name: pyodide_versions[name]- for name in installed+ for name in header_namesif name in pyodide_versions}except Exception:# Fetch failures degrade to pinning whatever is installed; the# WASM micropip will still try its bundled lockfile first.- pins = installed- pyodide_names = set(pyodide_versions)+ from importlib.metadata import distributions++ installed: dict[str, str] = {}+ for dist in distributions():+ name = dist.metadata["Name"]+ version = dist.version+ if name and version:+ installed[normalize_package_name(name)] = version+ pins = {+ name: installed[name] for name in header_names if name in installed+ }# Warn about top-level deps not bundled in pyodide. micropip *may*# still install pure-python ones from PyPI in the browser; native# ones (jax, torch, numpy alternatives) will fail. We don't know# which from here, so emit a single advisory.- if pyodide_names:- try:- from marimo._runtime.packages.utils import (- filter_requirements_for_emscripten,+ if pyodide_versions:+ unbundled = sorted(+ bare_name+ for canonical, bare_name in header_names.items()+ if canonical not in pyodide_versions+ )+ if unbundled:+ echo(+ "warn: these dependencies are not bundled in the "+ "Pyodide lockfile and may fail to install in the "+ "browser: " + ", ".join(unbundled),+ err=True,)- pyproject = PyProjectReader.from_filename(path.absolute_name)- top_level: list[str] = []- for dep in filter_requirements_for_emscripten(- pyproject.dependencies- ):- match = re.match(r"^([A-Za-z0-9][A-Za-z0-9._-]*)", dep.strip())- if match is None:- continue- canonical = normalize_package_name(match.group(1))- if canonical == "marimo" or canonical in pyodide_names:- continue- top_level.append(match.group(1))- if top_level:- echo(- "warn: these dependencies are not bundled in the "- "Pyodide lockfile and may fail to install in the "- "browser: " + ", ".join(sorted(set(top_level))),- err=True,- )- except Exception as e:- LOGGER.debug("Skipped wasm compat warn: %s", e)-lock_kind = ("resolved"if os.environ.get("MARIMO_HTML_WASM_SANDBOX_BOOTSTRAPPED") == "1"diff --git a/tests/_export/test_exporter.py b/tests/_export/test_exporter.pyindex 7f818d273a..cdfa16263d 100644--- a/tests/_export/test_exporter.py+++ b/tests/_export/test_exporter.py@@ -2886,3 +2886,71 @@ def test_export_assets_preserves_write_permission(assert (dest / "assets").stat().st_mode & stat.S_IWUSR, ("export_assets made the assets subdirectory read-only")+++async def test_export_wasm_unexecuted_pins_header_to_pyodide_lock(+ tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ """The default (unexecuted) export rewrites bundled pins to the lock.++ Specifiers on packages Pyodide does not ship are preserved.+ """+ lockfile = tmp_path / "pyodide-lock.json"+ lockfile.write_text(+ json.dumps(+ {+ "packages": {+ "numpy": {+ "name": "numpy",+ "version": "2.4.3",+ "package_type": "package",+ }+ }+ }+ )+ )+ monkeypatch.setenv("MARIMO_PYODIDE_LOCK_FILE", str(lockfile))+ monkeypatch.delenv("MARIMO_HTML_WASM_SANDBOX_BOOTSTRAPPED", raising=False)++ notebook = tmp_path / "notebook.py"+ notebook.write_text(+ textwrap.dedent(+ """+ # /// script+ # dependencies = [+ # "numpy==1.26.0",+ # "emoji",+ # "nltools==0.6.0.dev2",+ # ]+ # ///++ import marimo++ __generated_with = "0.0.0"+ app = marimo.App()+++ @app.cell+ def _():+ import emoji+ return+++ if __name__ == "__main__":+ app.run()+ """+ ).lstrip()+ )++ result = await export_wasm(+ WASMFileExportRequest(+ path=MarimoPath(notebook),+ options=WASMExportOptions(mode="run", show_code=True),+ )+ )++ assert result.did_error is False+ assert "numpy==2.4.3" in result.text+ assert "numpy==1.26.0" not in result.text+ assert "nltools==0.6.0.dev2" in result.text+ assert 'lock_kind = \\"observed\\"' in result.textdiff --git a/tests/_lint/test_wasm_rules.py b/tests/_lint/test_wasm_rules.pyindex 9394c097bf..bd140e5dac 100644--- a/tests/_lint/test_wasm_rules.py+++ b/tests/_lint/test_wasm_rules.py@@ -1057,6 +1057,91 @@ class TestMW003IncompatiblePackages:)assert not any(d.code == "MW003" for d in diagnostics)+ def test_bundled_pin_mismatch_flagged(self, monkeypatch, tmp_path):+ """A specifier the Pyodide build cannot satisfy names both versions."""+ from marimo._lint.rules.wasm import incompatible_packages as mod++ monkeypatch.setattr(mod, "_resolve_dep_tree", lambda deps: deps)+ mod._has_wasm_compatible_wheel.cache_clear()++ notebook, contents = self._write_and_parse(+ tmp_path, ["numpy==1.26.0", "pandas"]+ )+ with (+ patch(+ "marimo._pyodide.pyodide_constraints.requests.get",+ return_value=_fake_lockfile_response(+ {"numpy": "2.4.3", "pandas": "3.0.2"}+ ),+ ),+ patch(+ "marimo._lint.rules.wasm.incompatible_packages."+ "urllib.request.urlopen",+ side_effect=AssertionError(+ "PyPI must not be queried for lockfile-listed packages"+ ),+ ),+ ):+ diagnostics = lint_notebook(+ notebook, contents, lint_config={"select": ["MW003"]}+ )+ mw003 = [d for d in diagnostics if d.code == "MW003"]+ assert len(mw003) == 1+ assert "numpy==1.26.0" in mw003[0].message+ assert "2.4.3" in mw003[0].message+ assert "numpy==2.4.3" in (mw003[0].fix or "")+ assert mw003[0].severity == Severity.WASM++ def test_bundled_pin_satisfied_not_flagged(self, monkeypatch, tmp_path):+ """Specifiers the Pyodide build satisfies are left alone."""+ from marimo._lint.rules.wasm import incompatible_packages as mod++ monkeypatch.setattr(mod, "_resolve_dep_tree", lambda deps: deps)+ mod._has_wasm_compatible_wheel.cache_clear()++ notebook, contents = self._write_and_parse(+ tmp_path, ["pandas>=2.0", "numpy==2.4.3"]+ )+ with patch(+ "marimo._pyodide.pyodide_constraints.requests.get",+ return_value=_fake_lockfile_response(+ {"numpy": "2.4.3", "pandas": "3.0.2"}+ ),+ ):+ diagnostics = lint_notebook(+ notebook, contents, lint_config={"select": ["MW003"]}+ )+ assert not any(d.code == "MW003" for d in diagnostics)++ def test_unbundled_pin_not_version_checked(self, monkeypatch, tmp_path):+ """micropip resolves non-Pyodide packages from PyPI; any pin is fine."""+ from marimo._lint.rules.wasm import incompatible_packages as mod++ monkeypatch.setattr(mod, "_resolve_dep_tree", lambda deps: deps)+ mod._has_wasm_compatible_wheel.cache_clear()++ notebook, contents = self._write_and_parse(+ tmp_path, ["nltools==0.6.0.dev2"]+ )+ pypi_payload = {+ "urls": [{"filename": "nltools-0.6.0.dev2-py3-none-any.whl"}]+ }+ with (+ patch(+ "marimo._pyodide.pyodide_constraints.requests.get",+ return_value=_fake_lockfile_response({"numpy": "2.4.3"}),+ ),+ patch(+ "marimo._lint.rules.wasm.incompatible_packages."+ "urllib.request.urlopen",+ return_value=_FakePypiResponse(pypi_payload),+ ),+ ):+ diagnostics = lint_notebook(+ notebook, contents, lint_config={"select": ["MW003"]}+ )+ assert not any(d.code == "MW003" for d in diagnostics)+def test_pyemscripten_wheel_tag_compatible(self) -> None:"""PEP 783 pyemscripten_*_wasm32 wheels match via the wasm32 suffix."""from marimo._lint.rules.wasm import incompatible_packages as moddiff --git a/tests/_pyodide/test_pyodide_session.py b/tests/_pyodide/test_pyodide_session.pyindex 0b7a3838c5..370a121cca 100644--- a/tests/_pyodide/test_pyodide_session.py+++ b/tests/_pyodide/test_pyodide_session.py@@ -410,13 +410,39 @@ async def test_pyodide_session_find_packages_with_script_metadata()packages = pyodide_session.find_packages(code)- assert sorted(packages) == sorted(["foo", "bar", "baz"])+ # Specifiers are preserved: these go to micropip, which resolves them.+ assert sorted(packages) == sorted(["foo", "bar==1.0.0", "baz>=2.0.0"])mock_pyodide.code.find_imports.assert_not_called()+async def test_find_packages_keeps_a_pinned_prerelease(+ pyodide_session: PyodideSession,+) -> None:+ """A pin is the only thing that selects a pre-release.++ Dropping it does not just lose precision -- an unpinned name resolves to+ the newest *stable* release, so the notebook silently gets a different+ version than it asked for, and one that may not run in Pyodide at all.+ """+ code = dedent(+ """+ # /// script+ # dependencies = ["somepkg==1.0.0.dev2"]+ # ///++ import somepkg+ """+ )++ assert pyodide_session.find_packages(code) == ["somepkg==1.0.0.dev2"]++@pytest.mark.parametrize(("python_version", "expected_package"),- [("3.12", "older-python"), ("3.13", "newer-python")],+ [+ ("3.12", "older-python; python_version < '3.13'"),+ ("3.13", "newer-python; python_version >= '3.13'"),+ ],)async def test_find_packages_from_island_notebook(pyodide_session: PyodideSession,@@ -441,8 +467,8 @@ async def test_find_packages_from_island_notebook("packaging.markers.default_environment", return_value=environment):assert pyodide_session.find_packages(code) == [- "cowsay",- "rich[jupyter]",+ 'cowsay==6.1; sys_platform == "emscripten"',+ "rich[jupyter]>=13",expected_package,]mock_pyodide.code.find_imports.assert_not_called()@@ -471,36 +497,47 @@ async def test_complex_find_packages(pyodide_session: PyodideSession) -> None:""")packages = pyodide_session.find_packages(code)- assert sorted(packages) == sorted(["plotly[express]", "polars"])+ assert sorted(packages) == sorted(+ ["plotly[express]==6.5.0", "polars==1.36.1"]+ )async def test_strip_version_resilience(pyodide_session: PyodideSession,) -> None:- """Test strip_version function handles various PEP 440 version specifiers and edge cases."""+ """find_packages keeps each requirement as written.++ Specifiers reach micropip, which resolves them. Environment markers are+ still applied (a non-emscripten requirement drops out), and URL forms are+ unchanged. `strip_requirement_name` keeps its own tests in+ tests/_runtime/packages/test_package_utils.py.+ """code_templates = [# Basic version specifiers- ('dependencies = ["package==1.0.0"]', ["package"]),- ('dependencies = ["package>=1.0.0"]', ["package"]),- ('dependencies = ["package<=2.0.0"]', ["package"]),- ('dependencies = ["package~=1.4"]', ["package"]),- ('dependencies = ["package!=1.5.0"]', ["package"]),- ('dependencies = ["package>1.0"]', ["package"]),- ('dependencies = ["package<2.0"]', ["package"]),- ('dependencies = ["package===1.0.0"]', ["package"]),+ ('dependencies = ["package==1.0.0"]', ["package==1.0.0"]),+ ('dependencies = ["package>=1.0.0"]', ["package>=1.0.0"]),+ ('dependencies = ["package<=2.0.0"]', ["package<=2.0.0"]),+ ('dependencies = ["package~=1.4"]', ["package~=1.4"]),+ ('dependencies = ["package!=1.5.0"]', ["package!=1.5.0"]),+ ('dependencies = ["package>1.0"]', ["package>1.0"]),+ ('dependencies = ["package<2.0"]', ["package<2.0"]),+ ('dependencies = ["package===1.0.0"]', ["package===1.0.0"]),# With extras- ('dependencies = ["package[extra]==1.0.0"]', ["package[extra]"]),+ (+ 'dependencies = ["package[extra]==1.0.0"]',+ ["package[extra]==1.0.0"],+ ),('dependencies = ["package[extra1,extra2]>=1.0.0"]',- ["package[extra1,extra2]"],+ ["package[extra1,extra2]>=1.0.0"],),# With whitespace- ('dependencies = ["package >= 1.0.0"]', ["package"]),- ('dependencies = [" package==1.0.0 "]', ["package"]),+ ('dependencies = ["package >= 1.0.0"]', ["package >= 1.0.0"]),+ ('dependencies = [" package==1.0.0 "]', ["package==1.0.0"]),# With environment markers('dependencies = ["package>=1.0.0; python_version>=\\"3.8\\""]',- ["package"],+ ['package>=1.0.0; python_version>="3.8"'],),('dependencies = ["package[extra]>=1.0; sys_platform==\\"linux\\""]',@@ -508,11 +545,11 @@ async def test_strip_version_resilience(),('dependencies = ["native>=1.0; sys_platform!=\\"emscripten\\"", "pure-python>=1.0"]',- ["pure-python"],+ ["pure-python>=1.0"],),('dependencies = ["wasm-only; sys_platform==\\"emscripten\\"", "linux-only; sys_platform==\\"linux\\""]',- ["wasm-only"],+ ['wasm-only; sys_platform=="emscripten"'],),# URL dependencies - left as-is(@@ -534,7 +571,7 @@ async def test_strip_version_resilience(# Multiple packages with various specifiers('dependencies = ["foo==1.0", "bar>=2.0", "baz~=3.0", "qux[extra]>=4.0"]',- ["foo", "bar", "baz", "qux[extra]"],+ ["foo==1.0", "bar>=2.0", "baz~=3.0", "qux[extra]>=4.0"],),# No version specifier('dependencies = ["package"]', ["package"]),diff --git a/tests/_utils/test_inline_script_metadata.py b/tests/_utils/test_inline_script_metadata.pyindex a3611312f0..06e80f7269 100644--- a/tests/_utils/test_inline_script_metadata.py+++ b/tests/_utils/test_inline_script_metadata.py@@ -763,3 +763,72 @@ def test_markdown_manifest_accepts_comments_and_wrapped_metadata(header = get_headers_from_frontmatter({"pyproject": contents})["pyproject"]assert script_metadata.loads(header) == {"dependencies": ["numpy"]}+++_C6_SRC = """# /// script+# requires-python = ">=3.12"+# dependencies = [+# "numpy==1.26.0",+# "emoji",+# "nltools==0.6.0.dev2",+# "pandas>=2.0; sys_platform != 'emscripten'",+# ]+# ///+"""+++def _write_lockfile(tmp_path: Path, packages: dict[str, str]) -> Path:+ import json++ lockfile = tmp_path / "pyodide-lock.json"+ lockfile.write_text(+ json.dumps(+ {+ "packages": {+ name: {+ "name": name,+ "version": version,+ "package_type": "package",+ }+ for name, version in packages.items()+ }+ }+ )+ )+ return lockfile+++def test_pin_for_wasm_pins_from_header_not_host_env(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ """A bundled package is pinned to the lock even when the host lacks it.++ Non-bundled specifiers and Emscripten-excluded deps stay as written.+ """+ import importlib.metadata++ from marimo._utils.inline_script_metadata import (+ pin_pep723_dependencies_for_wasm,+ )+ from marimo._utils.marimo_path import MarimoPath++ monkeypatch.delenv("MARIMO_HTML_WASM_SANDBOX_BOOTSTRAPPED", raising=False)+ # Nothing installed on the exporting machine.+ monkeypatch.setattr(importlib.metadata, "distributions", list)+ monkeypatch.setenv(+ "MARIMO_PYODIDE_LOCK_FILE",+ str(_write_lockfile(tmp_path, {"numpy": "2.4.3", "pandas": "3.0.2"})),+ )+ script = tmp_path / "notebook.py"+ script.write_text(_C6_SRC)++ out = pin_pep723_dependencies_for_wasm(_C6_SRC, MarimoPath(script))++ assert "numpy==2.4.3" in out+ assert "numpy==1.26.0" not in out+ assert '"emoji"' in out+ assert "nltools==0.6.0.dev2" in out+ # Not installed in the browser, so its specifier is not the lock's+ # business.+ assert "pandas>=2.0; sys_platform != 'emscripten'" in out+ assert "pandas==" not in out