Skip to content

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.

improvement: support custom cloudpathlib providers in path normalization (#8929)

Myles Scolnicked0ec3b52059
diff --git a/marimo/_dependencies/dependencies.py b/marimo/_dependencies/dependencies.py
index 273c2a4134..169733de5a 100644
--- a/marimo/_dependencies/dependencies.py
+++ b/marimo/_dependencies/dependencies.py
@@ -259,6 +259,7 @@ class DependencyManager:
# Storage
obstore = Dependency("obstore")
fsspec = Dependency("fsspec")
+ cloudpathlib = Dependency("cloudpathlib")
# Version requirements to properly support the new superfences introduced in
# pymdown#2470
diff --git a/marimo/_plugins/ui/_impl/file_browser.py b/marimo/_plugins/ui/_impl/file_browser.py
index e99c7bab92..b7ff780a63 100644
--- a/marimo/_plugins/ui/_impl/file_browser.py
+++ b/marimo/_plugins/ui/_impl/file_browser.py
@@ -20,7 +20,7 @@ from marimo._plugins.ui._core.ui_element import UIElement
from marimo._plugins.validators import validate_one_of
from marimo._runtime.functions import Function
from marimo._utils.files import natural_sort
-from marimo._utils.paths import normalize_path
+from marimo._utils.paths import is_cloudpath, normalize_path
LOGGER = _loggers.marimo_logger()
@@ -198,8 +198,7 @@ class file_browser(
# Smart default limit based on path type
if limit is None:
- # Check if it's a cloud path
- if self._path_cls.__module__.startswith("cloudpathlib"):
+ if is_cloudpath(self._initial_path):
limit = 50 # Conservative for cloud storage
else:
limit = 10000 # High limit for local filesystems
diff --git a/marimo/_utils/paths.py b/marimo/_utils/paths.py
index fd87c1fa49..355cc5319a 100644
--- a/marimo/_utils/paths.py
+++ b/marimo/_utils/paths.py
@@ -95,6 +95,30 @@ def notebook_output_dir(notebook_path: Path | str | None) -> Path:
return parent / MARIMO_DIR_NAME
+def is_cloudpath(path: Path) -> bool:
+ """Check if a path is a cloudpathlib CloudPath (including subclasses).
+
+ Uses a module-name heuristic first, then falls back to isinstance
+ when cloudpathlib is already imported (to catch virtual subclasses
+ registered via CloudPath.register()).
+ """
+ # Quick module-name heuristic — works without importing cloudpathlib.
+ if path.__class__.__module__.startswith("cloudpathlib"):
+ return True
+
+ # If cloudpathlib hasn't been imported yet there's no way a virtual
+ # subclass (CloudPath.register()) could exist, so skip the import.
+ if "cloudpathlib" not in sys.modules:
+ return False
+
+ try:
+ from cloudpathlib import CloudPath # type: ignore[import-not-found]
+
+ return isinstance(path, CloudPath)
+ except ImportError:
+ return False
+
+
def normalize_path(path: Path) -> Path:
"""Normalize a path without resolving symlinks.
@@ -117,7 +141,7 @@ def normalize_path(path: Path) -> Path:
"""
# Skip normalization for cloud paths (e.g., S3Path, GCSPath, AzurePath)
# os.path.normpath corrupts URI schemes like s3:// by reducing them to s3:/
- if path.__class__.__module__.startswith("cloudpathlib"):
+ if is_cloudpath(path):
return path
# Make absolute if relative (relative to current working directory)
diff --git a/tests/_utils/test_paths.py b/tests/_utils/test_paths.py
index 4eab768693..3d7e1fe547 100644
--- a/tests/_utils/test_paths.py
+++ b/tests/_utils/test_paths.py
@@ -8,6 +8,7 @@ from pathlib import Path
import pytest
from marimo._utils.paths import (
+ is_cloudpath,
normalize_path,
notebook_output_dir,
pretty_path,
@@ -86,35 +87,102 @@ def test_normalize_path_does_not_resolve_symlinks() -> None:
assert normalized != resolved
+class TestIsCloudpath:
+ """Tests for is_cloudpath utility."""
+
+ def test_regular_path_is_not_cloudpath(self) -> None:
+ assert not is_cloudpath(Path("/tmp/foo"))
+
+ def test_builtin_cloudpath_detected(self) -> None:
+ """Built-in cloudpathlib paths are detected as cloud paths."""
+ cloudpathlib = pytest.importorskip("cloudpathlib")
+
+ assert is_cloudpath(cloudpathlib.S3Path("s3://bucket/key"))
+
+ def test_custom_cloudpath_subclass_detected(self) -> None:
+ """Custom CloudPath subclasses from external packages are detected.
+
+ This is the core scenario from issue #8868: a user creates a
+ custom provider whose __module__ does NOT start with 'cloudpathlib'.
+ We use virtual subclass registration to avoid cloudpathlib's
+ metaclass issues with direct subclassing.
+ """
+ cloudpathlib = pytest.importorskip("cloudpathlib")
+ CloudPath = cloudpathlib.CloudPath
+
+ class FakeSMBPath:
+ def __init__(self, s: str) -> None:
+ self._s = s
+
+ def __str__(self) -> str:
+ return self._s
+
+ CloudPath.register(FakeSMBPath)
+
+ path = FakeSMBPath("smb://server/share")
+ assert not FakeSMBPath.__module__.startswith("cloudpathlib")
+ assert is_cloudpath(path) # type: ignore[arg-type]
+
+ def test_fallback_module_check_when_cloudpathlib_missing(self) -> None:
+ """When cloudpathlib can't be imported, fall back to module name."""
+ from unittest.mock import MagicMock, patch
+
+ mock_path = MagicMock()
+ mock_path.__class__ = type(
+ "S3Path", (), {"__module__": "cloudpathlib.s3.s3path"}
+ )
+
+ with patch.dict("sys.modules", {"cloudpathlib": None}):
+ assert is_cloudpath(mock_path)
+
+ def test_non_cloud_mock_rejected(self) -> None:
+ from unittest.mock import MagicMock
+
+ mock_path = MagicMock()
+ mock_path.__class__ = type(
+ "MyPath", (), {"__module__": "mypackage.paths"}
+ )
+ assert not is_cloudpath(mock_path)
+
+
def test_normalize_path_skips_cloudpathlib_paths() -> None:
"""Test that cloud paths from cloudpathlib are returned unchanged.
os.path.normpath corrupts URI schemes like s3:// by reducing them to s3:/
- This test verifies that cloudpathlib paths bypass normalization.
"""
- from unittest.mock import MagicMock
+ cloudpathlib = pytest.importorskip("cloudpathlib")
+
+ cloud_path = cloudpathlib.S3Path("s3://bucket/folder/file.txt")
+ result = normalize_path(cloud_path)
+ assert result is cloud_path
+
+
+def test_normalize_path_skips_custom_cloudpath_subclass() -> None:
+ """Custom CloudPath subclasses should also skip normalization (#8868)."""
+ cloudpathlib = pytest.importorskip("cloudpathlib")
+ CloudPath = cloudpathlib.CloudPath
+
+ class FakeSMBPath:
+ def __init__(self, s: str) -> None:
+ self._s = s
- # Create a mock cloud path that simulates cloudpathlib.S3Path
- # We can't inherit from PurePosixPath because it normalizes on construction
- mock_path = MagicMock()
- mock_path.__class__.__module__ = "cloudpathlib.s3.s3path"
+ def __str__(self) -> str:
+ return self._s
- # normalize_path should return the path unchanged
- result = normalize_path(mock_path)
+ CloudPath.register(FakeSMBPath)
- # Should be the exact same object, returned unchanged
- assert result is mock_path
+ path = FakeSMBPath("smb://server/share/folder")
+ result = normalize_path(path) # type: ignore[arg-type]
+ assert result is path
+ assert "smb://" in str(result)
def test_normalize_path_does_not_skip_regular_paths() -> None:
"""Test that regular Path objects are still normalized properly."""
- # Ensure the cloudpathlib check doesn't affect regular paths
relative_path = Path("foo") / "bar"
result = normalize_path(relative_path)
- # Should be converted to absolute
assert result.is_absolute()
- # Module should be pathlib, not cloudpathlib
assert result.__class__.__module__.startswith("pathlib")