Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,3 @@ omit =
# Removing the imported libraries that might show up in this.
*/azure/functions/*
*/azure/*
*/azure_functions_worker/_thirdparty/*
4 changes: 0 additions & 4 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,11 @@ ignore = W503,E402,E731

exclude = .git, __pycache__, build, dist, .eggs, .github, .local, docs/,
Samples, workers/azure_functions_worker/protos/, workers/proxy_worker/protos/,
workers/azure_functions_worker/_thirdparty/typing_inspect.py,
workers/azure_functions_worker/_vendored/,
workers/r2p2/bridge/protos/,
workers/tests/unittests/test_typing_inspect.py,
workers/tests/unittests/broken_functions/syntax_error/main.py,
.env*, .vscode, venv*, *.venv*,
runtimes/v2/tests/protos/*,
runtimes/v2/azure_functions_runtime/utils/typing_inspect.py,
runtimes/v1/tests/protos/*,
runtimes/v1/azure_functions_runtime_v1/utils/typing_inspect.py

max-line-length = 88
2 changes: 0 additions & 2 deletions .github/linters/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ exclude =
Samples,
__pycache__,
azure_functions_worker/protos/,
azure_functions_worker/_thirdparty/typing_inspect.py,
azure_functions_worker/_vendored/,
tests/unittests/test_typing_inspect.py,
.venv*,
.env*,
.vscode,
Expand Down
3 changes: 2 additions & 1 deletion eng/templates/jobs/ci-unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ jobs:
if [ "$PY_MINOR" -ge 13 ]; then
echo "Running proxy_worker tests (Python >= 3.13)..."
python -m pytest -q -n auto --dist loadfile --reruns 4 --reruns-delay 5 --instafail \
--cov=./proxy_worker --cov-report xml --cov-branch tests/unittest_proxy
--cov=./proxy_worker --cov-report xml --cov-branch tests/unittest_proxy \
tests/unittests/test_binding_annotations.py
else
echo "Running unittests (Python < 3.13)..."
python -m pytest -q -n auto --dist loadfile --reruns 4 --reruns-delay 5 --instafail \
Expand Down
9 changes: 7 additions & 2 deletions runtimes/v1/azure_functions_runtime_v1/bindings/generic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import typing
from types import GenericAlias
from typing import Any, Optional

from .datumdef import Datum
Expand All @@ -14,11 +15,15 @@ def has_trigger_support(cls) -> bool:

@classmethod
def check_input_type_annotation(cls, pytype: type) -> bool:
return issubclass(pytype, (str, bytes))
return (isinstance(pytype, type)
and not isinstance(pytype, GenericAlias)
and issubclass(pytype, (str, bytes)))

@classmethod
def check_output_type_annotation(cls, pytype: type) -> bool:
return issubclass(pytype, (str, bytes, bytearray))
return (isinstance(pytype, type)
and not isinstance(pytype, GenericAlias)
and issubclass(pytype, (str, bytes, bytearray)))

@classmethod
def encode(cls, obj: Any, *,
Expand Down
9 changes: 9 additions & 0 deletions runtimes/v1/azure_functions_runtime_v1/bindings/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Licensed under the MIT License.
import os
import sys
from types import GenericAlias

from typing import Any, Dict, Optional

Expand All @@ -19,11 +20,15 @@


def _check_http_input_type_annotation(bind_name: str, pytype: type) -> bool:
if not isinstance(pytype, type) or isinstance(pytype, GenericAlias):
return False
binding = get_binding(bind_name)
return binding.check_input_type_annotation(pytype)


def _check_http_output_type_annotation(bind_name: str, pytype: type) -> bool:
if not isinstance(pytype, type) or isinstance(pytype, GenericAlias):
return False
binding = get_binding(bind_name)
return binding.check_output_type_annotation(pytype)

Expand Down Expand Up @@ -73,12 +78,16 @@ def is_trigger_binding(bind_name: str) -> bool:

def check_input_type_annotation(bind_name: str,
pytype: type) -> bool:
if bind_name == 'httpTrigger':
return _check_http_input_type_annotation(bind_name, pytype)
binding = get_binding(bind_name)

return binding.check_input_type_annotation(pytype)


def check_output_type_annotation(bind_name: str, pytype: type) -> bool:
if bind_name == 'http':
return _check_http_output_type_annotation(bind_name, pytype)
binding = get_binding(bind_name)
return binding.check_output_type_annotation(pytype)

Expand Down
77 changes: 37 additions & 40 deletions runtimes/v1/azure_functions_runtime_v1/functions.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,41 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import collections.abc
import inspect
import operator
import types
import typing

from .bindings.meta import (has_implicit_output,
check_output_type_annotation,
check_input_type_annotation)
from .utils.constants import HTTP_TRIGGER
from .utils.typing_inspect import is_generic_type, get_origin, get_args


def _is_class_type(annotation):
return (isinstance(annotation, type)
and not isinstance(annotation, types.GenericAlias))


def _is_supported_annotation(annotation):
"""Admit class-based aliases; binding converters validate their arguments."""
if _is_class_type(annotation):
return True
origin = typing.get_origin(annotation)
return (_is_class_type(origin)
and origin not in (tuple, collections.abc.Callable,
typing.Annotated, typing.Union, types.UnionType))


def _is_out_annotation(annotation):
origin = typing.get_origin(annotation)
candidate = annotation if origin is None else origin
return _is_class_type(candidate) and candidate.__name__ == 'Out'


def _annotation_name(annotation):
if typing.get_origin(annotation) is not None:
return repr(annotation)
return getattr(annotation, '__name__', repr(annotation))


class ParamTypeInfo(typing.NamedTuple):
Expand Down Expand Up @@ -146,50 +173,23 @@ def validate_function_params(params: dict, bound_params: dict,
param_has_anno = param.name in annotations
param_anno = annotations.get(param.name)

if param_has_anno:
if is_generic_type(param_anno):
param_anno_origin = get_origin(param_anno)
if param_anno_origin is not None:
is_param_out = (
isinstance(param_anno_origin, type)
and param_anno_origin.__name__ == 'Out'
)
else:
is_param_out = (
isinstance(param_anno, type)
and param_anno.__name__ == 'Out'
)
else:
is_param_out = (
isinstance(param_anno, type)
and param_anno.__name__ == 'Out'
)
else:
is_param_out = False
is_param_out = param_has_anno and _is_out_annotation(param_anno)

is_binding_out = binding.direction == protos.BindingInfo.out

if is_param_out:
param_anno_args = get_args(param_anno)
param_anno_args = typing.get_args(param_anno)
if len(param_anno_args) != 1:
raise FunctionLoadError(
func_name,
'binding ' + param.name
+ ' has invalid Out annotation ' + repr(param_anno))
param_py_type = param_anno_args[0]

# typing_inspect.get_args() returns a flat list,
# so if the annotation was func.Out[typing.List[foo]],
# we need to reconstruct it.
if (isinstance(param_py_type, tuple)
and is_generic_type(param_py_type[0])):
param_py_type = operator.getitem(
param_py_type[0], *param_py_type[1:])
else:
param_py_type = param_anno

if (param_has_anno and not isinstance(param_py_type, type)
and not is_generic_type(param_py_type)):
if param_has_anno and not _is_supported_annotation(param_py_type):
raise FunctionLoadError(
func_name,
'binding ' + param.name
Expand Down Expand Up @@ -231,13 +231,13 @@ def validate_function_params(params: dict, bound_params: dict,
+ '" and dataType "' + binding.type + '" in '
'function.json do not match the corresponding '
'function parameter\'s Python type '
'annotation ' + param_py_type.__name__)
'annotation ' + _annotation_name(param_py_type))
else:
raise FunctionLoadError(
func_name,
'type of ' + param.name + ' binding in function.json "'
+ binding.type + '" does not match its Python '
'annotation "' + param_py_type.__name__ + '"')
'annotation "' + _annotation_name(param_py_type) + '"')

param_type_info = ParamTypeInfo(param_bind_type,
param_py_type)
Expand All @@ -254,16 +254,13 @@ def get_function_return_type(annotations: dict, has_explicit_return: bool,
return_pytype = None
if has_explicit_return and 'return' in annotations:
return_anno = annotations.get('return')
if is_generic_type(
return_anno) and get_origin(
return_anno) is not None and get_origin(
return_anno).__name__ == 'Out':
if _is_out_annotation(return_anno):
raise FunctionLoadError(
func_name,
'return annotation should not be azure.functions.Out')

return_pytype = return_anno
if not isinstance(return_pytype, type):
if not _is_supported_annotation(return_pytype):
raise FunctionLoadError(
func_name,
'has invalid non-type return '
Expand All @@ -276,7 +273,7 @@ def get_function_return_type(annotations: dict, has_explicit_return: bool,
binding_name, return_pytype):
raise FunctionLoadError(
func_name,
'Python return annotation "' + return_pytype.__name__
'Python return annotation "' + _annotation_name(return_pytype)
+ '" does not match binding type "' + binding_name + '"')

if has_implicit_return and 'return' in annotations:
Expand Down
Loading
Loading