Skip to content

Commit

Permalink
Catch when users decorate class with @app.function() (#1801)
Browse files Browse the repository at this point in the history
  • Loading branch information
devennavani authored May 8, 2024
1 parent a72f88a commit e3927c7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions modal/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,10 @@ def wrapped(
) -> _Function:
nonlocal keep_warm, is_generator

# Check if the decorated object is a class
if inspect.isclass(f):
raise TypeError("The @app.function decorator cannot be used on a class. Please use @app.cls instead.")

if isinstance(f, _PartialFunction):
f.wrapped = True
info = FunctionInfo(f.raw_f, serialized=serialized, name_override=name, cls=_cls)
Expand Down
10 changes: 10 additions & 0 deletions test/app_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,15 @@ def f():
assert "function(image=image)" in str(excinfo.value)


def test_function_decorator_on_class():
app = App()
with pytest.raises(TypeError, match="cannot be used on a class"):

@app.function()
class Foo:
pass


@pytest.mark.asyncio
async def test_deploy_disconnect(servicer, client):
app = App()
Expand Down Expand Up @@ -356,6 +365,7 @@ def test_function_named_app():
app = App()

with pytest.warns(match="app"):

@app.function(serialized=True)
def app():
...
Expand Down

0 comments on commit e3927c7

Please sign in to comment.