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.

fix: dont drop async prefix when re-formatting cell signature (#1167)

Akshay Agrawal2592a4972f06
diff --git a/marimo/_ast/codegen.py b/marimo/_ast/codegen.py
index e468bcde28..2092f3260a 100644
--- a/marimo/_ast/codegen.py
+++ b/marimo/_ast/codegen.py
@@ -71,11 +71,11 @@ def to_functiondef(
decorator = _to_decorator(cell.config)
signature = f"def {name}({args}):"
- if cell.is_coroutine():
- signature = "async " + signature
-
- if len(INDENT + signature) >= MAX_LINE_LENGTH:
+ prefix = "" if not cell.is_coroutine() else "async "
+ if len(INDENT + prefix + signature) >= MAX_LINE_LENGTH:
signature = f"def {name}{_multiline_tuple(refs)}:"
+ signature = prefix + signature
+
fndef = decorator + "\n" + signature + "\n"
body = indent_text(cell.code)
if body:
diff --git a/tests/_ast/codegen_data/test_generate_filecontents_async_long_signature.py b/tests/_ast/codegen_data/test_generate_filecontents_async_long_signature.py
new file mode 100644
index 0000000000..0a5f17f47b
--- /dev/null
+++ b/tests/_ast/codegen_data/test_generate_filecontents_async_long_signature.py
@@ -0,0 +1,47 @@
+import marimo
+
+__generated_with = "0.4.0"
+app = marimo.App()
+
+
+def one():
+ (
+ client,
+ get_calculation_trigger,
+ get_components_configuration,
+ get_conditions_state,
+ ) = (1, 1, 1, 1)
+ return (
+ client,
+ get_calculation_trigger,
+ get_components_configuration,
+ get_conditions_state,
+ )
+
+
+async def two(
+ client,
+ get_calculation_trigger,
+ get_components_configuration,
+ get_conditions_state,
+):
+ _conditions = [c for c in get_conditions_state().values()]
+ _configuration = get_components_configuration()
+
+ _configuration_conditions_list = {
+ "configuration": _configuration,
+ "condition": _conditions,
+ }
+
+ _trigger = get_calculation_trigger()
+
+ async for data_point in client("test", "ws://localhost:8000"):
+ print(data_point)
+ data_point
+ return data_point,
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/tests/_ast/test_codegen.py b/tests/_ast/test_codegen.py
index 63c112199c..2c997d05a1 100644
--- a/tests/_ast/test_codegen.py
+++ b/tests/_ast/test_codegen.py
@@ -79,6 +79,42 @@ class TestGeneration:
"test_generate_filecontents_async"
)
+ @staticmethod
+ def test_generate_filecontents_async_long_signature() -> None:
+ cell_one = cleandoc(
+ """
+ (
+ client,
+ get_calculation_trigger,
+ get_components_configuration,
+ get_conditions_state,
+ ) = (1, 1, 1, 1)
+ """
+ )
+ cell_two = cleandoc(
+ """
+ _conditions = [c for c in get_conditions_state().values()]
+ _configuration = get_components_configuration()
+
+ _configuration_conditions_list = {
+ "configuration": _configuration,
+ "condition": _conditions,
+ }
+
+ _trigger = get_calculation_trigger()
+
+ async for data_point in client("test", "ws://localhost:8000"):
+ print(data_point)
+ data_point
+ """
+ )
+ codes = [cell_one, cell_two]
+ names = ["one", "two"]
+ contents = generate_filecontents(codes, names)
+ assert contents == get_expected_filecontents(
+ "test_generate_filecontents_async_long_signature"
+ )
+
@staticmethod
def test_generate_filecontents_single_cell() -> None:
cell_one = "import numpy as np"