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
lint: allow ASYNC240 (#6703)
Dylan Madisetti4ea8cf38d7a7
diff --git a/marimo/_lint/linter.py b/marimo/_lint/linter.pyindex 524ebcf76a..eabc5ae948 100644--- a/marimo/_lint/linter.py+++ b/marimo/_lint/linter.py@@ -17,6 +17,7 @@ from marimo._lint.formatters import LintResultJSONfrom marimo._lint.rule_engine import EarlyStoppingConfig, RuleEnginefrom marimo._loggers import capture_outputfrom marimo._schemas.serialization import NotebookSerialization+from marimo._utils import async_pathif TYPE_CHECKING:from collections.abc import AsyncIterator, Callable, Iterator@@ -114,7 +115,7 @@ class Linter:file_path = str(file)file_status = FileStatus(file=file_path)# Check if file exists first- if not file.exists():+ if not await async_path.exists(file):self.errored = Truefile_status.failed = Truefile_status.message = f"File not found: {file_path}"diff --git a/marimo/_server/export/exporter.py b/marimo/_server/export/exporter.pyindex 5c3623505f..57808baa09 100644--- a/marimo/_server/export/exporter.py+++ b/marimo/_server/export/exporter.py@@ -53,6 +53,7 @@ from marimo._server.templates.templates import ()from marimo._server.tokens import SkewProtectionTokenfrom marimo._types.ids import CellId_t+from marimo._utils import async_pathfrom marimo._utils.code import hash_codefrom marimo._utils.data_uri import build_data_urlfrom marimo._utils.marimo_path import MarimoPath@@ -529,10 +530,10 @@ class AutoExporter:if export_dir in self._created_dirs:return- if not directory.exists():+ if not await async_path.exists(directory):raise FileNotFoundError(f"Directory {directory} does not exist")- export_dir.mkdir(parents=True, exist_ok=True)+ await async_path.mkdir(export_dir, parents=True, exist_ok=True)# Cache that we've created this directoryself._created_dirs.add(export_dir)diff --git a/marimo/_server/sessions.py b/marimo/_server/sessions.pyindex 6e6b84891e..9505fe9779 100644--- a/marimo/_server/sessions.py+++ b/marimo/_server/sessions.py@@ -75,6 +75,7 @@ from marimo._server.tokens import AuthToken, SkewProtectionTokenfrom marimo._server.types import ProcessLikefrom marimo._server.utils import print_, print_tabbedfrom marimo._types.ids import CellId_t, ConsumerId, SessionId+from marimo._utils import async_pathfrom marimo._utils.disposable import Disposablefrom marimo._utils.distributor import (ConnectionDistributor,@@ -898,7 +899,7 @@ class SessionManager:async def on_file_changed(path: Path) -> None:# Skip if the session does not relate to the file- if session.app_file_manager.path != os.path.abspath(path):+ if session.app_file_manager.path != await async_path.abspath(path):return# Use the centralized file change handler@@ -1148,7 +1149,7 @@ class SessionFileChangeHandler:This method reloads the notebook and sends appropriate operationsto the frontend when a marimo file is modified."""- abs_file_path = os.path.abspath(file_path)+ abs_file_path = await async_path.abspath(file_path)# Use a lock to prevent concurrent processing of the same fileif abs_file_path not in self._file_change_locks:diff --git a/marimo/_utils/async_path.py b/marimo/_utils/async_path.pyindex 20ad489585..d2a5dd853b 100644--- a/marimo/_utils/async_path.py+++ b/marimo/_utils/async_path.py@@ -195,6 +195,11 @@ class AsyncPath(PurePath):result = await asyncio.to_thread(self._path.expanduser)return self.__class__(result)+ async def absolute(self) -> AsyncPath:+ """Return an absolute version of this path."""+ result = await asyncio.to_thread(self._path.absolute)+ return self.__class__(result)+# Context manager support for opening filesdef open(@@ -230,3 +235,42 @@ class AsyncWindowsPath(AsyncPath, PureWindowsPath):def __getattr__(self, name: str) -> Any:return super().__getattr__(name) # type: ignore+++# Module-level convenience functions for compatibility with aio_path+++async def exists(path: StrPath) -> bool:+ """Check if a path exists asynchronously."""+ return await AsyncPath(path).exists()+++async def isfile(path: StrPath) -> bool:+ """Check if a path is a file asynchronously."""+ return await AsyncPath(path).is_file()+++async def isdir(path: StrPath) -> bool:+ """Check if a path is a directory asynchronously."""+ return await AsyncPath(path).is_dir()+++async def abspath(path: StrPath) -> str:+ """Return absolute path asynchronously."""+ result = await AsyncPath(path).absolute()+ return str(result)+++async def normpath(path: str) -> str:+ """Normalize a pathname asynchronously."""+ return await asyncio.to_thread(os.path.normpath, path)+++async def mkdir(+ path: StrPath,+ mode: int = 0o777,+ parents: bool = False,+ exist_ok: bool = False,+) -> None:+ """Create a directory asynchronously."""+ await AsyncPath(path).mkdir(mode=mode, parents=parents, exist_ok=exist_ok)diff --git a/marimo/_utils/file_watcher.py b/marimo/_utils/file_watcher.pyindex 89a0dd1ce5..dc9e96e369 100644--- a/marimo/_utils/file_watcher.py+++ b/marimo/_utils/file_watcher.py@@ -11,6 +11,7 @@ from typing import Callable, Optionalfrom marimo import _loggersfrom marimo._dependencies.dependencies import DependencyManager+from marimo._utils import async_pathLOGGER = _loggers.marimo_logger()@@ -79,7 +80,7 @@ class PollingFileWatcher(FileWatcher):async def _poll(self) -> None:while self._running:- if not os.path.exists(self.path):+ if not await async_path.exists(self.path):LOGGER.warning(f"File at {self.path} does not exist.")raise FileNotFoundError(f"File at {self.path} does not exist.")diff --git a/marimo/_utils/files.py b/marimo/_utils/files.pyindex 38fe638fd7..3ed051b87d 100644--- a/marimo/_utils/files.py+++ b/marimo/_utils/files.py@@ -7,6 +7,8 @@ import refrom pathlib import Pathfrom typing import TYPE_CHECKING, Union+from marimo._utils import async_path+if TYPE_CHECKING:from collections.abc import AsyncGenerator, Generator@@ -107,12 +109,12 @@ async def async_expand_file_patterns(seen = set()for pattern in file_patterns:- if os.path.isfile(pattern):+ if await async_path.isfile(pattern):path = Path(pattern)if path not in seen:seen.add(path)yield path- elif os.path.isdir(pattern):+ elif await async_path.isdir(pattern):async for file_path in async_get_files(pattern):if file_path not in seen:seen.add(file_path)@@ -124,7 +126,7 @@ async def async_expand_file_patterns(root = _get_root(pattern)# Get all files from root and filter by pattern- if os.path.isdir(root):+ if await async_path.isdir(root):async for file_path in async_get_files(root):if (fnmatch.fnmatch(str(file_path), pattern)diff --git a/pixi.lock b/pixi.lockindex e553afa806..a16ccffbb5 100644--- a/pixi.lock+++ b/pixi.lock@@ -598,7 +598,8 @@ packages:- pythonlicense: MITlicense_family: MIT- purls: []+ purls:+ - pkg:pypi/hyperlink?source=hash-mappingsize: 74751timestamp: 1733319972207- conda: https://conda.anaconda.org/conda-forge/linux-64/icu-75.1-he02047a_0.conda@@ -1100,8 +1101,8 @@ packages:timestamp: 1727801725384- pypi: ./name: marimo- version: 0.16.5- sha256: 8e9c9303b4e9fb4005c9807e648993e54689447a06edc421a0f6117eb2bb5029+ version: 0.17.0+ sha256: 3ad30865c3f4e4123cc42ed7f504c66d52bf4c3c4378026197cd8a0bf4040350requires_dist:- click>=8.0,<9- jedi>=0.18.0@@ -1637,7 +1638,8 @@ packages:- pythonlicense: BSD-3-Clauselicense_family: BSD- purls: []+ purls:+ - pkg:pypi/pycparser?source=hash-mappingsize: 110100timestamp: 1733195786147- conda: https://conda.anaconda.org/conda-forge/noarch/pygments-2.19.1-pyhd8ed1ab_0.condadiff --git a/pyproject.toml b/pyproject.tomlindex bf45bd4235..d01dd24919 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -362,7 +362,6 @@ ignore = ["PERF203", # try-except within a loop incurs performance overhead; not always possible"PERF401", # Use {message_str} to create a transformed list; at the cost of readability"PERF403", # Use a dictionary comprehension instead of a for-loop; at the cost of readability- "ASYNC240", # Async functions should not use os.path methods, use trio.Path or anyio.path# TODO: we should fix these, and enable this rule"PT011", # `pytest.raises(ValueError)` is too broad, set the `match` parameter or use a more specific exception"E501", # Line too long, we still trimdiff --git a/scripts/pycheck.sh b/scripts/pycheck.shindex efca5ece8b..070b09ae49 100755--- a/scripts/pycheck.sh+++ b/scripts/pycheck.sh@@ -10,3 +10,5 @@ echo "[check: format]"hatch run ruff formatecho "[check: typecheck]"hatch run typecheck:check+echo "[check: update-lock]"+hatch run pixi lockdiff --git a/tests/_cli/test_cli_export.py b/tests/_cli/test_cli_export.pyindex 3aa6e5b87b..8526c58f42 100644--- a/tests/_cli/test_cli_export.py+++ b/tests/_cli/test_cli_export.py@@ -15,6 +15,7 @@ from typing import TYPE_CHECKINGimport pytestfrom marimo._dependencies.dependencies import DependencyManager+from marimo._utils import async_pathfrom marimo._utils.platform import is_windowsfrom tests._server.templates.utils import normalize_index_htmlfrom tests.mocks import snapshotter@@ -323,7 +324,7 @@ class TestExportHTML:assert f"Watching {temp_marimo_file}" in linebreak- assert not path.exists(temp_out_file)+ assert not await async_path.exists(temp_out_file)# Modify filewith open(temp_marimo_file, "a") as f: # noqa: ASYNC230@@ -653,7 +654,7 @@ class TestExportScript:assert f"Watching {temp_marimo_file}" in linebreak- assert not path.exists(temp_out_file)+ assert not await async_path.exists(temp_out_file)# Modify filewith open(temp_marimo_file, "a") as f: # noqa: ASYNC230@@ -668,7 +669,7 @@ class TestExportScript:breakawait asyncio.sleep(0.1)- assert path.exists(temp_out_file)+ assert await async_path.exists(temp_out_file)@pytest.mark.skipif(condition=DependencyManager.watchdog.has(),@@ -771,7 +772,7 @@ class TestExportMarkdown:assert f"Watching {temp_marimo_file}" in linebreak- assert not path.exists(temp_out_file)+ assert not await async_path.exists(temp_out_file)# Modify filewith open(temp_marimo_file, "a") as f: # noqa: ASYNC230@@ -786,7 +787,7 @@ class TestExportMarkdown:breakawait asyncio.sleep(0.1)- assert path.exists(temp_out_file)+ assert await async_path.exists(temp_out_file)@pytest.mark.skipif(condition=DependencyManager.watchdog.has(),diff --git a/tests/_runtime/test_trace.py b/tests/_runtime/test_trace.pyindex 7ccabf3b94..b499ddf02b 100644--- a/tests/_runtime/test_trace.py+++ b/tests/_runtime/test_trace.py@@ -1,4 +1,6 @@# Copyright 2024 Marimo. All rights reserved.+from __future__ import annotations+import osimport reimport subprocess@@ -9,6 +11,7 @@ from marimo._runtime.requests import (ExecutionRequest,)from marimo._runtime.runtime import Kernel+from marimo._utils import async_pathfrom tests._messaging.mocks import MockStderr, MockStream@@ -312,7 +315,7 @@ class TestEmbedTrace:result = tag_re.sub("", result)# windows support- file_path = os.path.normpath(+ file_path = await async_path.normpath("tests/_runtime/script_data/script_exception_with_output.py")diff --git a/tests/_smoke_tests/run_all.py b/tests/_smoke_tests/run_all.pyindex f5433c3e58..190f72870e 100644--- a/tests/_smoke_tests/run_all.py+++ b/tests/_smoke_tests/run_all.py@@ -17,10 +17,10 @@ config_file = os.path.join(os.path.dirname(__file__), "config.yml")async def test_all_smoke_tests() -> None:- root = os.path.realpath(+ root = os.path.realpath( # noqa: ASYNC240str(import_files("marimo").joinpath("_smoke_tests")))- all_py_paths = list(pathlib.Path(root).rglob("*.py"))+ all_py_paths = list(pathlib.Path(root).rglob("*.py")) # noqa: ASYNC240assert all_py_paths, "No smoke tests found"with open(config_file) as f: # noqa: ASYNC230@@ -35,10 +35,10 @@ async def test_all_smoke_tests() -> None:async def test_all_examples() -> None:- root = os.path.realpath(+ root = os.path.realpath( # noqa: ASYNC240str(import_files("marimo").joinpath("../examples")))- all_py_paths = list(pathlib.Path(root).rglob("*.py"))+ all_py_paths = list(pathlib.Path(root).rglob("*.py")) # noqa: ASYNC240assert all_py_paths, "No examples found"with open(config_file) as f: # noqa: ASYNC230diff --git a/tests/_utils/test_file_watcher.py b/tests/_utils/test_file_watcher.pyindex 278d9d8d78..180db9bcef 100644--- a/tests/_utils/test_file_watcher.py+++ b/tests/_utils/test_file_watcher.py@@ -10,6 +10,7 @@ from tempfile import NamedTemporaryFileimport pytestfrom marimo._dependencies.dependencies import DependencyManager+from marimo._utils import async_pathfrom marimo._utils.file_watcher import FileWatcherManager, PollingFileWatcher@@ -203,5 +204,5 @@ async def test_watchdog_file_moved() -> None:finally:# Cleanup- if tmp_path.exists():+ if await async_path.exists(tmp_path):os.remove(tmp_path)