Skip to content

Commit eb279bf

Browse files
davidmrdavidgavin-aguiarhallvictoria
authored
Naive implementation of a backwards-compatible function_name decorator (#202)
* Implement backwards compatibility for function_name * Moving function_name to decoratorapi * Fixed flake8 validation --------- Co-authored-by: gavin-aguiar <[email protected]> Co-authored-by: gavin-aguiar <[email protected]> Co-authored-by: hallvictoria <[email protected]>
1 parent 4b6c9eb commit eb279bf

File tree

2 files changed

+53
-28
lines changed

2 files changed

+53
-28
lines changed

azure/functions/decorators/function_app.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,30 @@ def app_script_file(self) -> str:
288288
"""
289289
return self._app_script_file
290290

291+
def function_name(self, name: str,
292+
setting_extra_fields: Dict[str, Any] = {},
293+
) -> Callable[..., Any]:
294+
"""Optional: Sets name of the :class:`Function` object. If not set,
295+
it will default to the name of the method name.
296+
297+
:param name: Name of the function.
298+
:param setting_extra_fields: Keyword arguments for specifying
299+
additional setting fields
300+
:return: Decorator function.
301+
"""
302+
303+
@self._configure_function_builder
304+
def wrap(fb):
305+
def decorator():
306+
fb.add_setting(setting=FunctionName(
307+
function_name=name,
308+
**setting_extra_fields))
309+
return fb
310+
311+
return decorator()
312+
313+
return wrap
314+
291315
def _validate_type(self,
292316
func: Union[Callable[..., Any], FunctionBuilder]) \
293317
-> FunctionBuilder:
@@ -2601,30 +2625,6 @@ class SettingsApi(DecoratorApi, ABC):
26012625
"""Interface to extend for using existing settings decorator in
26022626
functions."""
26032627

2604-
def function_name(self, name: str,
2605-
setting_extra_fields: Dict[str, Any] = {},
2606-
) -> Callable[..., Any]:
2607-
"""Optional: Sets name of the :class:`Function` object. If not set,
2608-
it will default to the name of the method name.
2609-
2610-
:param name: Name of the function.
2611-
:param setting_extra_fields: Keyword arguments for specifying
2612-
additional setting fields
2613-
:return: Decorator function.
2614-
"""
2615-
2616-
@self._configure_function_builder
2617-
def wrap(fb):
2618-
def decorator():
2619-
fb.add_setting(setting=FunctionName(
2620-
function_name=name,
2621-
**setting_extra_fields))
2622-
return fb
2623-
2624-
return decorator()
2625-
2626-
return wrap
2627-
26282628
def retry(self,
26292629
strategy: str,
26302630
max_retry_count: str,

tests/decorators/test_function_app.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
TIMER_TRIGGER
1212
from azure.functions.decorators.core import DataType, AuthLevel, \
1313
BindingDirection, SCRIPT_FILE_NAME
14-
from azure.functions.decorators.function_app import FunctionBuilder, \
15-
FunctionApp, Function, Blueprint, DecoratorApi, AsgiFunctionApp, \
16-
WsgiFunctionApp, HttpFunctionsAuthLevelMixin, FunctionRegister, \
17-
TriggerApi, ExternalHttpFunctionApp
14+
from azure.functions.decorators.function_app import BindingApi, \
15+
FunctionBuilder, FunctionApp, Function, Blueprint, DecoratorApi, \
16+
AsgiFunctionApp, WsgiFunctionApp, HttpFunctionsAuthLevelMixin, \
17+
FunctionRegister, TriggerApi, ExternalHttpFunctionApp
1818
from azure.functions.decorators.http import HttpTrigger, HttpOutput, \
1919
HttpMethod
2020
from azure.functions.decorators.retry_policy import RetryPolicy
@@ -489,6 +489,31 @@ def hello(name: str):
489489
self.assertEqual(setting.get_settings_value("function_name"),
490490
"timer_function")
491491

492+
def test_legacy_blueprints_with_function_name(self):
493+
class LegacyBluePrint(TriggerApi, BindingApi):
494+
pass
495+
496+
class DummyFunctionApp(FunctionRegister, TriggerApi):
497+
pass
498+
499+
app = DummyFunctionApp(auth_level=AuthLevel.ANONYMOUS)
500+
blueprint = LegacyBluePrint()
501+
502+
@blueprint.function_name("timer_function")
503+
@blueprint.schedule(arg_name="name", schedule="10****")
504+
def hello(name: str):
505+
return name
506+
507+
app.register_blueprint(blueprint)
508+
509+
functions = app.get_functions()
510+
self.assertEqual(len(functions), 1)
511+
512+
setting = functions[0].get_setting("function_name")
513+
514+
self.assertEqual(setting.get_settings_value("function_name"),
515+
"timer_function")
516+
492517
def test_function_register_register_function_register_error(self):
493518
class DummyFunctionApp(FunctionRegister):
494519
pass

0 commit comments

Comments
 (0)