Skip to content

Commit

Permalink
Error when decorating async functions with *sgi_app (#2279)
Browse files Browse the repository at this point in the history
* error when decorating async functions with *sgi_app

* Fix copypasta ASGI->WSGI

---------

Co-authored-by: Michael Waskom <[email protected]>
  • Loading branch information
aksh-at and mwaskom authored Nov 20, 2024
1 parent 967e74c commit 9f29bb5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
10 changes: 10 additions & 0 deletions modal/partial_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,11 @@ def wrapper(raw_f: Callable[..., Any]) -> _PartialFunction:
f"Modal will drop support for default parameters in a future release.",
)

if inspect.iscoroutinefunction(raw_f):
raise InvalidError(
f"ASGI app function {raw_f.__name__} is an async function. Only sync Python functions are supported."
)

if not wait_for_response:
deprecation_error(
(2024, 5, 13),
Expand Down Expand Up @@ -448,6 +453,11 @@ def wrapper(raw_f: Callable[..., Any]) -> _PartialFunction:
f"Modal will drop support for default parameters in a future release.",
)

if inspect.iscoroutinefunction(raw_f):
raise InvalidError(
f"WSGI app function {raw_f.__name__} is an async function. Only sync Python functions are supported."
)

if not wait_for_response:
deprecation_error(
(2024, 5, 13),
Expand Down
26 changes: 20 additions & 6 deletions test/webhook_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,40 +134,54 @@ async def test_asgi_wsgi(servicer, client):

@app.function(serialized=True)
@asgi_app()
async def my_asgi():
def my_asgi():
pass

@app.function(serialized=True)
@wsgi_app()
async def my_wsgi():
def my_wsgi():
pass

with pytest.raises(InvalidError, match="can't have parameters"):

@app.function(serialized=True)
@asgi_app()
async def my_invalid_asgi(x):
def my_invalid_asgi(x):
pass

with pytest.raises(InvalidError, match="can't have parameters"):

@app.function(serialized=True)
@wsgi_app()
async def my_invalid_wsgi(x):
def my_invalid_wsgi(x):
pass

with pytest.warns(DeprecationError, match="default parameters"):

@app.function(serialized=True)
@asgi_app()
async def my_deprecated_default_params_asgi(x=1):
def my_deprecated_default_params_asgi(x=1):
pass

with pytest.warns(DeprecationError, match="default parameters"):

@app.function(serialized=True)
@wsgi_app()
async def my_deprecated_default_params_wsgi(x=1):
def my_deprecated_default_params_wsgi(x=1):
pass

with pytest.raises(InvalidError, match="async function"):

@app.function(serialized=True)
@asgi_app()
async def my_async_asgi_function():
pass

with pytest.raises(InvalidError, match="async function"):

@app.function(serialized=True)
@wsgi_app()
async def my_async_wsgi_function():
pass

async with app.run(client=client):
Expand Down

0 comments on commit 9f29bb5

Please sign in to comment.