Skip to content

Commit 8214d0c

Browse files
hallvictoriaVictoria Hall
and
Victoria Hall
authored
fix: enable missed return types for generic (#1485)
* tests, support for dict & httpresp * support for returning list * added support for int, double * bool support, unit tests --------- Co-authored-by: Victoria Hall <[email protected]>
1 parent b0bacf0 commit 8214d0c

File tree

31 files changed

+624
-17
lines changed

31 files changed

+624
-17
lines changed

azure_functions_worker/bindings/datumdef.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,19 @@ def datum_as_proto(datum: Datum) -> protos.TypedData:
201201
))
202202
elif datum.type is None:
203203
return None
204+
elif datum.type == 'dict':
205+
# TypedData doesn't support dict, so we return it as json
206+
return protos.TypedData(json=json.dumps(datum.value))
207+
elif datum.type == 'list':
208+
# TypedData doesn't support list, so we return it as json
209+
return protos.TypedData(json=json.dumps(datum.value))
210+
elif datum.type == 'int':
211+
return protos.TypedData(int=datum.value)
212+
elif datum.type == 'double':
213+
return protos.TypedData(double=datum.value)
214+
elif datum.type == 'bool':
215+
# TypedData doesn't support bool, so we return it as an int
216+
return protos.TypedData(int=int(datum.value))
204217
else:
205218
raise NotImplementedError(
206219
'unexpected Datum type: {!r}'.format(datum.type)

azure_functions_worker/bindings/generic.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ def encode(cls, obj: Any, *,
3030
return datumdef.Datum(type='bytes', value=bytes(obj))
3131
elif obj is None:
3232
return datumdef.Datum(type=None, value=obj)
33+
elif isinstance(obj, dict):
34+
return datumdef.Datum(type='dict', value=obj)
35+
elif isinstance(obj, list):
36+
return datumdef.Datum(type='list', value=obj)
37+
elif isinstance(obj, int):
38+
return datumdef.Datum(type='int', value=obj)
39+
elif isinstance(obj, float):
40+
return datumdef.Datum(type='double', value=obj)
41+
elif isinstance(obj, bool):
42+
return datumdef.Datum(type='bool', value=obj)
3343
else:
3444
raise NotImplementedError
3545

tests/endtoend/generic_functions/generic_functions_stein/function_app.py

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Copyright (c) Microsoft Corporation. All rights reserved.
22
# Licensed under the MIT License.
33
import azure.functions as func
4+
45
import logging
56

67
app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)
@@ -45,7 +46,91 @@ def mytimer(mytimer: func.TimerRequest, testEntity) -> None:
4546
logging.info("This timer trigger function executed successfully")
4647

4748

48-
@app.function_name(name="mytimer2")
49+
@app.function_name(name="return_string")
50+
@app.schedule(schedule="*/1 * * * * *", arg_name="mytimer",
51+
run_on_startup=False,
52+
use_monitor=False)
53+
@app.generic_input_binding(
54+
arg_name="testEntity",
55+
type="table",
56+
connection="AzureWebJobsStorage",
57+
table_name="EventHubBatchTest")
58+
def return_string(mytimer: func.TimerRequest, testEntity):
59+
logging.info("Return string")
60+
return "hi!"
61+
62+
63+
@app.function_name(name="return_bytes")
64+
@app.schedule(schedule="*/1 * * * * *", arg_name="mytimer",
65+
run_on_startup=False,
66+
use_monitor=False)
67+
@app.generic_input_binding(
68+
arg_name="testEntity",
69+
type="table",
70+
connection="AzureWebJobsStorage",
71+
table_name="EventHubBatchTest")
72+
def return_bytes(mytimer: func.TimerRequest, testEntity):
73+
logging.info("Return bytes")
74+
return "test-dată"
75+
76+
77+
@app.function_name(name="return_dict")
78+
@app.schedule(schedule="*/1 * * * * *", arg_name="mytimer",
79+
run_on_startup=False,
80+
use_monitor=False)
81+
@app.generic_input_binding(
82+
arg_name="testEntity",
83+
type="table",
84+
connection="AzureWebJobsStorage",
85+
table_name="EventHubBatchTest")
86+
def return_dict(mytimer: func.TimerRequest, testEntity):
87+
logging.info("Return dict")
88+
return {"hello": "world"}
89+
90+
91+
@app.function_name(name="return_list")
92+
@app.schedule(schedule="*/1 * * * * *", arg_name="mytimer",
93+
run_on_startup=False,
94+
use_monitor=False)
95+
@app.generic_input_binding(
96+
arg_name="testEntity",
97+
type="table",
98+
connection="AzureWebJobsStorage",
99+
table_name="EventHubBatchTest")
100+
def return_list(mytimer: func.TimerRequest, testEntity):
101+
logging.info("Return list")
102+
return [1, 2, 3]
103+
104+
105+
@app.function_name(name="return_int")
106+
@app.schedule(schedule="*/1 * * * * *", arg_name="mytimer",
107+
run_on_startup=False,
108+
use_monitor=False)
109+
@app.generic_input_binding(
110+
arg_name="testEntity",
111+
type="table",
112+
connection="AzureWebJobsStorage",
113+
table_name="EventHubBatchTest")
114+
def return_int(mytimer: func.TimerRequest, testEntity):
115+
logging.info("Return int")
116+
return 12
117+
118+
119+
@app.function_name(name="return_double")
120+
@app.schedule(schedule="*/1 * * * * *", arg_name="mytimer",
121+
run_on_startup=False,
122+
use_monitor=False)
123+
@app.generic_input_binding(
124+
arg_name="testEntity",
125+
type="table",
126+
connection="AzureWebJobsStorage",
127+
table_name="EventHubBatchTest")
128+
def return_double(mytimer: func.TimerRequest, testEntity):
129+
logging.info("Return double")
130+
return 12.34
131+
132+
133+
@app.function_name(name="return_bool")
49134
@app.schedule(schedule="*/1 * * * * *", arg_name="mytimer",
50135
run_on_startup=False,
51136
use_monitor=False)
@@ -54,5 +139,6 @@ def mytimer(mytimer: func.TimerRequest, testEntity) -> None:
54139
type="table",
55140
connection="AzureWebJobsStorage",
56141
table_name="EventHubBatchTest")
57-
def mytimer2(mytimer: func.TimerRequest, testEntity):
58-
logging.info("Timer trigger with none return and no type hint")
142+
def return_bool(mytimer: func.TimerRequest, testEntity):
143+
logging.info("Return bool")
144+
return True
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"scriptFile": "main.py",
3+
"bindings": [
4+
{
5+
"name": "mytimer",
6+
"type": "timerTrigger",
7+
"direction": "in",
8+
"schedule": "*/1 * * * * *",
9+
"runOnStartup": false
10+
},
11+
{
12+
"direction": "in",
13+
"type": "table",
14+
"name": "testEntity",
15+
"partitionKey": "test",
16+
"rowKey": "WillBePopulatedWithGuid",
17+
"tableName": "BindingTestTable",
18+
"connection": "AzureWebJobsStorage"
19+
}
20+
]
21+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
import logging
5+
6+
import azure.functions as func
7+
8+
9+
def main(mytimer: func.TimerRequest, testEntity):
10+
logging.info("Return bool")
11+
return True
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"scriptFile": "main.py",
3+
"bindings": [
4+
{
5+
"name": "mytimer",
6+
"type": "timerTrigger",
7+
"direction": "in",
8+
"schedule": "*/1 * * * * *",
9+
"runOnStartup": false
10+
},
11+
{
12+
"direction": "in",
13+
"type": "table",
14+
"name": "testEntity",
15+
"partitionKey": "test",
16+
"rowKey": "WillBePopulatedWithGuid",
17+
"tableName": "BindingTestTable",
18+
"connection": "AzureWebJobsStorage"
19+
}
20+
]
21+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
import logging
5+
6+
import azure.functions as func
7+
8+
9+
def main(mytimer: func.TimerRequest, testEntity):
10+
logging.info("Return bytes")
11+
return "test-dată"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"scriptFile": "main.py",
3+
"bindings": [
4+
{
5+
"name": "mytimer",
6+
"type": "timerTrigger",
7+
"direction": "in",
8+
"schedule": "*/1 * * * * *",
9+
"runOnStartup": false
10+
},
11+
{
12+
"direction": "in",
13+
"type": "table",
14+
"name": "testEntity",
15+
"partitionKey": "test",
16+
"rowKey": "WillBePopulatedWithGuid",
17+
"tableName": "BindingTestTable",
18+
"connection": "AzureWebJobsStorage"
19+
}
20+
]
21+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
import logging
5+
6+
import azure.functions as func
7+
8+
9+
def main(mytimer: func.TimerRequest, testEntity):
10+
logging.info("Return dict")
11+
return {"hello": "world"}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"scriptFile": "main.py",
3+
"bindings": [
4+
{
5+
"name": "mytimer",
6+
"type": "timerTrigger",
7+
"direction": "in",
8+
"schedule": "*/1 * * * * *",
9+
"runOnStartup": false
10+
},
11+
{
12+
"direction": "in",
13+
"type": "table",
14+
"name": "testEntity",
15+
"partitionKey": "test",
16+
"rowKey": "WillBePopulatedWithGuid",
17+
"tableName": "BindingTestTable",
18+
"connection": "AzureWebJobsStorage"
19+
}
20+
]
21+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
import logging
5+
6+
import azure.functions as func
7+
8+
9+
def main(mytimer: func.TimerRequest, testEntity):
10+
logging.info("Return double")
11+
return 12.34
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"scriptFile": "main.py",
3+
"bindings": [
4+
{
5+
"name": "mytimer",
6+
"type": "timerTrigger",
7+
"direction": "in",
8+
"schedule": "*/1 * * * * *",
9+
"runOnStartup": false
10+
},
11+
{
12+
"direction": "in",
13+
"type": "table",
14+
"name": "testEntity",
15+
"partitionKey": "test",
16+
"rowKey": "WillBePopulatedWithGuid",
17+
"tableName": "BindingTestTable",
18+
"connection": "AzureWebJobsStorage"
19+
}
20+
]
21+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
import logging
5+
6+
import azure.functions as func
7+
8+
9+
def main(mytimer: func.TimerRequest, testEntity):
10+
logging.info("Return int")
11+
return 12
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"scriptFile": "main.py",
3+
"bindings": [
4+
{
5+
"name": "mytimer",
6+
"type": "timerTrigger",
7+
"direction": "in",
8+
"schedule": "*/1 * * * * *",
9+
"runOnStartup": false
10+
},
11+
{
12+
"direction": "in",
13+
"type": "table",
14+
"name": "testEntity",
15+
"partitionKey": "test",
16+
"rowKey": "WillBePopulatedWithGuid",
17+
"tableName": "BindingTestTable",
18+
"connection": "AzureWebJobsStorage"
19+
}
20+
]
21+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
import logging
5+
6+
import azure.functions as func
7+
8+
9+
def main(mytimer: func.TimerRequest, testEntity):
10+
logging.info("Return list")
11+
return [1, 2, 3]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"scriptFile": "main.py",
3+
"bindings": [
4+
{
5+
"name": "mytimer",
6+
"type": "timerTrigger",
7+
"direction": "in",
8+
"schedule": "*/1 * * * * *",
9+
"runOnStartup": false
10+
},
11+
{
12+
"direction": "in",
13+
"type": "table",
14+
"name": "testEntity",
15+
"partitionKey": "test",
16+
"rowKey": "WillBePopulatedWithGuid",
17+
"tableName": "BindingTestTable",
18+
"connection": "AzureWebJobsStorage"
19+
}
20+
]
21+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
import logging
5+
6+
import azure.functions as func
7+
8+
9+
def main(mytimer: func.TimerRequest, testEntity):
10+
logging.info("Return string")
11+
return "hi!"

0 commit comments

Comments
 (0)