Skip to content

Commit

Permalink
Rename is_module to use_module_mode
Browse files Browse the repository at this point in the history
  • Loading branch information
freider committed Feb 19, 2025
1 parent 479318e commit bb22f2c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 17 deletions.
10 changes: 5 additions & 5 deletions modal/cli/import_refs.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
@dataclasses.dataclass
class ImportRef:
file_or_module: str
is_module: bool
use_module_mode: bool # i.e. using the -m flag

# object_path is a .-delimited path to the object to execute, or a parent from which to infer the object
# e.g.
Expand All @@ -42,15 +42,15 @@ class ImportRef:
object_path: str = dataclasses.field(default="")


def parse_import_ref(object_ref: str, is_module: bool = False) -> ImportRef:
def parse_import_ref(object_ref: str, use_module_mode: bool = False) -> ImportRef:
if object_ref.find("::") > 1:
file_or_module, object_path = object_ref.split("::", 1)
elif object_ref.find(":") > 1:
raise InvalidError(f"Invalid object reference: {object_ref}. Did you mean '::' instead of ':'?")
else:
file_or_module, object_path = object_ref, ""

return ImportRef(file_or_module, is_module, object_path)
return ImportRef(file_or_module, use_module_mode, object_path)


DEFAULT_APP_NAME = "app"
Expand All @@ -63,8 +63,8 @@ def import_file_or_module(import_ref: ImportRef, base_cmd: str = ""):
# so we add it in order to make module path specification possible
sys.path.insert(0, "") # "" means the current working directory

if not import_ref.file_or_module.endswith(".py") or import_ref.is_module:
if not import_ref.is_module:
if not import_ref.file_or_module.endswith(".py") or import_ref.use_module_mode:
if not import_ref.use_module_mode:
deprecation_warning(
(2025, 2, 6),
f"Using Python module paths will require using the -m flag in a future version of Modal.\n"
Expand Down
2 changes: 1 addition & 1 deletion modal/cli/launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def _launch_program(name: str, filename: str, detach: bool, args: dict[str, Any]

program_path = str(Path(__file__).parent / "programs" / filename)
base_cmd = f"modal launch {name}"
module = import_file_or_module(ImportRef(program_path, is_module=False), base_cmd=base_cmd)
module = import_file_or_module(ImportRef(program_path, use_module_mode=False), base_cmd=base_cmd)
entrypoint = module.main

app = _get_runnable_app(entrypoint)
Expand Down
10 changes: 5 additions & 5 deletions modal/cli/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ def get_command(self, ctx, func_ref):
ctx.ensure_object(dict)
ctx.obj["env"] = ensure_env(ctx.params["env"])

import_ref = parse_import_ref(func_ref, is_module=ctx.params["m"])
import_ref = parse_import_ref(func_ref, use_module_mode=ctx.params["m"])
runnable, all_usable_commands = import_and_filter(
import_ref, base_cmd="modal run", accept_local_entrypoint=True, accept_webhook=False
)
Expand Down Expand Up @@ -422,7 +422,7 @@ def deploy(
env: str = ENV_OPTION,
stream_logs: bool = typer.Option(False, help="Stream logs from the app upon deployment."),
tag: str = typer.Option("", help="Tag the deployment with a version."),
is_module: bool = typer.Option(
use_module_mode: bool = typer.Option(
False, "-m", help="Interpret argument as a Python module path instead of a file/script path"
),
):
Expand All @@ -435,7 +435,7 @@ def deploy(
# this ensures that lookups without environment specification use the same env as specified
env = ensure_env(env)

import_ref = parse_import_ref(app_ref, is_module=is_module)
import_ref = parse_import_ref(app_ref, use_module_mode=use_module_mode)
app = import_app_from_ref(import_ref, base_cmd="modal deploy")

if name is None:
Expand All @@ -452,7 +452,7 @@ def serve(
app_ref: str = typer.Argument(..., help="Path to a Python file with an app."),
timeout: Optional[float] = None,
env: str = ENV_OPTION,
is_module: bool = typer.Option(
use_module_mode: bool = typer.Option(
False, "-m", help="Interpret argument as a Python module path instead of a file/script path"
),
):
Expand All @@ -465,7 +465,7 @@ def serve(
```
"""
env = ensure_env(env)
import_ref = parse_import_ref(app_ref, is_module=is_module)
import_ref = parse_import_ref(app_ref, use_module_mode=use_module_mode)
app = import_app_from_ref(import_ref, base_cmd="modal serve")
if app.description is None:
app.set_description(_get_clean_app_description(app_ref))
Expand Down
11 changes: 6 additions & 5 deletions test/cli_imports_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def test_import_and_filter(dir_structure, ref, mock_dir, returned_runnable_type,
def test_import_and_filter_2(monkeypatch, supports_on_path):
def import_runnable(object_path, accept_local_entrypoint=False, accept_webhook=False):
return import_and_filter(
ImportRef("import_and_filter_source", is_module=True, object_path=object_path),
ImportRef("import_and_filter_source", use_module_mode=True, object_path=object_path),
base_cmd="",
accept_local_entrypoint=accept_local_entrypoint,
accept_webhook=accept_webhook,
Expand Down Expand Up @@ -166,25 +166,26 @@ def test_import_package_and_module_names(monkeypatch, supports_dir):
# is __main__ when using `python` but in the Modal runtime it's the name of the
# file minus the ".py", since Modal has its own __main__
monkeypatch.chdir(supports_dir)
mod1 = import_file_or_module(ImportRef("assert_package", is_module=True))
mod1 = import_file_or_module(ImportRef("assert_package", use_module_mode=True))
assert mod1.__package__ == ""
assert mod1.__name__ == "assert_package"

monkeypatch.chdir(supports_dir.parent)
with pytest.warns(PendingDeprecationError, match=r"\s-m\s"):
mod2 = import_file_or_module(ImportRef("test.supports.assert_package", is_module=False)) # TODO: is_module=True
# TODO: this should use use_module_mode=True once we remove the deprecation warning
mod2 = import_file_or_module(ImportRef("test.supports.assert_package", use_module_mode=False))

assert mod2.__package__ == "test.supports"
assert mod2.__name__ == "test.supports.assert_package"

mod3 = import_file_or_module(ImportRef("supports/assert_package.py", is_module=False))
mod3 = import_file_or_module(ImportRef("supports/assert_package.py", use_module_mode=False))
assert mod3.__package__ == ""
assert mod3.__name__ == "assert_package"


def test_invalid_source_file_exception():
with pytest.raises(InvalidError, match="Invalid Modal source filename: 'foo.bar.py'"):
import_file_or_module(ImportRef("path/to/foo.bar.py", is_module=False))
import_file_or_module(ImportRef("path/to/foo.bar.py", use_module_mode=False))


def test_list_cli_commands():
Expand Down
2 changes: 1 addition & 1 deletion test/live_reload_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

@pytest.fixture
def import_ref(test_dir):
return ImportRef(str(test_dir / "supports" / "app_run_tests" / "webhook.py"), is_module=False)
return ImportRef(str(test_dir / "supports" / "app_run_tests" / "webhook.py"), use_module_mode=False)


@pytest.mark.asyncio
Expand Down

0 comments on commit bb22f2c

Please sign in to comment.