Skip to content

Commit 3873bab

Browse files
authoredAug 19, 2024··
feat: Adding ability to invoke functions directly (#238)
* Adding new direct calling tests * Adding comments and improving tests
1 parent b620229 commit 3873bab

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed
 

‎azure/functions/decorators/function_app.py

+22-4
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,26 @@ def __init__(self, func: Callable[..., Any], script_file: str):
7575
self.http_type = 'function'
7676
self._is_http_function = False
7777

78+
def __str__(self):
79+
"""Return the function.json representation of the function"""
80+
return self.get_function_json()
81+
82+
def __call__(self, *args, **kwargs):
83+
"""This would allow the Function object to be directly callable and runnable
84+
directly using the interpreter locally.
85+
86+
Example:
87+
@app.route(route="http_trigger")
88+
def http_trigger(req: func.HttpRequest) -> func.HttpResponse:
89+
return "Hello, World!"
90+
91+
print(http_trigger(None))
92+
93+
➜ python function_app.py
94+
Hello, World!
95+
"""
96+
return self._func(*args, **kwargs)
97+
7898
def add_binding(self, binding: Binding) -> None:
7999
"""Add a binding instance to the function.
80100
@@ -201,17 +221,15 @@ def get_function_json(self) -> str:
201221
"""
202222
return json.dumps(self.get_dict_repr(), cls=StringifyEnumJsonEncoder)
203223

204-
def __str__(self):
205-
return self.get_function_json()
206-
207224

208225
class FunctionBuilder(object):
209226

210227
def __init__(self, func, function_script_file):
211228
self._function = Function(func, function_script_file)
212229

213230
def __call__(self, *args, **kwargs):
214-
pass
231+
"""Call the Function object directly"""
232+
return self._function(*args, **kwargs)
215233

216234
def configure_http_type(self, http_type: str) -> 'FunctionBuilder':
217235
self._function.set_http_type(http_type)

‎tests/decorators/test_function_app.py

+16
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,22 @@ def test_blueprint_same_function_and_method_name(name: str):
543543
" Please change @app.function_name() or the function"
544544
" method name to be unique.")
545545

546+
def test_user_function_is_directly_callable_no_args(self):
547+
def test_validate_function_working_no_args():
548+
return "dummy"
549+
550+
self.dummy = test_validate_function_working_no_args
551+
self.fb = FunctionBuilder(self.dummy, "dummy.py")
552+
self.assertEqual(self.fb(), "dummy")
553+
554+
def test_user_function_is_directly_callable_args(self):
555+
def test_validate_function_working_sum_args(arg1: int, arg2: int):
556+
return arg1 + arg2
557+
558+
self.dummy = test_validate_function_working_sum_args
559+
self.fb = FunctionBuilder(self.dummy, "dummy.py")
560+
self.assertEqual(self.fb(1, 2), 3)
561+
546562

547563
class TestScaffold(unittest.TestCase):
548564
def setUp(self):

0 commit comments

Comments
 (0)
Please sign in to comment.