diff --git a/.coveragerc b/.coveragerc index a3dbea58b..c524c38bb 100644 --- a/.coveragerc +++ b/.coveragerc @@ -49,4 +49,3 @@ omit = # Removing the imported libraries that might show up in this. */azure/functions/* */azure/* - */azure_functions_worker/_thirdparty/* diff --git a/.flake8 b/.flake8 index cbbf6ef88..5cfd87652 100644 --- a/.flake8 +++ b/.flake8 @@ -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 \ No newline at end of file diff --git a/.github/linters/tox.ini b/.github/linters/tox.ini index be81037b2..acb0c986a 100644 --- a/.github/linters/tox.ini +++ b/.github/linters/tox.ini @@ -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, diff --git a/eng/templates/jobs/ci-unit-tests.yml b/eng/templates/jobs/ci-unit-tests.yml index 57a4925ae..333d10436 100644 --- a/eng/templates/jobs/ci-unit-tests.yml +++ b/eng/templates/jobs/ci-unit-tests.yml @@ -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 \ diff --git a/runtimes/v1/azure_functions_runtime_v1/bindings/generic.py b/runtimes/v1/azure_functions_runtime_v1/bindings/generic.py index edcc28218..20cfcdd53 100644 --- a/runtimes/v1/azure_functions_runtime_v1/bindings/generic.py +++ b/runtimes/v1/azure_functions_runtime_v1/bindings/generic.py @@ -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 @@ -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, *, diff --git a/runtimes/v1/azure_functions_runtime_v1/bindings/meta.py b/runtimes/v1/azure_functions_runtime_v1/bindings/meta.py index 92e2b3f1b..b2333273f 100644 --- a/runtimes/v1/azure_functions_runtime_v1/bindings/meta.py +++ b/runtimes/v1/azure_functions_runtime_v1/bindings/meta.py @@ -2,6 +2,7 @@ # Licensed under the MIT License. import os import sys +from types import GenericAlias from typing import Any, Dict, Optional @@ -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) @@ -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) diff --git a/runtimes/v1/azure_functions_runtime_v1/functions.py b/runtimes/v1/azure_functions_runtime_v1/functions.py index 580f21e07..035a756d6 100644 --- a/runtimes/v1/azure_functions_runtime_v1/functions.py +++ b/runtimes/v1/azure_functions_runtime_v1/functions.py @@ -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): @@ -146,31 +173,12 @@ 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, @@ -178,18 +186,10 @@ def validate_function_params(params: dict, bound_params: dict, + ' 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 @@ -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) @@ -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 ' @@ -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: diff --git a/runtimes/v1/azure_functions_runtime_v1/utils/typing_inspect.py b/runtimes/v1/azure_functions_runtime_v1/utils/typing_inspect.py deleted file mode 100644 index 27ba19da8..000000000 --- a/runtimes/v1/azure_functions_runtime_v1/utils/typing_inspect.py +++ /dev/null @@ -1,294 +0,0 @@ -# Imported from https://github.com/ilevkivskyi/typing_inspect/blob/168fa6f7c5c55f720ce6282727211cf4cf6368f6/typing_inspect.py # NoQA E501 -# Author: Ivan Levkivskyi -# License: MIT - -"""Defines experimental API for runtime inspection of types defined -in the standard "typing" module. - -Example usage:: - from typing_inspect import is_generic_type -""" - -# NOTE: This module must support Python 2.7 in addition to Python 3.x - -import collections.abc -import sys -from typing import Callable, ClassVar, Generic, Tuple, TypeVar, Union, _GenericAlias, _SpecialGenericAlias - - -# from mypy_extensions import _TypedDictMeta - -def is_generic_type(tp): - """Test if the given type is a generic type. This includes Generic itself, - but excludes special typing constructs such as Union, Tuple, Callable, - ClassVar. - Examples:: - - is_generic_type(int) == False - is_generic_type(Union[int, str]) == False - is_generic_type(Union[int, T]) == False - is_generic_type(ClassVar[List[int]]) == False - is_generic_type(Callable[..., T]) == False - is_generic_type(Generic) == True - is_generic_type(Generic[T]) == True - is_generic_type(Iterable[int]) == True - is_generic_type(Mapping) == True - is_generic_type(MutableMapping[T, List[int]]) == True - is_generic_type(Sequence[Union[str, bytes]]) == True - """ - return (isinstance(tp, type) and issubclass(tp, Generic) - or ((isinstance(tp, _GenericAlias) or isinstance(tp, _SpecialGenericAlias)) # NoQA E501 - and tp.__origin__ not in (Union, tuple, ClassVar, collections.abc.Callable))) # NoQA E501 - - -def is_callable_type(tp): - """Test if the type is a generic callable type, including subclasses - excluding non-generic types and callables. - Examples:: - - is_callable_type(int) == False - is_callable_type(type) == False - is_callable_type(Callable) == True - is_callable_type(Callable[..., int]) == True - is_callable_type(Callable[[int, int], Iterable[str]]) == True - class MyClass(Callable[[int], int]): - ... - is_callable_type(MyClass) == True - - For more general tests use callable(), for more precise test - (excluding subclasses) use:: - - get_origin(tp) is collections.abc.Callable # Callable prior to Python 3.7 # NoQA E501 - """ - return (tp is Callable or isinstance(tp, _GenericAlias) and - tp.__origin__ is collections.abc.Callable or - isinstance(tp, type) and issubclass(tp, Generic) and - issubclass(tp, collections.abc.Callable)) - - -def is_tuple_type(tp): - """Test if the type is a generic tuple type, including subclasses excluding - non-generic classes. - Examples:: - - is_tuple_type(int) == False - is_tuple_type(tuple) == False - is_tuple_type(Tuple) == True - is_tuple_type(Tuple[str, int]) == True - class MyClass(Tuple[str, int]): - ... - is_tuple_type(MyClass) == True - - For more general tests use issubclass(..., tuple), for more precise test - (excluding subclasses) use:: - - get_origin(tp) is tuple # Tuple prior to Python 3.7 - """ - return (tp is Tuple or isinstance(tp, _GenericAlias) and - tp.__origin__ is tuple or - isinstance(tp, type) and issubclass(tp, Generic) and - issubclass(tp, tuple)) - - -def is_union_type(tp): - """Test if the type is a union type. Examples:: - - is_union_type(int) == False - is_union_type(Union) == True - is_union_type(Union[int, int]) == False - is_union_type(Union[T, int]) == True - """ - return (tp is Union or - isinstance(tp, _GenericAlias) and tp.__origin__ is Union) - - -def is_typevar(tp): - """Test if the type represents a type variable. Examples:: - - is_typevar(int) == False - is_typevar(T) == True - is_typevar(Union[T, int]) == False - """ - - return type(tp) is TypeVar - - -def is_classvar(tp): - """Test if the type represents a class variable. Examples:: - - is_classvar(int) == False - is_classvar(ClassVar) == True - is_classvar(ClassVar[int]) == True - is_classvar(ClassVar[List[T]]) == True - """ - return (tp is ClassVar or - isinstance(tp, _GenericAlias) and tp.__origin__ is ClassVar) - - -def get_last_origin(tp): - """Get the last base of (multiply) subscripted type. Supports generic types, - Union, Callable, and Tuple. Returns None for unsupported types. - Examples:: - - get_last_origin(int) == None - get_last_origin(ClassVar[int]) == None - get_last_origin(Generic[T]) == Generic - get_last_origin(Union[T, int][str]) == Union[T, int] - get_last_origin(List[Tuple[T, T]][int]) == List[Tuple[T, T]] - get_last_origin(List) == List - """ - raise ValueError('This function is only supported in Python 3.6,' - ' use get_origin instead') - - -def get_origin(tp): - """Get the unsubscripted version of a type. Supports generic types, Union, - Callable, and Tuple. Returns None for unsupported types. Examples:: - - get_origin(int) == None - get_origin(ClassVar[int]) == None - get_origin(Generic) == Generic - get_origin(Generic[T]) == Generic - get_origin(Union[T, int]) == Union - get_origin(List[Tuple[T, T]][int]) == list # List prior to Python 3.7 - """ - if isinstance(tp, _GenericAlias): - return tp.__origin__ if tp.__origin__ is not ClassVar else None - if tp is Generic: - return Generic - return None - - -def get_parameters(tp): - """Return type parameters of a parameterizable type as a tuple - in lexicographic order. Parameterizable types are generic types, - unions, tuple types and callable types. Examples:: - - get_parameters(int) == () - get_parameters(Generic) == () - get_parameters(Union) == () - get_parameters(List[int]) == () - - get_parameters(Generic[T]) == (T,) - get_parameters(Tuple[List[T], List[S_co]]) == (T, S_co) - get_parameters(Union[S_co, Tuple[T, T]][int, U]) == (U,) - get_parameters(Mapping[T, Tuple[S_co, T]]) == (T, S_co) - """ - if (isinstance(tp, _GenericAlias) or isinstance(tp, type) and - issubclass(tp, Generic) and tp is not Generic): # NoQA E129 - return tp.__parameters__ - return () - - -def get_last_args(tp): - """Get last arguments of (multiply) subscripted type. - Parameters for Callable are flattened. Examples:: - - get_last_args(int) == () - get_last_args(Union) == () - get_last_args(ClassVar[int]) == (int,) - get_last_args(Union[T, int]) == (T, int) - get_last_args(Iterable[Tuple[T, S]][int, T]) == (int, T) - get_last_args(Callable[[T], int]) == (T, int) - get_last_args(Callable[[], int]) == (int,) - """ - raise ValueError('This function is only supported in Python 3.6,' - ' use get_args instead') - - -def _eval_args(args): - """Internal helper for get_args.""" - res = [] - for arg in args: - if not isinstance(arg, tuple): - res.append(arg) - elif is_callable_type(arg[0]): - if len(arg) == 2: - res.append(Callable[[], arg[1]]) - elif arg[1] is Ellipsis: - res.append(Callable[..., arg[2]]) - else: - res.append(Callable[list(arg[1:-1]), arg[-1]]) - else: - res.append(type(arg[0]).__getitem__(arg[0], _eval_args(arg[1:]))) - return tuple(res) - - -def get_args(tp, evaluate=None): - """Get type arguments with all substitutions performed. For unions, - basic simplifications used by Union constructor are performed. - On versions prior to 3.7 if `evaluate` is False (default), - report result as nested tuple, this matches - the internal representation of types. If `evaluate` is True - (or if Python version is 3.7 or greater), then all - type parameters are applied (this could be time and memory expensive). - Examples:: - - get_args(int) == () - get_args(Union[int, Union[T, int], str][int]) == (int, str) - get_args(Union[int, Tuple[T, int]][str]) == (int, (Tuple, str, int)) - - get_args(Union[int, Tuple[T, int]][str], evaluate=True) == \ - (int, Tuple[str, int]) - get_args(Dict[int, Tuple[T, T]][Optional[int]], evaluate=True) == \ - (int, Tuple[Optional[int], Optional[int]]) - get_args(Callable[[], T][int], evaluate=True) == ([], int,) - """ - if evaluate is not None and not evaluate: - raise ValueError('evaluate can only be True in Python 3.7') - if isinstance(tp, _GenericAlias): - res = tp.__args__ - if get_origin(tp) is collections.abc.Callable and res[0] is not Ellipsis: # NoQA E501 - res = (list(res[:-1]), res[-1]) - return res - return () - - -def get_generic_type(obj): - """Get the generic type of an object if possible, or runtime class - otherwise. - Examples:: - - class Node(Generic[T]): - ... - type(Node[int]()) == Node - get_generic_type(Node[int]()) == Node[int] - get_generic_type(Node[T]()) == Node[T] - get_generic_type(1) == int - """ - - gen_type = getattr(obj, '__orig_class__', None) - return gen_type if gen_type is not None else type(obj) - - -def get_generic_bases(tp): - """Get generic base types of a type or empty tuple if not possible. - Example:: - - class MyClass(List[int], Mapping[str, List[int]]): - ... - MyClass.__bases__ == (List, Mapping) - get_generic_bases(MyClass) == (List[int], Mapping[str, List[int]]) - """ - - return getattr(tp, '__orig_bases__', ()) - - -def typed_dict_keys(td): - """If td is a TypedDict class, return a dictionary mapping the typed keys - to types. Otherwise, return None. Examples:: - - class TD(TypedDict): - x: int - y: int - class Other(dict): - x: int - y: int - - typed_dict_keys(TD) == {'x': int, 'y': int} - typed_dict_keys(dict) == None - typed_dict_keys(Other) == None - """ - if isinstance(td, _TypedDictMeta): # NoQA F821 - return td.__annotations__.copy() - return None diff --git a/runtimes/v1/tests/unittests/test_typing_inspect.py b/runtimes/v1/tests/unittests/test_typing_inspect.py deleted file mode 100644 index d13eb89a2..000000000 --- a/runtimes/v1/tests/unittests/test_typing_inspect.py +++ /dev/null @@ -1,142 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. -import unittest - -from typing import ( - Any, - Callable, - ClassVar, - Dict, - Generic, - Iterable, - List, - Mapping, - MutableMapping, - NamedTuple, - Optional, - Sequence, - Tuple, - TypeVar, - Union, -) -from azure_functions_runtime_v1.utils.typing_inspect import ( - get_args, - get_generic_bases, - get_generic_type, - get_origin, - get_parameters, - is_callable_type, - is_classvar, - is_generic_type, - is_tuple_type, - is_typevar, - is_union_type, -) - - -class IsUtilityTestCase(unittest.TestCase): - def sample_test(self, fun, samples, nonsamples): - for s in samples: - self.assertTrue(fun(s), f"{s} type expected in {samples}") - for s in nonsamples: - self.assertFalse(fun(s), f"{s} type expected in {nonsamples}") - - def test_generic(self): - T = TypeVar('T') - samples = [Generic, Generic[T], Iterable[int], Mapping, - MutableMapping[T, List[int]], Sequence[Union[str, bytes]]] - nonsamples = [int, Union[int, str], Union[int, T], ClassVar[List[int]], - Callable[..., T], ClassVar, Optional, bytes, list] - self.sample_test(is_generic_type, samples, nonsamples) - - def test_callable(self): - class MyClass(Callable[[int], int]): - pass - samples = [Callable, Callable[..., int], - Callable[[int, int], Iterable[str]]] - nonsamples = [int, type, 42, [], List[int], - Union[callable, Callable[..., int]]] - self.sample_test(is_callable_type, samples, nonsamples) - self.assertTrue(is_callable_type(MyClass)) - - def test_tuple(self): - class MyClass(Tuple[str, int]): - pass - samples = [Tuple, Tuple[str, int], Tuple[Iterable, ...]] - nonsamples = [int, tuple, 42, List[int], NamedTuple('N', [('x', int)])] - self.sample_test(is_tuple_type, samples, nonsamples) - self.assertTrue(is_tuple_type(MyClass)) - - def test_union(self): - T = TypeVar('T') - S = TypeVar('S') - samples = [Union, Union[T, int], Union[int, Union[T, S]]] - nonsamples = [int, Union[int, int], [], Iterable[Any]] - self.sample_test(is_union_type, samples, nonsamples) - - def test_typevar(self): - T = TypeVar('T') - S_co = TypeVar('S_co', covariant=True) - samples = [T, S_co] - nonsamples = [int, Union[T, int], Union[T, S_co], type, ClassVar[int]] - self.sample_test(is_typevar, samples, nonsamples) - - def test_classvar(self): - T = TypeVar('T') - samples = [ClassVar, ClassVar[int], ClassVar[List[T]]] - nonsamples = [int, 42, Iterable, List[int], type, T] - self.sample_test(is_classvar, samples, nonsamples) - - -class GetUtilityTestCase(unittest.TestCase): - - def test_origin(self): - T = TypeVar('T') - - class MyClass(Generic[T]): - pass - - self.assertEqual(get_origin(int), None) - self.assertEqual(get_origin(ClassVar[int]), None) - self.assertEqual(get_origin(Generic), Generic) - self.assertEqual(get_origin(Generic[T]), Generic) - self.assertEqual(get_origin(List[Tuple[T, T]][int]), list) - self.assertEqual(get_origin(MyClass), None) - - def test_parameters(self): - T = TypeVar('T') - S_co = TypeVar('S_co', covariant=True) - U = TypeVar('U') - self.assertEqual(get_parameters(int), ()) - self.assertEqual(get_parameters(Generic), ()) - self.assertEqual(get_parameters(Union), ()) - self.assertEqual(get_parameters(List[int]), ()) - self.assertEqual(get_parameters(Generic[T]), (T,)) - self.assertEqual(get_parameters(Tuple[List[T], List[S_co]]), (T, S_co)) - self.assertEqual(get_parameters(Union[S_co, Tuple[T, T]][int, U]), (U,)) - self.assertEqual(get_parameters(Mapping[T, Tuple[S_co, T]]), (T, S_co)) - - def test_args_evaluated(self): - T = TypeVar('T') - self.assertEqual(get_args(Union[int, Tuple[T, int]][str], evaluate=True), - (int, Tuple[str, int])) - self.assertEqual(get_args(Dict[int, Tuple[T, T]][Optional[int]], evaluate=True), - (int, Tuple[Optional[int], Optional[int]])) - self.assertEqual(get_args(Callable[[], T][int], evaluate=True), ([], int,)) - - def test_generic_type(self): - T = TypeVar('T') - - class Node(Generic[T]): - pass - self.assertIs(get_generic_type(Node()), Node) - self.assertIs(get_generic_type(Node[int]()), Node[int]) - self.assertIs(get_generic_type(Node[T]()), Node[T],) - self.assertIs(get_generic_type(1), int) - - def test_generic_bases(self): - class MyClass(List[int], Mapping[str, List[int]]): - pass - self.assertEqual(get_generic_bases(MyClass), - (List[int], Mapping[str, List[int]])) - self.assertEqual(get_generic_bases(int), ()) diff --git a/runtimes/v2/azure_functions_runtime/bindings/generic.py b/runtimes/v2/azure_functions_runtime/bindings/generic.py index ad41b2e82..7e64752e4 100644 --- a/runtimes/v2/azure_functions_runtime/bindings/generic.py +++ b/runtimes/v2/azure_functions_runtime/bindings/generic.py @@ -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 @@ -15,11 +16,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, *, diff --git a/runtimes/v2/azure_functions_runtime/bindings/meta.py b/runtimes/v2/azure_functions_runtime/bindings/meta.py index 3c429aece..c98a33bb7 100644 --- a/runtimes/v2/azure_functions_runtime/bindings/meta.py +++ b/runtimes/v2/azure_functions_runtime/bindings/meta.py @@ -3,6 +3,7 @@ # mypy: disable-error-code="attr-defined" import os import sys +from types import GenericAlias from typing import Any, Dict, Optional, Union @@ -35,6 +36,8 @@ def _check_http_input_type_annotation(bind_name: str, pytype: type, is_deferred_binding: bool) -> bool: + if not isinstance(pytype, type) or isinstance(pytype, GenericAlias): + return False if HttpV2Registry.http_v2_enabled(): return HttpV2Registry.ext_base().RequestTrackerMeta \ .check_type(pytype) @@ -44,6 +47,8 @@ def _check_http_input_type_annotation(bind_name: str, pytype: type, def _check_http_output_type_annotation(bind_name: str, pytype: type) -> bool: + if not isinstance(pytype, type) or isinstance(pytype, GenericAlias): + return False if HttpV2Registry.http_v2_enabled(): return HttpV2Registry.ext_base().ResponseTrackerMeta.check_type(pytype) diff --git a/runtimes/v2/azure_functions_runtime/functions.py b/runtimes/v2/azure_functions_runtime/functions.py index ac9692658..f0798050d 100644 --- a/runtimes/v2/azure_functions_runtime/functions.py +++ b/runtimes/v2/azure_functions_runtime/functions.py @@ -1,8 +1,9 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. +import collections.abc import inspect -import operator import pathlib +import types import typing import uuid @@ -14,7 +15,33 @@ check_input_type_annotation, validate_settlement_param) from .utils.constants import HTTP_TRIGGER -from .utils.typing_inspect import is_generic_type, get_origin, get_args # type: ignore + + +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): @@ -181,31 +208,12 @@ def validate_function_params(params: dict, bound_params: dict, param_anno, fx_deferred_bindings_enabled)) - 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, @@ -213,20 +221,12 @@ def validate_function_params(params: dict, bound_params: dict, + ' 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 logger.debug("Param_py_type %s", param_py_type) - 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 @@ -273,13 +273,14 @@ def validate_function_params(params: dict, bound_params: dict, + '" and dataType "' + binding.type + '" in ' 'the binding decorator 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 the binding ' 'decorator "' + binding.type + '" does not match its ' - 'Python annotation "' + param_py_type.__name__ + '"') + 'Python annotation "' + + _annotation_name(param_py_type) + '"') param_type_info = ParamTypeInfo(param_bind_type, param_py_type, @@ -298,16 +299,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 ' @@ -320,7 +318,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: diff --git a/runtimes/v2/azure_functions_runtime/utils/typing_inspect.py b/runtimes/v2/azure_functions_runtime/utils/typing_inspect.py deleted file mode 100644 index 67726ee3a..000000000 --- a/runtimes/v2/azure_functions_runtime/utils/typing_inspect.py +++ /dev/null @@ -1,295 +0,0 @@ -# type: ignore -# Imported from https://github.com/ilevkivskyi/typing_inspect/blob/168fa6f7c5c55f720ce6282727211cf4cf6368f6/typing_inspect.py # NoQA E501 -# Author: Ivan Levkivskyi -# License: MIT - -"""Defines experimental API for runtime inspection of types defined -in the standard "typing" module. - -Example usage:: - from typing_inspect import is_generic_type -""" - -# NOTE: This module must support Python 2.7 in addition to Python 3.x - -import collections.abc -from typing import (Callable, ClassVar, Generic, Tuple, - TypeVar, Union, _GenericAlias, _SpecialGenericAlias) - - -# from mypy_extensions import _TypedDictMeta - -def is_generic_type(tp): - """Test if the given type is a generic type. This includes Generic itself, - but excludes special typing constructs such as Union, Tuple, Callable, - ClassVar. - Examples:: - - is_generic_type(int) == False - is_generic_type(Union[int, str]) == False - is_generic_type(Union[int, T]) == False - is_generic_type(ClassVar[List[int]]) == False - is_generic_type(Callable[..., T]) == False - is_generic_type(Generic) == True - is_generic_type(Generic[T]) == True - is_generic_type(Iterable[int]) == True - is_generic_type(Mapping) == True - is_generic_type(MutableMapping[T, List[int]]) == True - is_generic_type(Sequence[Union[str, bytes]]) == True - """ - return (isinstance(tp, type) and issubclass(tp, Generic) - or ((isinstance(tp, _GenericAlias) or isinstance(tp, _SpecialGenericAlias)) # NoQA E501 - and tp.__origin__ not in (Union, tuple, ClassVar, collections.abc.Callable))) # NoQA E501 - - -def is_callable_type(tp): - """Test if the type is a generic callable type, including subclasses - excluding non-generic types and callables. - Examples:: - - is_callable_type(int) == False - is_callable_type(type) == False - is_callable_type(Callable) == True - is_callable_type(Callable[..., int]) == True - is_callable_type(Callable[[int, int], Iterable[str]]) == True - class MyClass(Callable[[int], int]): - ... - is_callable_type(MyClass) == True - - For more general tests use callable(), for more precise test - (excluding subclasses) use:: - - get_origin(tp) is collections.abc.Callable # Callable prior to Python 3.7 # NoQA E501 - """ - return (tp is Callable or isinstance(tp, _GenericAlias) and - tp.__origin__ is collections.abc.Callable or - isinstance(tp, type) and issubclass(tp, Generic) and - issubclass(tp, collections.abc.Callable)) - - -def is_tuple_type(tp): - """Test if the type is a generic tuple type, including subclasses excluding - non-generic classes. - Examples:: - - is_tuple_type(int) == False - is_tuple_type(tuple) == False - is_tuple_type(Tuple) == True - is_tuple_type(Tuple[str, int]) == True - class MyClass(Tuple[str, int]): - ... - is_tuple_type(MyClass) == True - - For more general tests use issubclass(..., tuple), for more precise test - (excluding subclasses) use:: - - get_origin(tp) is tuple # Tuple prior to Python 3.7 - """ - return (tp is Tuple or isinstance(tp, _GenericAlias) and - tp.__origin__ is tuple or - isinstance(tp, type) and issubclass(tp, Generic) and - issubclass(tp, tuple)) - - -def is_union_type(tp): - """Test if the type is a union type. Examples:: - - is_union_type(int) == False - is_union_type(Union) == True - is_union_type(Union[int, int]) == False - is_union_type(Union[T, int]) == True - """ - return (tp is Union or - isinstance(tp, _GenericAlias) and tp.__origin__ is Union) - - -def is_typevar(tp): - """Test if the type represents a type variable. Examples:: - - is_typevar(int) == False - is_typevar(T) == True - is_typevar(Union[T, int]) == False - """ - - return type(tp) is TypeVar - - -def is_classvar(tp): - """Test if the type represents a class variable. Examples:: - - is_classvar(int) == False - is_classvar(ClassVar) == True - is_classvar(ClassVar[int]) == True - is_classvar(ClassVar[List[T]]) == True - """ - return (tp is ClassVar or - isinstance(tp, _GenericAlias) and tp.__origin__ is ClassVar) - - -def get_last_origin(tp): - """Get the last base of (multiply) subscripted type. Supports generic types, - Union, Callable, and Tuple. Returns None for unsupported types. - Examples:: - - get_last_origin(int) == None - get_last_origin(ClassVar[int]) == None - get_last_origin(Generic[T]) == Generic - get_last_origin(Union[T, int][str]) == Union[T, int] - get_last_origin(List[Tuple[T, T]][int]) == List[Tuple[T, T]] - get_last_origin(List) == List - """ - raise ValueError('This function is only supported in Python 3.6,' - ' use get_origin instead') - - -def get_origin(tp): - """Get the unsubscripted version of a type. Supports generic types, Union, - Callable, and Tuple. Returns None for unsupported types. Examples:: - - get_origin(int) == None - get_origin(ClassVar[int]) == None - get_origin(Generic) == Generic - get_origin(Generic[T]) == Generic - get_origin(Union[T, int]) == Union - get_origin(List[Tuple[T, T]][int]) == list # List prior to Python 3.7 - """ - if isinstance(tp, _GenericAlias): - return tp.__origin__ if tp.__origin__ is not ClassVar else None - if tp is Generic: - return Generic - return None - - -def get_parameters(tp): - """Return type parameters of a parameterizable type as a tuple - in lexicographic order. Parameterizable types are generic types, - unions, tuple types and callable types. Examples:: - - get_parameters(int) == () - get_parameters(Generic) == () - get_parameters(Union) == () - get_parameters(List[int]) == () - - get_parameters(Generic[T]) == (T,) - get_parameters(Tuple[List[T], List[S_co]]) == (T, S_co) - get_parameters(Union[S_co, Tuple[T, T]][int, U]) == (U,) - get_parameters(Mapping[T, Tuple[S_co, T]]) == (T, S_co) - """ - if (isinstance(tp, _GenericAlias) or isinstance(tp, type) and - issubclass(tp, Generic) and tp is not Generic): # NoQA E129 - return tp.__parameters__ - return () - - -def get_last_args(tp): - """Get last arguments of (multiply) subscripted type. - Parameters for Callable are flattened. Examples:: - - get_last_args(int) == () - get_last_args(Union) == () - get_last_args(ClassVar[int]) == (int,) - get_last_args(Union[T, int]) == (T, int) - get_last_args(Iterable[Tuple[T, S]][int, T]) == (int, T) - get_last_args(Callable[[T], int]) == (T, int) - get_last_args(Callable[[], int]) == (int,) - """ - raise ValueError('This function is only supported in Python 3.6,' - ' use get_args instead') - - -def _eval_args(args): - """Internal helper for get_args.""" - res = [] - for arg in args: - if not isinstance(arg, tuple): - res.append(arg) - elif is_callable_type(arg[0]): - if len(arg) == 2: - res.append(Callable[[], arg[1]]) - elif arg[1] is Ellipsis: - res.append(Callable[..., arg[2]]) - else: - res.append(Callable[list(arg[1:-1]), arg[-1]]) - else: - res.append(type(arg[0]).__getitem__(arg[0], _eval_args(arg[1:]))) - return tuple(res) - - -def get_args(tp, evaluate=None): - """Get type arguments with all substitutions performed. For unions, - basic simplifications used by Union constructor are performed. - On versions prior to 3.7 if `evaluate` is False (default), - report result as nested tuple, this matches - the internal representation of types. If `evaluate` is True - (or if Python version is 3.7 or greater), then all - type parameters are applied (this could be time and memory expensive). - Examples:: - - get_args(int) == () - get_args(Union[int, Union[T, int], str][int]) == (int, str) - get_args(Union[int, Tuple[T, int]][str]) == (int, (Tuple, str, int)) - - get_args(Union[int, Tuple[T, int]][str], evaluate=True) == \ - (int, Tuple[str, int]) - get_args(Dict[int, Tuple[T, T]][Optional[int]], evaluate=True) == \ - (int, Tuple[Optional[int], Optional[int]]) - get_args(Callable[[], T][int], evaluate=True) == ([], int,) - """ - if evaluate is not None and not evaluate: - raise ValueError('evaluate can only be True in Python 3.7') - if isinstance(tp, _GenericAlias): - res = tp.__args__ - if get_origin(tp) is collections.abc.Callable and res[0] is not Ellipsis: # NoQA E501 - res = (list(res[:-1]), res[-1]) - return res - return () - - -def get_generic_type(obj): - """Get the generic type of an object if possible, or runtime class - otherwise. - Examples:: - - class Node(Generic[T]): - ... - type(Node[int]()) == Node - get_generic_type(Node[int]()) == Node[int] - get_generic_type(Node[T]()) == Node[T] - get_generic_type(1) == int - """ - - gen_type = getattr(obj, '__orig_class__', None) - return gen_type if gen_type is not None else type(obj) - - -def get_generic_bases(tp): - """Get generic base types of a type or empty tuple if not possible. - Example:: - - class MyClass(List[int], Mapping[str, List[int]]): - ... - MyClass.__bases__ == (List, Mapping) - get_generic_bases(MyClass) == (List[int], Mapping[str, List[int]]) - """ - - return getattr(tp, '__orig_bases__', ()) - - -def typed_dict_keys(td): - """If td is a TypedDict class, return a dictionary mapping the typed keys - to types. Otherwise, return None. Examples:: - - class TD(TypedDict): - x: int - y: int - class Other(dict): - x: int - y: int - - typed_dict_keys(TD) == {'x': int, 'y': int} - typed_dict_keys(dict) == None - typed_dict_keys(Other) == None - """ - if isinstance(td, _TypedDictMeta): # NoQA F821 - return td.__annotations__.copy() - return None diff --git a/runtimes/v2/tests/unittests/test_typing_inspect.py b/runtimes/v2/tests/unittests/test_typing_inspect.py deleted file mode 100644 index 1d77f13d8..000000000 --- a/runtimes/v2/tests/unittests/test_typing_inspect.py +++ /dev/null @@ -1,142 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. -import unittest - -from typing import ( - Any, - Callable, - ClassVar, - Dict, - Generic, - Iterable, - List, - Mapping, - MutableMapping, - NamedTuple, - Optional, - Sequence, - Tuple, - TypeVar, - Union, -) -from azure_functions_runtime.utils.typing_inspect import ( - get_args, - get_generic_bases, - get_generic_type, - get_origin, - get_parameters, - is_callable_type, - is_classvar, - is_generic_type, - is_tuple_type, - is_typevar, - is_union_type, -) - - -class IsUtilityTestCase(unittest.TestCase): - def sample_test(self, fun, samples, nonsamples): - for s in samples: - self.assertTrue(fun(s), f"{s} type expected in {samples}") - for s in nonsamples: - self.assertFalse(fun(s), f"{s} type expected in {nonsamples}") - - def test_generic(self): - T = TypeVar('T') - samples = [Generic, Generic[T], Iterable[int], Mapping, - MutableMapping[T, List[int]], Sequence[Union[str, bytes]]] - nonsamples = [int, Union[int, str], Union[int, T], ClassVar[List[int]], - Callable[..., T], ClassVar, Optional, bytes, list] - self.sample_test(is_generic_type, samples, nonsamples) - - def test_callable(self): - class MyClass(Callable[[int], int]): - pass - samples = [Callable, Callable[..., int], - Callable[[int, int], Iterable[str]]] - nonsamples = [int, type, 42, [], List[int], - Union[callable, Callable[..., int]]] - self.sample_test(is_callable_type, samples, nonsamples) - self.assertTrue(is_callable_type(MyClass)) - - def test_tuple(self): - class MyClass(Tuple[str, int]): - pass - samples = [Tuple, Tuple[str, int], Tuple[Iterable, ...]] - nonsamples = [int, tuple, 42, List[int], NamedTuple('N', [('x', int)])] - self.sample_test(is_tuple_type, samples, nonsamples) - self.assertTrue(is_tuple_type(MyClass)) - - def test_union(self): - T = TypeVar('T') - S = TypeVar('S') - samples = [Union, Union[T, int], Union[int, Union[T, S]]] - nonsamples = [int, Union[int, int], [], Iterable[Any]] - self.sample_test(is_union_type, samples, nonsamples) - - def test_typevar(self): - T = TypeVar('T') - S_co = TypeVar('S_co', covariant=True) - samples = [T, S_co] - nonsamples = [int, Union[T, int], Union[T, S_co], type, ClassVar[int]] - self.sample_test(is_typevar, samples, nonsamples) - - def test_classvar(self): - T = TypeVar('T') - samples = [ClassVar, ClassVar[int], ClassVar[List[T]]] - nonsamples = [int, 42, Iterable, List[int], type, T] - self.sample_test(is_classvar, samples, nonsamples) - - -class GetUtilityTestCase(unittest.TestCase): - - def test_origin(self): - T = TypeVar('T') - - class MyClass(Generic[T]): - pass - - self.assertEqual(get_origin(int), None) - self.assertEqual(get_origin(ClassVar[int]), None) - self.assertEqual(get_origin(Generic), Generic) - self.assertEqual(get_origin(Generic[T]), Generic) - self.assertEqual(get_origin(List[Tuple[T, T]][int]), list) - self.assertEqual(get_origin(MyClass), None) - - def test_parameters(self): - T = TypeVar('T') - S_co = TypeVar('S_co', covariant=True) - U = TypeVar('U') - self.assertEqual(get_parameters(int), ()) - self.assertEqual(get_parameters(Generic), ()) - self.assertEqual(get_parameters(Union), ()) - self.assertEqual(get_parameters(List[int]), ()) - self.assertEqual(get_parameters(Generic[T]), (T,)) - self.assertEqual(get_parameters(Tuple[List[T], List[S_co]]), (T, S_co)) - self.assertEqual(get_parameters(Union[S_co, Tuple[T, T]][int, U]), (U,)) - self.assertEqual(get_parameters(Mapping[T, Tuple[S_co, T]]), (T, S_co)) - - def test_args_evaluated(self): - T = TypeVar('T') - self.assertEqual(get_args(Union[int, Tuple[T, int]][str], evaluate=True), - (int, Tuple[str, int])) - self.assertEqual(get_args(Dict[int, Tuple[T, T]][Optional[int]], evaluate=True), - (int, Tuple[Optional[int], Optional[int]])) - self.assertEqual(get_args(Callable[[], T][int], evaluate=True), ([], int,)) - - def test_generic_type(self): - T = TypeVar('T') - - class Node(Generic[T]): - pass - self.assertIs(get_generic_type(Node()), Node) - self.assertIs(get_generic_type(Node[int]()), Node[int]) - self.assertIs(get_generic_type(Node[T]()), Node[T],) - self.assertIs(get_generic_type(1), int) - - def test_generic_bases(self): - class MyClass(List[int], Mapping[str, List[int]]): - pass - self.assertEqual(get_generic_bases(MyClass), - (List[int], Mapping[str, List[int]])) - self.assertEqual(get_generic_bases(int), ()) diff --git a/setup.cfg b/setup.cfg index 2fe4fd01b..a119f0054 100644 --- a/setup.cfg +++ b/setup.cfg @@ -16,11 +16,5 @@ ignore_missing_imports = True [mypy-azure_functions_worker.protos.*] ignore_errors = True -[mypy-azure_functions_worker._thirdparty.typing_inspect] -ignore_errors = True - [mypy-proxy_worker.protos.*] -ignore_errors = True - -[mypy-proxy_worker._thirdparty.typing_inspect] ignore_errors = True \ No newline at end of file diff --git a/workers/azure_functions_worker/_thirdparty/__init__.py b/workers/azure_functions_worker/_thirdparty/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/workers/azure_functions_worker/_thirdparty/typing_inspect.py b/workers/azure_functions_worker/_thirdparty/typing_inspect.py deleted file mode 100644 index f5ae783d2..000000000 --- a/workers/azure_functions_worker/_thirdparty/typing_inspect.py +++ /dev/null @@ -1,303 +0,0 @@ -# Imported from https://github.com/ilevkivskyi/typing_inspect/blob/168fa6f7c5c55f720ce6282727211cf4cf6368f6/typing_inspect.py # NoQA E501 -# Author: Ivan Levkivskyi -# License: MIT - -"""Defines experimental API for runtime inspection of types defined -in the standard "typing" module. - -Example usage:: - from typing_inspect import is_generic_type -""" - -# NOTE: This module must support Python 2.7 in addition to Python 3.x - -import collections.abc -import sys -from typing import Callable, ClassVar, Generic, Tuple, TypeVar, Union, _GenericAlias - -NEW_39_TYPING = sys.version_info[:3] >= (3, 9, 0) # PEP 560 -if NEW_39_TYPING: - from typing import _SpecialGenericAlias - - -# from mypy_extensions import _TypedDictMeta - -def is_generic_type(tp): - """Test if the given type is a generic type. This includes Generic itself, - but excludes special typing constructs such as Union, Tuple, Callable, - ClassVar. - Examples:: - - is_generic_type(int) == False - is_generic_type(Union[int, str]) == False - is_generic_type(Union[int, T]) == False - is_generic_type(ClassVar[List[int]]) == False - is_generic_type(Callable[..., T]) == False - is_generic_type(Generic) == True - is_generic_type(Generic[T]) == True - is_generic_type(Iterable[int]) == True - is_generic_type(Mapping) == True - is_generic_type(MutableMapping[T, List[int]]) == True - is_generic_type(Sequence[Union[str, bytes]]) == True - """ - if NEW_39_TYPING: - return (isinstance(tp, type) and issubclass(tp, Generic) - or ((isinstance(tp, _GenericAlias) or isinstance(tp, _SpecialGenericAlias)) # NoQA E501 - and tp.__origin__ not in (Union, tuple, ClassVar, collections.abc.Callable))) # NoQA E501 - return (isinstance(tp, type) - and issubclass(tp, Generic) - or isinstance(tp, _GenericAlias) - and tp.__origin__ not in (Union, tuple, ClassVar, collections.abc.Callable)) # NoQA E501 - - -def is_callable_type(tp): - """Test if the type is a generic callable type, including subclasses - excluding non-generic types and callables. - Examples:: - - is_callable_type(int) == False - is_callable_type(type) == False - is_callable_type(Callable) == True - is_callable_type(Callable[..., int]) == True - is_callable_type(Callable[[int, int], Iterable[str]]) == True - class MyClass(Callable[[int], int]): - ... - is_callable_type(MyClass) == True - - For more general tests use callable(), for more precise test - (excluding subclasses) use:: - - get_origin(tp) is collections.abc.Callable # Callable prior to Python 3.7 # NoQA E501 - """ - return (tp is Callable or isinstance(tp, _GenericAlias) and - tp.__origin__ is collections.abc.Callable or - isinstance(tp, type) and issubclass(tp, Generic) and - issubclass(tp, collections.abc.Callable)) - - -def is_tuple_type(tp): - """Test if the type is a generic tuple type, including subclasses excluding - non-generic classes. - Examples:: - - is_tuple_type(int) == False - is_tuple_type(tuple) == False - is_tuple_type(Tuple) == True - is_tuple_type(Tuple[str, int]) == True - class MyClass(Tuple[str, int]): - ... - is_tuple_type(MyClass) == True - - For more general tests use issubclass(..., tuple), for more precise test - (excluding subclasses) use:: - - get_origin(tp) is tuple # Tuple prior to Python 3.7 - """ - return (tp is Tuple or isinstance(tp, _GenericAlias) and - tp.__origin__ is tuple or - isinstance(tp, type) and issubclass(tp, Generic) and - issubclass(tp, tuple)) - - -def is_union_type(tp): - """Test if the type is a union type. Examples:: - - is_union_type(int) == False - is_union_type(Union) == True - is_union_type(Union[int, int]) == False - is_union_type(Union[T, int]) == True - """ - return (tp is Union or - isinstance(tp, _GenericAlias) and tp.__origin__ is Union) - - -def is_typevar(tp): - """Test if the type represents a type variable. Examples:: - - is_typevar(int) == False - is_typevar(T) == True - is_typevar(Union[T, int]) == False - """ - - return type(tp) is TypeVar - - -def is_classvar(tp): - """Test if the type represents a class variable. Examples:: - - is_classvar(int) == False - is_classvar(ClassVar) == True - is_classvar(ClassVar[int]) == True - is_classvar(ClassVar[List[T]]) == True - """ - return (tp is ClassVar or - isinstance(tp, _GenericAlias) and tp.__origin__ is ClassVar) - - -def get_last_origin(tp): - """Get the last base of (multiply) subscripted type. Supports generic types, - Union, Callable, and Tuple. Returns None for unsupported types. - Examples:: - - get_last_origin(int) == None - get_last_origin(ClassVar[int]) == None - get_last_origin(Generic[T]) == Generic - get_last_origin(Union[T, int][str]) == Union[T, int] - get_last_origin(List[Tuple[T, T]][int]) == List[Tuple[T, T]] - get_last_origin(List) == List - """ - raise ValueError('This function is only supported in Python 3.6,' - ' use get_origin instead') - - -def get_origin(tp): - """Get the unsubscripted version of a type. Supports generic types, Union, - Callable, and Tuple. Returns None for unsupported types. Examples:: - - get_origin(int) == None - get_origin(ClassVar[int]) == None - get_origin(Generic) == Generic - get_origin(Generic[T]) == Generic - get_origin(Union[T, int]) == Union - get_origin(List[Tuple[T, T]][int]) == list # List prior to Python 3.7 - """ - if isinstance(tp, _GenericAlias): - return tp.__origin__ if tp.__origin__ is not ClassVar else None - if tp is Generic: - return Generic - return None - - -def get_parameters(tp): - """Return type parameters of a parameterizable type as a tuple - in lexicographic order. Parameterizable types are generic types, - unions, tuple types and callable types. Examples:: - - get_parameters(int) == () - get_parameters(Generic) == () - get_parameters(Union) == () - get_parameters(List[int]) == () - - get_parameters(Generic[T]) == (T,) - get_parameters(Tuple[List[T], List[S_co]]) == (T, S_co) - get_parameters(Union[S_co, Tuple[T, T]][int, U]) == (U,) - get_parameters(Mapping[T, Tuple[S_co, T]]) == (T, S_co) - """ - if (isinstance(tp, _GenericAlias) or isinstance(tp, type) and - issubclass(tp, Generic) and tp is not Generic): # NoQA E129 - return tp.__parameters__ - return () - - -def get_last_args(tp): - """Get last arguments of (multiply) subscripted type. - Parameters for Callable are flattened. Examples:: - - get_last_args(int) == () - get_last_args(Union) == () - get_last_args(ClassVar[int]) == (int,) - get_last_args(Union[T, int]) == (T, int) - get_last_args(Iterable[Tuple[T, S]][int, T]) == (int, T) - get_last_args(Callable[[T], int]) == (T, int) - get_last_args(Callable[[], int]) == (int,) - """ - raise ValueError('This function is only supported in Python 3.6,' - ' use get_args instead') - - -def _eval_args(args): - """Internal helper for get_args.""" - res = [] - for arg in args: - if not isinstance(arg, tuple): - res.append(arg) - elif is_callable_type(arg[0]): - if len(arg) == 2: - res.append(Callable[[], arg[1]]) - elif arg[1] is Ellipsis: - res.append(Callable[..., arg[2]]) - else: - res.append(Callable[list(arg[1:-1]), arg[-1]]) - else: - res.append(type(arg[0]).__getitem__(arg[0], _eval_args(arg[1:]))) - return tuple(res) - - -def get_args(tp, evaluate=None): - """Get type arguments with all substitutions performed. For unions, - basic simplifications used by Union constructor are performed. - On versions prior to 3.7 if `evaluate` is False (default), - report result as nested tuple, this matches - the internal representation of types. If `evaluate` is True - (or if Python version is 3.7 or greater), then all - type parameters are applied (this could be time and memory expensive). - Examples:: - - get_args(int) == () - get_args(Union[int, Union[T, int], str][int]) == (int, str) - get_args(Union[int, Tuple[T, int]][str]) == (int, (Tuple, str, int)) - - get_args(Union[int, Tuple[T, int]][str], evaluate=True) == \ - (int, Tuple[str, int]) - get_args(Dict[int, Tuple[T, T]][Optional[int]], evaluate=True) == \ - (int, Tuple[Optional[int], Optional[int]]) - get_args(Callable[[], T][int], evaluate=True) == ([], int,) - """ - if evaluate is not None and not evaluate: - raise ValueError('evaluate can only be True in Python 3.7') - if isinstance(tp, _GenericAlias): - res = tp.__args__ - if get_origin(tp) is collections.abc.Callable and res[0] is not Ellipsis: # NoQA E501 - res = (list(res[:-1]), res[-1]) - return res - return () - - -def get_generic_type(obj): - """Get the generic type of an object if possible, or runtime class - otherwise. - Examples:: - - class Node(Generic[T]): - ... - type(Node[int]()) == Node - get_generic_type(Node[int]()) == Node[int] - get_generic_type(Node[T]()) == Node[T] - get_generic_type(1) == int - """ - - gen_type = getattr(obj, '__orig_class__', None) - return gen_type if gen_type is not None else type(obj) - - -def get_generic_bases(tp): - """Get generic base types of a type or empty tuple if not possible. - Example:: - - class MyClass(List[int], Mapping[str, List[int]]): - ... - MyClass.__bases__ == (List, Mapping) - get_generic_bases(MyClass) == (List[int], Mapping[str, List[int]]) - """ - - return getattr(tp, '__orig_bases__', ()) - - -def typed_dict_keys(td): - """If td is a TypedDict class, return a dictionary mapping the typed keys - to types. Otherwise, return None. Examples:: - - class TD(TypedDict): - x: int - y: int - class Other(dict): - x: int - y: int - - typed_dict_keys(TD) == {'x': int, 'y': int} - typed_dict_keys(dict) == None - typed_dict_keys(Other) == None - """ - if isinstance(td, _TypedDictMeta): # NoQA F821 - return td.__annotations__.copy() - return None diff --git a/workers/azure_functions_worker/bindings/generic.py b/workers/azure_functions_worker/bindings/generic.py index e5c2168b2..406ab630e 100644 --- a/workers/azure_functions_worker/bindings/generic.py +++ b/workers/azure_functions_worker/bindings/generic.py @@ -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 . import datumdef @@ -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, *, diff --git a/workers/azure_functions_worker/bindings/meta.py b/workers/azure_functions_worker/bindings/meta.py index 93d544b8e..1532d3778 100644 --- a/workers/azure_functions_worker/bindings/meta.py +++ b/workers/azure_functions_worker/bindings/meta.py @@ -3,6 +3,7 @@ import os import sys import typing +from types import GenericAlias from .. import protos from ..constants import ( @@ -27,6 +28,8 @@ def _check_http_input_type_annotation(bind_name: str, pytype: type, is_deferred_binding: bool) -> bool: + if not isinstance(pytype, type) or isinstance(pytype, GenericAlias): + return False if HttpV2Registry.http_v2_enabled(): return HttpV2Registry.ext_base().RequestTrackerMeta \ .check_type(pytype) @@ -36,6 +39,8 @@ def _check_http_input_type_annotation(bind_name: str, pytype: type, def _check_http_output_type_annotation(bind_name: str, pytype: type) -> bool: + if not isinstance(pytype, type) or isinstance(pytype, GenericAlias): + return False if HttpV2Registry.http_v2_enabled(): return HttpV2Registry.ext_base().ResponseTrackerMeta.check_type(pytype) diff --git a/workers/azure_functions_worker/functions.py b/workers/azure_functions_worker/functions.py index c4d4e37df..76786c3ef 100644 --- a/workers/azure_functions_worker/functions.py +++ b/workers/azure_functions_worker/functions.py @@ -1,18 +1,45 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. +import collections.abc import inspect -import operator import pathlib +import types import typing import uuid from . import bindings as bindings_utils from . import protos -from ._thirdparty import typing_inspect from .constants import HTTP_TRIGGER from .protos import BindingInfo +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): binding_name: str pytype: typing.Optional[type] @@ -172,31 +199,12 @@ def validate_function_params(params: dict, bound_params: dict, param_anno, fx_deferred_bindings_enabled)) - if param_has_anno: - if typing_inspect.is_generic_type(param_anno): - param_anno_origin = typing_inspect.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 = typing_inspect.get_args(param_anno) + param_anno_args = typing.get_args(param_anno) if len(param_anno_args) != 1: raise FunctionLoadError( func_name, @@ -204,18 +212,10 @@ def validate_function_params(params: dict, bound_params: dict, f'{param_anno!r}') 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 typing_inspect.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 typing_inspect.is_generic_type(param_py_type)): + if param_has_anno and not _is_supported_annotation(param_py_type): raise FunctionLoadError( func_name, f'binding {param.name} has invalid non-type annotation ' @@ -259,7 +259,7 @@ def validate_function_params(params: dict, bound_params: dict, 'function definition (function.json or function ' 'decorators) do not match the corresponding ' f'function parameter\'s Python type ' - f'annotation "{param_py_type.__name__}"') + f'annotation "{_annotation_name(param_py_type)}"') else: raise FunctionLoadError( func_name, @@ -267,7 +267,7 @@ def validate_function_params(params: dict, bound_params: dict, 'definition (function.json or function ' 'decorators) ' f'"{binding.type}" does not match its Python ' - f'annotation "{param_py_type.__name__}"') + f'annotation "{_annotation_name(param_py_type)}"') param_type_info = ParamTypeInfo(param_bind_type, param_py_type, @@ -286,16 +286,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 typing_inspect.is_generic_type( - return_anno) and typing_inspect.get_origin( - return_anno) is not None and typing_inspect.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, f'has invalid non-type return ' @@ -308,7 +305,7 @@ def get_function_return_type(annotations: dict, has_explicit_return: bool, binding_name, return_pytype): raise FunctionLoadError( func_name, - f'Python return annotation "{return_pytype.__name__}" ' + f'Python return annotation "{_annotation_name(return_pytype)}" ' f'does not match binding type "{binding_name}"') if has_implicit_return and 'return' in annotations: diff --git a/workers/tests/unittests/test_binding_annotations.py b/workers/tests/unittests/test_binding_annotations.py new file mode 100644 index 000000000..3e58fa857 --- /dev/null +++ b/workers/tests/unittests/test_binding_annotations.py @@ -0,0 +1,468 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. +"""Annotation registration tests independent of the installed SDK version. + +Converter spies test delegation; HTTP and generic fallback guards remain real. +Python below 3.13 tests the worker; Python 3.13+ tests the runtime libraries. +AZURE_FUNCTIONS_TEST_LEGACY_REGISTRY=1 adds legacy worker coverage on 3.13+. +""" + +import collections.abc +import importlib +import inspect +import os +from pathlib import Path +import sys +from types import GenericAlias, SimpleNamespace +import typing +from unittest.mock import Mock + +import azure.functions as azf +import pytest + + +ROOT = Path(__file__).resolve().parents[3] +INCLUDE_LEGACY = ( + sys.version_info < (3, 13) + or os.environ.get("AZURE_FUNCTIONS_TEST_LEGACY_REGISTRY") == "1" +) +COPIES = [] +if INCLUDE_LEGACY: + COPIES.append(pytest.param( + ("workers", "azure_functions_worker"), id="worker")) +if sys.version_info >= (3, 13): + COPIES.extend([ + pytest.param(("runtimes/v1", "azure_functions_runtime_v1"), + id="runtime-v1"), + pytest.param(("runtimes/v2", "azure_functions_runtime"), + id="runtime-v2"), + ]) + + +@pytest.fixture(params=COPIES) +def implementation(request, monkeypatch): + source, package = request.param + for relative in ("workers", "runtimes/v1", "runtimes/v2"): + monkeypatch.syspath_prepend(str(ROOT / relative)) + functions = importlib.import_module(f"{package}.functions") + meta = importlib.import_module(f"{package}.bindings.meta") + generic = importlib.import_module(f"{package}.bindings.generic") + for module, relative in ( + (functions, "functions.py"), + (meta, "bindings/meta.py"), + (generic, "bindings/generic.py"), + ): + expected = ROOT / source / package / relative + assert Path(module.__file__).resolve() == expected.resolve() + monkeypatch.setattr(meta, "BINDING_REGISTRY", azf.get_binding_registry(), + raising=False) + if hasattr(meta, "DEFERRED_BINDING_REGISTRY"): + monkeypatch.setattr(meta, "DEFERRED_BINDING_REGISTRY", None) + if hasattr(meta, "_DEFERRED_BINDING_REGISTRY_LOADED"): + monkeypatch.setattr(meta, "_DEFERRED_BINDING_REGISTRY_LOADED", True) + # Use one generated protocol package per process: both copies register + # FunctionRpc.proto in the same global protobuf descriptor pool. + protocol_package = ( + "azure_functions_worker" if INCLUDE_LEGACY else "proxy_worker") + protos = importlib.import_module(f"{protocol_package}.protos") + return SimpleNamespace( + functions=functions, meta=meta, protos=protos, + generic=generic.GenericBinding, + ) + + +def validate_parameter( + implementation, annotation=inspect.Parameter.empty, *, + direction="in", binding_type="eventHubTrigger"): + """Exercise validation, adapting only the implementation's signature.""" + registry = implementation.functions.Registry + protos = implementation.protos + parameter = inspect.Parameter( + "value", inspect.Parameter.POSITIONAL_OR_KEYWORD, + annotation=annotation, + ) + annotations = {} if annotation is inspect.Parameter.empty else { + "value": annotation + } + binding = SimpleNamespace( + type=binding_type, + direction=getattr(protos.BindingInfo, direction), + data_type=protos.BindingInfo.undefined, + ) + kwargs = {} + signature = inspect.signature(registry.validate_function_params) + if "protos" in signature.parameters: + kwargs["protos"] = protos + result = registry.validate_function_params( + {"value": parameter}, {"value": binding}, annotations, "test", **kwargs + ) + return result[:2] + + +def explicit_return(implementation, annotation, binding_type="eventHub"): + return implementation.functions.Registry.get_function_return_type( + {"return": annotation}, True, False, binding_type, "test" + ) + + +@pytest.mark.parametrize("annotation", [ + pytest.param(azf.EventHubEvent, id="event"), + pytest.param(typing.List[azf.EventHubEvent], id="typing-list"), + pytest.param(list[azf.EventHubEvent], id="builtin-list"), + pytest.param( + collections.abc.Sequence[azf.EventHubEvent], id="abc-sequence" + ), + # Element compatibility, including union membership, belongs to the SDK. + pytest.param( + list[typing.Union[azf.EventHubEvent, str]], id="typing-union-item" + ), + pytest.param(list[azf.EventHubEvent | str], id="pep604-union-item"), +]) +def test_eventhub_input_preserves_sdk_accepted_annotation( + implementation, annotation, monkeypatch): + converter = azf.get_binding_registry().get("eventHubTrigger") + validator = Mock(return_value=True) + monkeypatch.setattr(converter, "check_input_type_annotation", validator) + + inputs, outputs = validate_parameter(implementation, annotation) + + validator.assert_called_once_with(annotation) + assert outputs == {} + assert inputs["value"].binding_name == "eventHubTrigger" + assert inputs["value"].pytype is annotation + + +@pytest.mark.parametrize("annotation", [ + pytest.param(typing.List[str], id="typing-list"), + pytest.param(list[str], id="builtin-list"), + pytest.param(collections.abc.Sequence[str], id="abc-sequence"), + pytest.param(list[typing.Union[str, bytes]], id="typing-union-item"), + pytest.param(list[str | bytes], id="pep604-union-item"), + pytest.param(typing.List[typing.Union[str, dict]], id="typing-str-dict"), + pytest.param(list[str | dict], id="builtin-str-dict"), +]) +def test_out_preserves_sdk_accepted_nested_annotation( + implementation, annotation, monkeypatch): + converter = azf.get_binding_registry().get("eventHub") + validator = Mock(return_value=True) + monkeypatch.setattr(converter, "check_output_type_annotation", validator) + + out_annotation = azf.Out[annotation] + inputs, outputs = validate_parameter( + implementation, out_annotation, + direction="out", binding_type="eventHub", + ) + + validator.assert_called_once_with(typing.get_args(out_annotation)[0]) + assert inputs == {} + assert outputs["value"].binding_name == "eventHub" + # typing may cache Out[T] using an equal (but not identical) alias. + assert outputs["value"].pytype is typing.get_args(out_annotation)[0] + assert outputs["value"].pytype == annotation + + +@pytest.mark.parametrize("annotation", [ + pytest.param(typing.List[int], id="typing-list-int"), + pytest.param(list[int], id="builtin-list-int"), + pytest.param(collections.abc.Sequence[int], id="abc-sequence-int"), + pytest.param(list[list[azf.EventHubEvent]], id="nested-list"), + pytest.param( + GenericAlias(list, (typing.ForwardRef("EventHubEvent"),)), + id="forward-item", + ), + pytest.param(list[typing.Union[int, str]], id="incompatible-union-item"), + pytest.param(list[typing.Union[int, list[str]]], id="nonclass-union-item"), +]) +def test_eventhub_input_delegates_element_rejection_to_sdk( + implementation, annotation, monkeypatch): + converter = azf.get_binding_registry().get("eventHubTrigger") + validator = Mock(return_value=False) + monkeypatch.setattr(converter, "check_input_type_annotation", validator) + + # Assert delegation of the SDK decision, not SDK element policy. + with pytest.raises( + implementation.functions.FunctionLoadError, + match="does not match") as exc_info: + validate_parameter(implementation, annotation) + validator.assert_called_once_with(annotation) + assert repr(annotation) in str(exc_info.value) + + +@pytest.mark.parametrize("annotation", [ + pytest.param(typing.List[int], id="typing-list-int"), + pytest.param(list[int], id="builtin-list-int"), + pytest.param(list[bytes], id="list-bytes"), + pytest.param(list[list[str]], id="nested-list"), + pytest.param(list["str"], id="forward-item"), + pytest.param(list[int | bytes], id="incompatible-union-item"), +]) +def test_out_delegates_element_rejection_to_sdk( + implementation, annotation, monkeypatch): + converter = azf.get_binding_registry().get("eventHub") + validator = Mock(return_value=False) + monkeypatch.setattr(converter, "check_output_type_annotation", validator) + out_annotation = azf.Out[annotation] + nested_annotation = typing.get_args(out_annotation)[0] + + # Out[T] may cache an equal alias; delegate its actual inner annotation. + with pytest.raises( + implementation.functions.FunctionLoadError, + match="does not match") as exc_info: + validate_parameter( + implementation, out_annotation, + direction="out", binding_type="eventHub", + ) + validator.assert_called_once_with(nested_annotation) + assert repr(nested_annotation) in str(exc_info.value) + + +UNSUPPORTED = [ + pytest.param(typing.Tuple[azf.EventHubEvent, ...], id="typing-tuple"), + pytest.param(tuple[azf.EventHubEvent, ...], id="builtin-tuple"), + pytest.param(typing.Union[azf.EventHubEvent, str], id="typing-union"), + pytest.param(azf.EventHubEvent | str, id="pep604-union"), + pytest.param(typing.ClassVar[azf.EventHubEvent], id="classvar"), + pytest.param(typing.Callable[..., azf.EventHubEvent], id="typing-callable"), + pytest.param( + collections.abc.Callable[..., azf.EventHubEvent], id="abc-callable" + ), + pytest.param(typing.Annotated[azf.EventHubEvent, "metadata"], id="annotated"), + pytest.param(typing.Literal["event"], id="literal"), + pytest.param("EventHubEvent", id="forward-string"), + pytest.param(typing.ForwardRef("EventHubEvent"), id="forward-ref"), +] + + +@pytest.mark.parametrize("annotation", UNSUPPORTED) +@pytest.mark.parametrize("is_out", [False, True], ids=["input", "out"]) +def test_parameter_rejects_unsupported_annotation( + implementation, annotation, is_out): + # Pass raw annotations directly: get_type_hints at the loader boundary may + # resolve strings or strip Annotated before this validation layer sees them. + if is_out: + # Bypass typing's subscription-time checks (notably for ClassVar) so + # unsupported inner forms reach the worker's own validation boundary. + annotation = GenericAlias(azf.Out, (annotation,)) + with pytest.raises( + implementation.functions.FunctionLoadError, + match="invalid non-type annotation"): + validate_parameter( + implementation, annotation, + direction="out" if is_out else "in", + binding_type="eventHub" if is_out else "eventHubTrigger", + ) + + +@pytest.mark.parametrize("annotation", [ + pytest.param(azf.Out, id="unparameterized"), + pytest.param(GenericAlias(azf.Out, ()), id="zero-arguments"), + pytest.param(GenericAlias(azf.Out, (str, bytes)), id="two-arguments"), +]) +def test_rejects_malformed_out(implementation, annotation): + with pytest.raises( + implementation.functions.FunctionLoadError, + match="invalid Out annotation"): + validate_parameter( + implementation, annotation, + direction="out", binding_type="eventHub", + ) + + +def test_out_annotation_requires_output_direction(implementation): + with pytest.raises( + implementation.functions.FunctionLoadError, + match='declared to have the "in" direction'): + validate_parameter(implementation, azf.Out[list[str]]) + + +def test_output_direction_requires_out_annotation(implementation): + with pytest.raises( + implementation.functions.FunctionLoadError, + match='declared to have the "out" direction'): + validate_parameter( + implementation, list[str], direction="out", binding_type="eventHub" + ) + + +@pytest.mark.parametrize("name,direction,message", [ + ("value", "inout", '"inout" bindings are not supported'), + ("$return", "in", 'must have direction set to "out"'), +]) +def test_rejects_invalid_binding_direction( + implementation, name, direction, message): + registry = implementation.functions.Registry + kwargs = {} + signature = inspect.signature(registry.validate_binding_direction) + if "protos" in signature.parameters: + kwargs["protos"] = implementation.protos + with pytest.raises( + implementation.functions.FunctionLoadError, match=message): + registry.validate_binding_direction( + name, getattr(implementation.protos.BindingInfo, direction), + "test", **kwargs, + ) + + +@pytest.mark.parametrize("direction", ["in", "out"]) +def test_unannotated_parameter_still_accepted(implementation, direction): + inputs, outputs = validate_parameter( + implementation, direction=direction, binding_type="eventHub" + ) + selected, other = ( + (inputs, outputs) if direction == "in" else (outputs, inputs) + ) + assert other == {} + assert selected["value"].binding_name == "eventHub" + assert selected["value"].pytype is None + + +@pytest.mark.parametrize("annotation", [ + pytest.param(str, id="str"), + pytest.param(typing.List[str], id="typing-list"), + pytest.param(list[str], id="builtin-list"), + pytest.param(collections.abc.Sequence[str], id="abc-sequence"), + pytest.param(list[str | bytes], id="union-item"), +]) +def test_explicit_return_preserves_sdk_accepted_annotation( + implementation, annotation, monkeypatch): + converter = azf.get_binding_registry().get("eventHub") + validator = Mock(return_value=True) + monkeypatch.setattr(converter, "check_output_type_annotation", validator) + + result = explicit_return(implementation, annotation) + + validator.assert_called_once_with(annotation) + assert result.binding_name == "eventHub" + assert result.pytype is annotation + + +@pytest.mark.parametrize("annotation", [list[int], list[bytes], list[list[str]]]) +def test_explicit_return_delegates_element_rejection_to_sdk( + implementation, annotation, monkeypatch): + converter = azf.get_binding_registry().get("eventHub") + validator = Mock(return_value=False) + monkeypatch.setattr(converter, "check_output_type_annotation", validator) + # The worker must honor rejection independently of released SDK policy. + with pytest.raises( + implementation.functions.FunctionLoadError, + match="does not match") as exc_info: + explicit_return(implementation, annotation) + validator.assert_called_once_with(annotation) + assert repr(annotation) in str(exc_info.value) + + +@pytest.mark.parametrize("annotation", UNSUPPORTED) +def test_explicit_return_rejects_unsupported_annotation( + implementation, annotation): + with pytest.raises( + implementation.functions.FunctionLoadError, + match="invalid non-type return annotation"): + explicit_return(implementation, annotation) + + +@pytest.mark.parametrize("annotation", [ + pytest.param(azf.Out, id="unparameterized"), + pytest.param(azf.Out[str], id="scalar"), + pytest.param(azf.Out[typing.List[str]], id="typing-list"), + pytest.param(azf.Out[list[str]], id="builtin-list"), +]) +def test_explicit_return_rejects_out(implementation, annotation): + with pytest.raises( + implementation.functions.FunctionLoadError, + match="return annotation should not be azure.functions.Out"): + explicit_return(implementation, annotation) + + +class Text(str): + pass + + +class Bytes(bytes): + pass + + +class ByteArray(bytearray): + pass + + +@pytest.mark.parametrize("annotation,input_ok,output_ok", [ + (str, True, True), + (bytes, True, True), + (bytearray, False, True), + (Text, True, True), + (Bytes, True, True), + (ByteArray, False, True), + (int, False, False), +]) +def test_generic_binding_preserves_class_support( + implementation, annotation, input_ok, output_ok): + assert ( + implementation.generic.check_input_type_annotation(annotation) + is input_ok + ) + assert ( + implementation.generic.check_output_type_annotation(annotation) + is output_ok + ) + + +NONCLASS = UNSUPPORTED + [ + pytest.param(typing.List[str], id="typing-list"), + pytest.param(list[str], id="builtin-list"), + pytest.param(collections.abc.Sequence[str], id="abc-sequence"), + # GenericAlias instances could pass isinstance(..., type) on older Python. + pytest.param(GenericAlias(str, (int,)), id="str-generic-alias"), + pytest.param(None, id="none"), + pytest.param(42, id="integer-instance"), +] + + +@pytest.mark.parametrize("annotation", NONCLASS) +def test_generic_binding_rejects_nonclass_without_typeerror( + implementation, annotation): + assert ( + implementation.generic.check_input_type_annotation(annotation) + is False + ) + assert ( + implementation.generic.check_output_type_annotation(annotation) + is False + ) + + +@pytest.mark.parametrize("annotation", NONCLASS) +@pytest.mark.parametrize("binding_type,direction", [ + pytest.param("httpTrigger", "in", id="http-trigger-input"), + pytest.param("http", "out", id="http-output"), +]) +def test_http_parameter_rejects_nonclass_with_load_error( + implementation, annotation, binding_type, direction): + if direction == "out": + annotation = GenericAlias(azf.Out, (annotation,)) + # Require the public load error, not a leaked converter TypeError or + # AttributeError. Expected rejection does not depend on production helpers. + with pytest.raises(implementation.functions.FunctionLoadError): + validate_parameter( + implementation, annotation, + direction=direction, binding_type=binding_type, + ) + + +@pytest.mark.parametrize("annotation", NONCLASS) +def test_http_return_rejects_nonclass_with_load_error( + implementation, annotation): + with pytest.raises(implementation.functions.FunctionLoadError): + explicit_return(implementation, annotation, binding_type="http") + + +@pytest.mark.parametrize("is_out", [False, True], ids=["input", "out"]) +def test_unknown_binding_generic_alias_raises_load_error( + implementation, is_out): + annotation = azf.Out[list[str]] if is_out else list[str] + with pytest.raises( + implementation.functions.FunctionLoadError, match="does not match"): + validate_parameter( + implementation, annotation, + direction="out" if is_out else "in", + binding_type="unknownBinding", + ) diff --git a/workers/tests/unittests/test_typing_inspect.py b/workers/tests/unittests/test_typing_inspect.py deleted file mode 100644 index a7d7a4d2d..000000000 --- a/workers/tests/unittests/test_typing_inspect.py +++ /dev/null @@ -1,147 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. -# Imported from https://github.com/ilevkivskyi/typing_inspect/blob/168fa6f7c5c55f720ce6282727211cf4cf6368f6/test_typing_inspect.py -# Author: Ivan Levkivskyi -# License: MIT - -from typing import ( - Any, - Callable, - ClassVar, - Dict, - Generic, - Iterable, - List, - Mapping, - MutableMapping, - NamedTuple, - Optional, - Sequence, - Tuple, - TypeVar, - Union, -) -from unittest import TestCase, main, skipIf - -from azure_functions_worker._thirdparty.typing_inspect import ( - get_args, - get_generic_bases, - get_generic_type, - get_last_args, - get_last_origin, - get_origin, - get_parameters, - is_callable_type, - is_classvar, - is_generic_type, - is_tuple_type, - is_typevar, - is_union_type, -) - - -class IsUtilityTestCase(TestCase): - def sample_test(self, fun, samples, nonsamples): - for s in samples: - self.assertTrue(fun(s), f"{s} type expected in {samples}") - for s in nonsamples: - self.assertFalse(fun(s), f"{s} type expected in {nonsamples}") - - def test_generic(self): - T = TypeVar('T') - samples = [Generic, Generic[T], Iterable[int], Mapping, - MutableMapping[T, List[int]], Sequence[Union[str, bytes]]] - nonsamples = [int, Union[int, str], Union[int, T], ClassVar[List[int]], - Callable[..., T], ClassVar, Optional, bytes, list] - self.sample_test(is_generic_type, samples, nonsamples) - - def test_callable(self): - samples = [Callable, Callable[..., int], - Callable[[int, int], Iterable[str]]] - nonsamples = [int, type, 42, [], List[int], - Union[callable, Callable[..., int]]] - self.sample_test(is_callable_type, samples, nonsamples) - class MyClass(Callable[[int], int]): - pass - self.assertTrue(is_callable_type(MyClass)) - - def test_tuple(self): - samples = [Tuple, Tuple[str, int], Tuple[Iterable, ...]] - nonsamples = [int, tuple, 42, List[int], NamedTuple('N', [('x', int)])] - self.sample_test(is_tuple_type, samples, nonsamples) - class MyClass(Tuple[str, int]): - pass - self.assertTrue(is_tuple_type(MyClass)) - - def test_union(self): - T = TypeVar('T') - S = TypeVar('S') - samples = [Union, Union[T, int], Union[int, Union[T, S]]] - nonsamples = [int, Union[int, int], [], Iterable[Any]] - self.sample_test(is_union_type, samples, nonsamples) - - def test_typevar(self): - T = TypeVar('T') - S_co = TypeVar('S_co', covariant=True) - samples = [T, S_co] - nonsamples = [int, Union[T, int], Union[T, S_co], type, ClassVar[int]] - self.sample_test(is_typevar, samples, nonsamples) - - def test_classvar(self): - T = TypeVar('T') - samples = [ClassVar, ClassVar[int], ClassVar[List[T]]] - nonsamples = [int, 42, Iterable, List[int], type, T] - self.sample_test(is_classvar, samples, nonsamples) - - -class GetUtilityTestCase(TestCase): - - def test_origin(self): - T = TypeVar('T') - class MyClass(Generic[T]): pass - - self.assertEqual(get_origin(int), None) - self.assertEqual(get_origin(ClassVar[int]), None) - self.assertEqual(get_origin(Generic), Generic) - self.assertEqual(get_origin(Generic[T]), Generic) - self.assertEqual(get_origin(List[Tuple[T, T]][int]), list) - self.assertEqual(get_origin(MyClass), None) - - def test_parameters(self): - T = TypeVar('T') - S_co = TypeVar('S_co', covariant=True) - U = TypeVar('U') - self.assertEqual(get_parameters(int), ()) - self.assertEqual(get_parameters(Generic), ()) - self.assertEqual(get_parameters(Union), ()) - self.assertEqual(get_parameters(List[int]), ()) - self.assertEqual(get_parameters(Generic[T]), (T,)) - self.assertEqual(get_parameters(Tuple[List[T], List[S_co]]), (T, S_co)) - self.assertEqual(get_parameters(Union[S_co, Tuple[T, T]][int, U]), (U,)) - self.assertEqual(get_parameters(Mapping[T, Tuple[S_co, T]]), (T, S_co)) - - def test_args_evaluated(self): - T = TypeVar('T') - self.assertEqual(get_args(Union[int, Tuple[T, int]][str], evaluate=True), - (int, Tuple[str, int])) - self.assertEqual(get_args(Dict[int, Tuple[T, T]][Optional[int]], evaluate=True), - (int, Tuple[Optional[int], Optional[int]])) - self.assertEqual(get_args(Callable[[], T][int], evaluate=True), ([], int,)) - - def test_generic_type(self): - T = TypeVar('T') - class Node(Generic[T]): pass - self.assertIs(get_generic_type(Node()), Node) - self.assertIs(get_generic_type(Node[int]()), Node[int]) - self.assertIs(get_generic_type(Node[T]()), Node[T],) - self.assertIs(get_generic_type(1), int) - - def test_generic_bases(self): - class MyClass(List[int], Mapping[str, List[int]]): pass - self.assertEqual(get_generic_bases(MyClass), - (List[int], Mapping[str, List[int]])) - self.assertEqual(get_generic_bases(int), ()) - - -if __name__ == '__main__': - main()