Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: top level imports #3779

Merged
merged 19 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
72a1643
feat: top level imports
dmadisetti Feb 13, 2025
022e799
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 13, 2025
314717a
fix: nvm imports _should_ have returns
dmadisetti Feb 13, 2025
14c50f4
Merge branch 'main' of github:marimo-team/marimo into dm/toplevel-imp…
dmadisetti Feb 13, 2025
042ce29
Merge branch 'dm/toplevel-imports' of github:marimo-team/marimo into …
dmadisetti Feb 13, 2025
7db678f
temp: import_guard relatively easy to put in
dmadisetti Feb 14, 2025
8d94f03
Merge branch 'main' into dm/toplevel-imports
dmadisetti Feb 14, 2025
8281b78
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 14, 2025
db9d257
format: formated imports with stripped comments over import_guard + t…
dmadisetti Feb 17, 2025
017f266
merge res
dmadisetti Feb 17, 2025
042417c
Merge branch 'main' of github:marimo-team/marimo into dm/toplevel-imp…
dmadisetti Feb 17, 2025
36aa98e
experimental upstream prior to design doc
dmadisetti Feb 17, 2025
ecd4b50
fix: py39 support
dmadisetti Feb 17, 2025
5f169a2
fix: more py39 typing support
dmadisetti Feb 17, 2025
310d46b
fix: py39 typing support
dmadisetti Feb 17, 2025
fffc67f
fix: wonder if my ruff is different locally?
dmadisetti Feb 17, 2025
b8f5889
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 17, 2025
22559eb
fix: strip whitespace (for now) for difference between CI
dmadisetti Feb 18, 2025
95c9d71
Merge branch 'dm/toplevel-imports' of github:marimo-team/marimo into …
dmadisetti Feb 18, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion marimo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"icon",
"iframe",
"image",
"import_guard",
"latex",
"lazy",
"left",
Expand Down Expand Up @@ -125,7 +126,7 @@
redirect_stderr,
redirect_stdout,
)
from marimo._runtime.context.utils import running_in_notebook
from marimo._runtime.context.utils import import_guard, running_in_notebook
from marimo._runtime.control_flow import MarimoStopError, stop
from marimo._runtime.runtime import (
app_meta,
Expand Down
14 changes: 9 additions & 5 deletions marimo/_ast/cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,9 @@ def namespace_to_variable(self, namespace: str) -> Name | None:
def is_coroutine(self) -> bool:
return _is_coroutine(self.body) or _is_coroutine(self.last_expr)

@property
def is_toplevel_acceptable(self) -> bool:
def is_toplevel_acceptable(
self, allowed_refs: Optional[set[Name]] = None
) -> bool:
# Check no defs aside from the single function
if len(self.defs) != 1:
return False
Expand All @@ -316,13 +317,16 @@ def is_toplevel_acceptable(self) -> bool:
return False

# No required_refs are allowed for now
# TODO: Allow imports and other toplevel functions
refs = set().union(
*[v.required_refs for v in self.variable_data[name]]
)
refs -= set(globals()["__builtins__"].keys())
# NOTE: Builtins are allowed, but should be passed in under
# allowed_refs. Defers to allowed_refs because shadowed builtins
# are accounted for.
if allowed_refs is None:
allowed_refs = set(globals()["__builtins__"].keys())
# Allow recursion
refs -= {name}
refs -= {name} | allowed_refs
if refs:
return False

Expand Down
Loading
Loading