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
improvement: poetry package list support for v2 (#7144)
Robf5eb28e5a549
diff --git a/marimo/_runtime/packages/pypi_package_manager.py b/marimo/_runtime/packages/pypi_package_manager.pyindex 2bf5a10f03..3a611105b4 100644--- a/marimo/_runtime/packages/pypi_package_manager.py+++ b/marimo/_runtime/packages/pypi_package_manager.py@@ -539,6 +539,16 @@ class PoetryPackageManager(PypiPackageManager):name = "poetry"docs_url = "https://python-poetry.org/docs/"+ def _get_poetry_version(self) -> int:+ proc = subprocess.run(+ ["poetry", "--version"], capture_output=True, text=True+ )+ if proc.returncode != 0:+ return -1 # and raise on the impl side+ version_str = proc.stdout.split()[-1].strip("()")+ major, *_ = map(int, version_str.split("."))+ return major+def install_command(self, package: str, *, upgrade: bool) -> list[str]:return ["poetry",@@ -558,6 +568,7 @@ class PoetryPackageManager(PypiPackageManager):) -> list[PackageDescription]:if not self.is_manager_installed():return []+proc = subprocess.run(cmd, capture_output=True, text=True, encoding="utf-8")@@ -580,6 +591,43 @@ class PoetryPackageManager(PypiPackageManager):)return packages+ def _generate_list_packages_cmd(self, version: int) -> list[str]:+ """Poetry 1.x and 2.x handle the "show" command differently+ In poetry 1.x, "poetry show --no-dev" works perfectly fine but is deprecated. This+ shouldn't matter if 1.8.x is still installed.+ In poetry 2.x the preferred command is "poetry show --without dev" but will throw+ an error if there are no dev packages installed. We will capture that error and+ adjust the cmd accordingly.+ """+ if version == 1:+ return ["poetry", "show", "--no-dev"]++ elif version != 2:+ LOGGER.warning(+ f"Unknown poetry version {version}, attempting fallback"+ )++ try:+ cmd = ["poetry", "show", "--without", "dev"]+ result = subprocess.run(+ cmd, capture_output=True, text=True, check=False+ )++ # If Poetry 2.x throws "Group(s) not found"+ if "Group(s) not found" in result.stderr:+ return ["poetry", "show"]++ # Otherwise, if the command succeeded+ if result.returncode == 0:+ return cmd++ except FileNotFoundError:+ return []++ # Default fallback+ return ["poetry", "show"]+def list_packages(self) -> list[PackageDescription]:- cmd = ["poetry", "show", "--no-dev"]+ version = self._get_poetry_version()+ cmd = self._generate_list_packages_cmd(version)return self._list_packages_from_cmd(cmd)diff --git a/tests/_runtime/packages/test_package_managers.py b/tests/_runtime/packages/test_package_managers.pyindex 00b29af84f..cb5cd418e2 100644--- a/tests/_runtime/packages/test_package_managers.py+++ b/tests/_runtime/packages/test_package_managers.py@@ -668,11 +668,14 @@ def test_poetry_list_packages_uses_utf8_encoding(mock_run: MagicMock):packages = mgr.list_packages()- # Verify encoding='utf-8' is passed- mock_run.assert_called_once()- call_kwargs = mock_run.call_args[1]- assert call_kwargs.get("encoding") == "utf-8"- assert call_kwargs.get("text") is True+ # Verify encoding='utf-8' is passed (PoetryPackageManager does multiple subprocess calls)+ encoding_calls = [+ kwargs+ for _, kwargs in mock_run.call_args_list+ if kwargs.get("encoding") == "utf-8"+ ]+ assert encoding_calls, "Expected at least one call with encoding='utf-8'"+ assert all(call.get("text") is True for call in encoding_calls)@pytest.mark.skipif(diff --git a/tests/_runtime/packages/test_pypi_package_manager.py b/tests/_runtime/packages/test_pypi_package_manager.pyindex 1aea16eb0e..d1ab1543fc 100644--- a/tests/_runtime/packages/test_pypi_package_manager.py+++ b/tests/_runtime/packages/test_pypi_package_manager.py@@ -10,6 +10,7 @@ from marimo._ast import compilerfrom marimo._runtime.packages.pypi_package_manager import (PackageDescription,PipPackageManager,+ PoetryPackageManager,UvPackageManager,)@@ -120,6 +121,90 @@ def test_list_packages_failure(mock_run: MagicMock):assert len(packages) == 0+# Poetry Package Manager Tests+++def test_poetry_generate_cmd_version_one():+ mgr = PoetryPackageManager()+ assert mgr._generate_list_packages_cmd(1) == [+ "poetry",+ "show",+ "--no-dev",+ ]+++@patch("subprocess.run")+def test_poetry_generate_cmd_version_two_prefers_without_dev(+ mock_run: MagicMock,+):+ mock_run.return_value = MagicMock(returncode=0, stderr="")+ mgr = PoetryPackageManager()++ cmd = mgr._generate_list_packages_cmd(2)++ assert cmd == ["poetry", "show", "--without", "dev"]+ mock_run.assert_called_once_with(+ ["poetry", "show", "--without", "dev"],+ capture_output=True,+ text=True,+ check=False,+ )+++@patch("subprocess.run")+def test_poetry_generate_cmd_version_two_falls_back_when_missing_group(+ mock_run: MagicMock,+):+ mock_run.return_value = MagicMock(+ returncode=1, stderr="Group(s) not found"+ )+ mgr = PoetryPackageManager()++ cmd = mgr._generate_list_packages_cmd(2)++ assert cmd == ["poetry", "show"]+++@patch("subprocess.run")+def test_poetry_generate_cmd_default_for_other_versions(+ mock_run: MagicMock,+):+ mock_run.return_value = MagicMock(returncode=1, stderr="")+ mgr = PoetryPackageManager()++ cmd = mgr._generate_list_packages_cmd(3)++ assert cmd == ["poetry", "show"]+++@patch("subprocess.run")+def test_poetry_list_packages_parses_output(mock_run: MagicMock):+ mock_run.side_effect = [+ MagicMock(returncode=0, stdout="Poetry (1.8.2)"),+ MagicMock(+ returncode=0,+ stdout="package-1 1.0.0\npackage-two 2.0.0\n",+ ),+ ]+ mgr = PoetryPackageManager()++ with patch.object(+ PoetryPackageManager, "is_manager_installed", return_value=True+ ):+ packages = mgr.list_packages()++ assert packages == [+ PackageDescription(name="package-1", version="1.0.0"),+ PackageDescription(name="package-two", version="2.0.0"),+ ]++ # Last subprocess call should be the list invocation with UTF-8 encoding+ cmd_args, cmd_kwargs = mock_run.call_args_list[-1]+ assert cmd_args[0] == ["poetry", "show", "--no-dev"]+ assert cmd_kwargs.get("encoding") == "utf-8"+ assert cmd_kwargs.get("text") is True++# UV Package Manager Tests