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.

improve: parse google-style attributes in docstrings (#3247)

Akshay Agrawalc69a4c87755a
diff --git a/docs/api/inputs/dates.md b/docs/api/inputs/dates.md
index ae26a4eb0e..b1e1782850 100644
--- a/docs/api/inputs/dates.md
+++ b/docs/api/inputs/dates.md
@@ -2,6 +2,8 @@
## Single date
+/// marimo-embed
+
```python
@app.cell
def __():
@@ -15,6 +17,8 @@
```
+///
+
::: marimo.ui.date
:members:
@@ -37,6 +41,8 @@
## Date range
+/// marimo-embed
+
```python
@app.cell
def __():
@@ -49,5 +55,7 @@
return
```
+///
+
::: marimo.ui.date_range
:members:
diff --git a/marimo/_plugins/__init__.py b/marimo/_plugins/__init__.py
index e69de29bb2..be52407b50 100644
--- a/marimo/_plugins/__init__.py
+++ b/marimo/_plugins/__init__.py
@@ -0,0 +1 @@
+# Copyright 2024 Marimo. All rights reserved.
diff --git a/marimo/_plugins/ui/_impl/chat/__init__.py b/marimo/_plugins/ui/_impl/chat/__init__.py
index e69de29bb2..be52407b50 100644
--- a/marimo/_plugins/ui/_impl/chat/__init__.py
+++ b/marimo/_plugins/ui/_impl/chat/__init__.py
@@ -0,0 +1 @@
+# Copyright 2024 Marimo. All rights reserved.
diff --git a/marimo/_plugins/ui/_impl/dataframes/__init__.py b/marimo/_plugins/ui/_impl/dataframes/__init__.py
index e69de29bb2..be52407b50 100644
--- a/marimo/_plugins/ui/_impl/dataframes/__init__.py
+++ b/marimo/_plugins/ui/_impl/dataframes/__init__.py
@@ -0,0 +1 @@
+# Copyright 2024 Marimo. All rights reserved.
diff --git a/marimo/_utils/docs.py b/marimo/_utils/docs.py
index 0bae403420..f4070cec20 100644
--- a/marimo/_utils/docs.py
+++ b/marimo/_utils/docs.py
@@ -1,3 +1,4 @@
+# Copyright 2024 Marimo. All rights reserved.
import re
from textwrap import dedent
@@ -25,14 +26,39 @@ def google_docstring_to_markdown(docstring: str) -> str:
lines = docstring.strip().splitlines()
parsed_lines: list[str] = []
arg_table: list[tuple[str, str, str]] = []
+ attribute_table: list[tuple[str, str, str]] = []
returns_table: list[tuple[str, str]] = []
raises_table: list[tuple[str, str]] = []
in_args = False
+ in_attributes = False
in_returns = False
in_raises = False
in_examples = False
examples_lines: list[str] = []
+ def _handle_arg_or_attribute(
+ table: list[tuple[str, str, str]], stripped: str
+ ) -> None:
+ # Typically: " arg_name (arg_type): description"
+ match = re.match(r"^(\w+)\s*\(([^)]+)\):\s*(.*)", stripped)
+ if match:
+ arg_name, arg_type, description = match.groups()
+ table.append((arg_name, arg_type, description.strip()))
+ else:
+ # Fallback to " arg_name: description"
+ match = re.match(r"^(\w+)\s*:\s*(.*)", stripped)
+ if match:
+ arg_name, description = match.groups()
+ table.append((arg_name, "", description.strip()))
+ else:
+ # Possibly just an indented line continuing the description
+ if table:
+ table[-1] = (
+ table[-1][0],
+ table[-1][1],
+ table[-1][2] + " " + stripped.strip(),
+ )
+
# We'll store a simple summary until we see "Args:" or "Returns:" or "Raises:"
for line in lines:
# Only strip the first 4 spaces
@@ -44,24 +70,35 @@ def google_docstring_to_markdown(docstring: str) -> str:
# Check for control keywords
if re.match(r"^Args:\s*$", stripped):
in_args = True
+ in_attributes = False
+ in_returns = False
+ in_raises = False
+ in_examples = False
+ continue
+ elif re.match(r"^Attributes:\s*$", stripped):
+ in_args = False
+ in_attributes = True
in_returns = False
in_raises = False
in_examples = False
continue
elif re.match(r"^Returns?:\s*$", stripped):
in_args = False
+ in_attributes = False
in_returns = True
in_raises = False
in_examples = False
continue
elif re.match(r"^Raises:\s*$", stripped):
in_args = False
+ in_attributes = False
in_returns = False
in_raises = True
in_examples = False
continue
elif re.match(r"^Examples?:\s*$", stripped):
in_args = False
+ in_attributes = False
in_returns = False
in_raises = False
in_examples = True
@@ -74,25 +111,12 @@ def google_docstring_to_markdown(docstring: str) -> str:
# If within Args:
if in_args:
- # Typically: " arg_name (arg_type): description"
- match = re.match(r"^(\w+)\s*\(([^)]+)\):\s*(.*)", stripped)
- if match:
- arg_name, arg_type, description = match.groups()
- arg_table.append((arg_name, arg_type, description.strip()))
- else:
- # Fallback to " arg_name: description"
- match = re.match(r"^(\w+)\s*:\s*(.*)", stripped)
- if match:
- arg_name, description = match.groups()
- arg_table.append((arg_name, "", description.strip()))
- else:
- # Possibly just an indented line continuing the description
- if arg_table:
- arg_table[-1] = (
- arg_table[-1][0],
- arg_table[-1][1],
- arg_table[-1][2] + " " + stripped.strip(),
- )
+ _handle_arg_or_attribute(arg_table, stripped)
+ continue
+
+ # If within Attributes:
+ if in_attributes:
+ _handle_arg_or_attribute(attribute_table, stripped)
continue
# If within Returns:
@@ -138,6 +162,10 @@ def google_docstring_to_markdown(docstring: str) -> str:
output.append("# Summary")
output.append("\n".join(parsed_lines).strip())
+ if examples_lines:
+ output.append("\n# Examples")
+ output.append("\n".join(examples_lines))
+
if arg_table:
output.append("\n# Arguments")
output.append("| Parameter | Type | Description |")
@@ -145,6 +173,13 @@ def google_docstring_to_markdown(docstring: str) -> str:
for arg_name, arg_type, desc in arg_table:
output.append(f"| `{arg_name}` | `{arg_type}` | {desc.strip()} |")
+ if attribute_table:
+ output.append("\n# Attributes")
+ output.append("| Attribute | Type | Description |")
+ output.append("|-----------|------|-------------|")
+ for arg_name, arg_type, desc in attribute_table:
+ output.append(f"| `{arg_name}` | `{arg_type}` | {desc.strip()} |")
+
if returns_table:
output.append("\n# Returns")
output.append("| Type | Description |")
@@ -157,8 +192,4 @@ def google_docstring_to_markdown(docstring: str) -> str:
for err_type, explanation in raises_table:
output.append(f"- **{err_type}**: {explanation.strip()}")
- if examples_lines:
- output.append("\n# Examples")
- output.append("\n".join(examples_lines))
-
return "\n".join(output)
diff --git a/tests/_utils/snapshots/docstring_complex.txt b/tests/_utils/snapshots/docstring_complex.txt
index 737368968b..38c40ff1d0 100644
--- a/tests/_utils/snapshots/docstring_complex.txt
+++ b/tests/_utils/snapshots/docstring_complex.txt
@@ -2,20 +2,6 @@
Stack items vertically, in a column.
Combine with `hstack` to build a grid of items.
-# Arguments
-| Parameter | Type | Description |
-|-----------|------|-------------|
-| `items` | `Sequence[object]` | A list of items. |
-| `align` | `Literal["start", "end", "center", "stretch"], optional` | Align items horizontally: start, end, center, or stretch. |
-| `justify` | `Literal["start", "center", "end", "space-between", "space-around"]` | Justify items vertically: start, center, end, space-between, or space-around. Defaults to "start". |
-| `gap` | `float, optional` | Gap between items as a float in rem. 1rem is 16px by default. Defaults to 0.5. |
-| `heights` | `Union[Literal["equal"], Sequence[float]], optional` | "equal" to give items equal height; or a list of relative heights with same length as `items`, eg, [1, 2] means the second item is twice as tall as the first; or None for a sensible default. |
-
-# Returns
-| Type | Description |
-|------|-------------|
-| `Html` | An Html object. |
-
# Examples
Build a column of items:
```python
@@ -32,3 +18,18 @@ mo.vstack(
]
)
```
+
+
+# Arguments
+| Parameter | Type | Description |
+|-----------|------|-------------|
+| `items` | `Sequence[object]` | A list of items. |
+| `align` | `Literal["start", "end", "center", "stretch"], optional` | Align items horizontally: start, end, center, or stretch. |
+| `justify` | `Literal["start", "center", "end", "space-between", "space-around"]` | Justify items vertically: start, center, end, space-between, or space-around. Defaults to "start". |
+| `gap` | `float, optional` | Gap between items as a float in rem. 1rem is 16px by default. Defaults to 0.5. |
+| `heights` | `Union[Literal["equal"], Sequence[float]], optional` | "equal" to give items equal height; or a list of relative heights with same length as `items`, eg, [1, 2] means the second item is twice as tall as the first; or None for a sensible default. |
+
+# Returns
+| Type | Description |
+|------|-------------|
+| `Html` | An Html object. |
\ No newline at end of file
diff --git a/tests/_utils/snapshots/docstring_summary.txt b/tests/_utils/snapshots/docstring_summary.txt
index 57af8a36da..fc52f6a989 100644
--- a/tests/_utils/snapshots/docstring_summary.txt
+++ b/tests/_utils/snapshots/docstring_summary.txt
@@ -1,22 +1,28 @@
# Summary
This is the summary.
+# Examples
+```python
+print('Hello, world!')
+print(foo, bar)
+```
+
# Arguments
| Parameter | Type | Description |
|-----------|------|-------------|
| `foo` | `str` | Lorem ipsum |
| `bar` | `int` | Dolor sit amet |
+# Attributes
+| Attribute | Type | Description |
+|-----------|------|-------------|
+| `baz` | `str` | Lorem ipsum |
+| `boo` | `int` | Dolor sit amet |
+
# Returns
| Type | Description |
|------|-------------|
| `bool` | Return description |
# Raises
-- **ValueError**: Something about the value
-
-# Examples
-```python
-print('Hello, world!')
-print(foo, bar)
-```
\ No newline at end of file
+- **ValueError**: Something about the value
\ No newline at end of file
diff --git a/tests/_utils/test_docs.py b/tests/_utils/test_docs.py
index 80f6f56b59..196822815f 100644
--- a/tests/_utils/test_docs.py
+++ b/tests/_utils/test_docs.py
@@ -11,6 +11,10 @@ def test_google_docstring_to_markdown_summary():
foo (str): Lorem ipsum
bar (int): Dolor sit amet
+ Attributes:
+ baz (str): Lorem ipsum
+ boo (int): Dolor sit amet
+
Returns:
bool: Return description
@@ -32,6 +36,9 @@ def test_google_docstring_to_markdown_summary():
"# Arguments",
"`foo`",
"`bar`",
+ "# Attributes",
+ "`baz`",
+ "`boo`",
"# Returns",
"`bool`",
"# Raises",