Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 14 additions & 1 deletion modal/_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,20 @@ def __init_subclass__(cls, type_prefix: Optional[str] = None):

def __init__(self, *args, **kwargs):
"""mdmd:hidden"""
raise InvalidError(f"Class {type(self).__name__} has no constructor. Use class constructor methods instead.")
constructor_methods = ["from_name", "from_id", "ephemeral"]

valid_constructor_methods = []
class_name = type(self).__name__.lstrip("_")
for method in constructor_methods:
if hasattr(self, method):
valid_constructor_methods.append(f"`{class_name}.{method}`")

valid_methods_msg = ""
if valid_constructor_methods:
valid_methods = ", ".join(valid_constructor_methods)
valid_methods_msg = f"Please use {valid_methods} instead"

raise InvalidError(f"{class_name}(...) is not allowed. {valid_methods_msg}")

def _init(
self,
Expand Down
6 changes: 0 additions & 6 deletions modal/dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,6 @@ class _Dict(_Object, type_prefix="di"):
_name: Optional[str] = None
_metadata: Optional[api_pb2.DictMetadata] = None

def __init__(self, data={}):
"""mdmd:hidden"""
raise RuntimeError(
"`Dict(...)` constructor is not allowed. Please use `Dict.from_name` or `Dict.ephemeral` instead"
)

@classproperty
def objects(cls) -> _DictManager:
return _DictManager
Expand Down
4 changes: 0 additions & 4 deletions modal/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,6 @@ class _Queue(_Object, type_prefix="qu"):

_metadata: Optional[api_pb2.QueueMetadata] = None

def __init__(self):
"""mdmd:hidden"""
raise RuntimeError("Queue() is not allowed. Please use `Queue.from_name(...)` or `Queue.ephemeral()` instead.")

@classproperty
def objects(cls) -> _QueueManager:
return _QueueManager
Expand Down
17 changes: 16 additions & 1 deletion test/object_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright Modal Labs 2022
import pytest

from modal import Secret
from modal import Image, Queue, Secret, Volume
from modal._object import _Object
from modal.dict import Dict, _Dict
from modal.exception import InvalidError
Expand Down Expand Up @@ -41,3 +41,18 @@ def test_types():
assert not _Dict._is_id_type("qu-123")
assert _Queue._is_id_type("qu-123")
assert not _Queue._is_id_type("di-123")


@pytest.mark.parametrize(
"Kls, msg",
[
(Image, r"Please use `Image\.from_id` instead"),
(Dict, r"Please use `Dict\.from_name`, `Dict\.ephemeral` instead"),
(Volume, r"Please use `Volume\.from_name`, `Volume\.ephemeral` instead"),
(Queue, r"Please use `Queue\.from_name`, `Queue\.ephemeral` instead"),
(Secret, r"Please use `Secret\.from_name` instead"),
],
)
def test_improve_error_messaage(Kls, msg):
with pytest.raises(InvalidError, match=msg):
_ = Kls()
Loading