Skip to content

Commit 98053f1

Browse files
authored
Removed library imports from the worker (#1046)
* Fix ModuleNotFoundError for older pinned functions versions * Organising imports * Fixed unit tests * Added unit test * Added unit tests * Remove library imports
1 parent bcda54d commit 98053f1

File tree

6 files changed

+50
-24
lines changed

6 files changed

+50
-24
lines changed

azure_functions_worker/functions.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
import pathlib
66
import typing
77

8-
from azure.functions import DataType, Function
9-
108
from . import bindings as bindings_utils
119
from . import protos
1210
from ._thirdparty import typing_inspect
@@ -216,8 +214,7 @@ def validate_function_params(params: dict, bound_params: dict,
216214
param_bind_type, param_py_type)
217215

218216
if not checks_out:
219-
if binding.data_type is not DataType(
220-
protos.BindingInfo.undefined):
217+
if binding.data_type is not protos.BindingInfo.undefined:
221218
raise FunctionLoadError(
222219
func_name,
223220
f'{param.name!r} binding type "{binding.type}" '
@@ -357,7 +354,7 @@ def add_function(self, function_id: str,
357354
output_types, return_type)
358355

359356
def add_indexed_function(self, function_id: str,
360-
function: Function):
357+
function):
361358
func = function.get_user_function()
362359
func_name = function.get_function_name()
363360
return_binding_name: typing.Optional[str] = None

azure_functions_worker/loader.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
import sys
1111
import uuid
1212
from os import PathLike, fspath
13-
from typing import List, Optional, Dict
14-
15-
from azure.functions import Function, FunctionApp
13+
from typing import Optional, Dict
1614

1715
from . import protos, functions
1816
from .constants import MODULE_NOT_FOUND_TS_URL, SCRIPT_FILE_NAME, \
@@ -47,7 +45,7 @@ def uninstall() -> None:
4745
pass
4846

4947

50-
def build_binding_protos(indexed_function: List[Function]) -> Dict:
48+
def build_binding_protos(indexed_function) -> Dict:
5149
binding_protos = {}
5250
for binding in indexed_function.get_bindings():
5351
binding_protos[binding.name] = protos.BindingInfo(
@@ -59,7 +57,7 @@ def build_binding_protos(indexed_function: List[Function]) -> Dict:
5957

6058

6159
def process_indexed_function(functions_registry: functions.Registry,
62-
indexed_functions: List[Function]):
60+
indexed_functions):
6361
fx_metadata_results = []
6462
for indexed_function in indexed_functions:
6563
function_id = str(uuid.uuid4())
@@ -141,10 +139,11 @@ def load_function(name: str, directory: str, script_file: str,
141139
expt_type=ImportError,
142140
message=f'Troubleshooting Guide: {MODULE_NOT_FOUND_TS_URL}'
143141
)
144-
def index_function_app(function_path: str) -> List[Function]:
142+
def index_function_app(function_path: str):
145143
module_name = pathlib.Path(function_path).stem
146144
imported_module = importlib.import_module(module_name)
147145

146+
from azure.functions import FunctionApp
148147
app: Optional[FunctionApp] = None
149148
for i in imported_module.__dir__():
150149
if isinstance(getattr(imported_module, i, None), FunctionApp):
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"scriptFile": "main.py",
3+
"bindings": [
4+
{
5+
"type": "httpTrigger",
6+
"direction": "in",
7+
"dataType" : "string",
8+
"name": "req"
9+
}
10+
]
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
import azure.functions as azf
4+
5+
6+
def main(req: azf.HttpResponse):
7+
return 'This function should fail!!'

tests/unittests/test_broken_functions.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,9 @@ async def test_load_broken__invalid_http_trigger_anno(self):
201201

202202
self.assertEqual(
203203
r.response.result.exception.message,
204-
'FunctionLoadError: cannot load the invalid_http_trigger_anno '
205-
'function: \'req\' binding type "httpTrigger" and dataType "0"'
206-
' in function.json do not match the corresponding function'
207-
' parameter\'s Python type annotation "int"')
204+
'FunctionLoadError: cannot load the invalid_http_trigger_anno'
205+
' function: type of req binding in function.json "httpTrigger" '
206+
'does not match its Python annotation "int"')
208207

209208
async def test_load_broken__invalid_out_anno(self):
210209
async with testutils.start_mockhost(
@@ -218,9 +217,8 @@ async def test_load_broken__invalid_out_anno(self):
218217
self.assertEqual(
219218
r.response.result.exception.message,
220219
'FunctionLoadError: cannot load the invalid_out_anno function: '
221-
'\'ret\' binding type "http" and dataType "0" in function.json'
222-
' do not match the corresponding function parameter\'s Python'
223-
' type annotation "HttpRequest"')
220+
r'type of ret binding in function.json "http" '
221+
r'does not match its Python annotation "HttpRequest"')
224222

225223
async def test_load_broken__invalid_in_anno(self):
226224
async with testutils.start_mockhost(
@@ -233,10 +231,25 @@ async def test_load_broken__invalid_in_anno(self):
233231

234232
self.assertEqual(
235233
r.response.result.exception.message,
236-
'FunctionLoadError: cannot load the invalid_in_anno function:'
237-
' \'req\' binding type "httpTrigger" and dataType "0" in '
238-
'function.json do not match the corresponding function '
239-
'parameter\'s Python type annotation "HttpResponse"')
234+
'FunctionLoadError: cannot load the invalid_in_anno function: '
235+
r'type of req binding in function.json "httpTrigger" '
236+
r'does not match its Python annotation "HttpResponse"')
237+
238+
async def test_load_broken__invalid_datatype(self):
239+
async with testutils.start_mockhost(
240+
script_root=self.broken_funcs_dir) as host:
241+
func_id, r = await host.load_function('invalid_datatype')
242+
243+
self.assertEqual(r.response.function_id, func_id)
244+
self.assertEqual(r.response.result.status,
245+
protos.StatusResult.Failure)
246+
247+
self.assertRegex(
248+
r.response.result.exception.message,
249+
r'.*cannot load the invalid_datatype function: '
250+
r'.*binding type "httpTrigger" and dataType "1" in '
251+
r'function.json do not match the corresponding function '
252+
r'parameter.* Python type annotation "HttpResponse"')
240253

241254
async def test_load_broken__invalid_in_anno_non_type(self):
242255
async with testutils.start_mockhost(

tests/unittests/test_utilities.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
# Licensed under the MIT License.
33
import os
44
import sys
5+
import typing
56
import unittest
67
from unittest.mock import patch
7-
import typing
88

99
from azure_functions_worker.utils import common, wrappers
1010

11-
1211
TEST_APP_SETTING_NAME = "TEST_APP_SETTING_NAME"
1312
TEST_FEATURE_FLAG = "APP_SETTING_FEATURE_FLAG"
1413
FEATURE_DEFAULT = 42

0 commit comments

Comments
 (0)