diff --git a/.coveragerc b/.coveragerc index 21030a82..90e137d5 100644 --- a/.coveragerc +++ b/.coveragerc @@ -34,7 +34,6 @@ omit = */setup.py setup.py */site-packages/* - azure/functions/_thirdparty/* */venv/* */.venv/* */.env*/* diff --git a/.flake8 b/.flake8 index 4db1dd6a..8773d751 100644 --- a/.flake8 +++ b/.flake8 @@ -2,7 +2,7 @@ ignore = W503,E402,E731 exclude = .git, __pycache__, build, dist, .eggs, .github, .local, - Samples, azure/functions/_thirdparty, docs/, .venv*/, .env*/, .vscode/, venv*, pyvenv* + Samples, docs/, .venv*/, .env*/, .vscode/, venv*, pyvenv* max-line-length = 100 extend-ignore = D401, D101, D105, D400, D205, D102, D100, D107, DAR401, DAR101, DAR201 strictness = short diff --git a/README.md b/README.md index f233fff3..cec2ba77 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,21 @@ Issues and feature requests are tracked in a variety of places. To report this f | Portal | User Interface or Experience Issue | [File an Issue](https://github.com/azure/azure-functions-ux/issues) | | Templates | Code Issues with Creation Template | [File an Issue](https://github.com/Azure/azure-functions-templates/issues) | +## Binding type annotations + +Binding annotation checks accept PEP 585 builtin generics wherever the equivalent +`typing` annotation is supported. For example, Event Hub batch inputs accept +`list[func.EventHubEvent]` as well as `typing.List[func.EventHubEvent]`, and list +outputs for Event Hub, Queue, and Event Grid accept `func.Out[list[str]]`. +Parameterized `collections.abc.Iterable` and `Sequence` annotations are also +supported where their `typing` equivalents are accepted. + +This does not change binding cardinality or allowed element types: +`list[str]` is not a valid Event Hub trigger annotation, and bindings that require +a single event or a specialized collection such as `func.DocumentList` still +require that type. Worker-side annotation handling must also support PEP 585; +see [Azure/azure-functions-python-worker#1034](https://github.com/Azure/azure-functions-python-worker/issues/1034). + ## Durable Functions JSON serialization `azure.functions._durable_functions.df_dumps(value, *, ensure_ascii=True)` diff --git a/azure/functions/_thirdparty/__init__.py b/azure/functions/_thirdparty/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/azure/functions/_thirdparty/typing_inspect.py b/azure/functions/_thirdparty/typing_inspect.py deleted file mode 100644 index 4cb6d0c3..00000000 --- a/azure/functions/_thirdparty/typing_inspect.py +++ /dev/null @@ -1,375 +0,0 @@ -# Imported from https://github.com/ilevkivskyi/typing_inspect/blob/168fa6f7c5c55f720ce6282727211cf4cf6368f6/typing_inspect.py -# 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 sys -NEW_TYPING = sys.version_info[:3] >= (3, 7, 0) # PEP 560 -if NEW_TYPING: - import collections.abc - - -if NEW_TYPING: - from typing import ( - Generic, Callable, Union, TypeVar, ClassVar, Tuple, _GenericAlias - ) -else: - from typing import ( - Callable, CallableMeta, Union, _Union, TupleMeta, TypeVar, - _ClassVar, GenericMeta, - ) - - -# from mypy_extensions import _TypedDictMeta - - -def _gorg(cls): - """This function exists for compatibility with old typing versions.""" - assert isinstance(cls, GenericMeta) - if hasattr(cls, '_gorg'): - return cls._gorg - while cls.__origin__ is not None: - cls = cls.__origin__ - return cls - - -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_TYPING: - return (isinstance(tp, type) and issubclass(tp, Generic) or - isinstance(tp, _GenericAlias) and - tp.__origin__ not in (Union, tuple, ClassVar, collections.abc.Callable)) - return (isinstance(tp, GenericMeta) and not - isinstance(tp, (CallableMeta, TupleMeta))) - - -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 - """ - if NEW_TYPING: - 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)) - return type(tp) is CallableMeta - - -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 - """ - if NEW_TYPING: - 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)) - return type(tp) is TupleMeta - - -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 - """ - if NEW_TYPING: - return (tp is Union or - isinstance(tp, _GenericAlias) and tp.__origin__ is Union) - return type(tp) 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 - """ - if NEW_TYPING: - return (tp is ClassVar or - isinstance(tp, _GenericAlias) and tp.__origin__ is ClassVar) - return type(tp) 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 - """ - if NEW_TYPING: - raise ValueError('This function is only supported in Python 3.6,' - ' use get_origin instead') - sentinel = object() - origin = getattr(tp, '__origin__', sentinel) - if origin is sentinel: - return None - if origin is None: - return tp - return origin - - -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 NEW_TYPING: - if isinstance(tp, _GenericAlias): - return tp.__origin__ if tp.__origin__ is not ClassVar else None - if tp is Generic: - return Generic - return None - if isinstance(tp, GenericMeta): - return _gorg(tp) - if is_union_type(tp): - return Union - - 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 NEW_TYPING: - if (isinstance(tp, _GenericAlias) or - isinstance(tp, type) and issubclass(tp, Generic) and - tp is not Generic): - return tp.__parameters__ - return () - if ( - is_generic_type(tp) or is_union_type(tp) or - is_callable_type(tp) or is_tuple_type(tp) - ): - return tp.__parameters__ if tp.__parameters__ is not None else () - 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,) - """ - if NEW_TYPING: - raise ValueError('This function is only supported in Python 3.6,' - ' use get_args instead') - if is_classvar(tp): - return (tp.__type__,) if tp.__type__ is not None else () - if ( - is_generic_type(tp) or is_union_type(tp) or - is_callable_type(tp) or is_tuple_type(tp) - ): - return tp.__args__ if tp.__args__ is not None else () - return () - - -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 NEW_TYPING: - if not (evaluate is None or 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: - res = (list(res[:-1]), res[-1]) - return res - return () - if is_classvar(tp): - return (tp.__type__,) - if ( - is_generic_type(tp) or is_union_type(tp) or - is_callable_type(tp) or is_tuple_type(tp) - ): - tree = tp._subs_tree() - if isinstance(tree, tuple) and len(tree) > 1: - if not evaluate: - return tree[1:] - res = _eval_args(tree[1:]) - if get_origin(tp) is Callable and res[0] is not Ellipsis: - 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): - return td.__annotations__.copy() - return None diff --git a/azure/functions/blob.py b/azure/functions/blob.py index a404bc5c..9fbad523 100644 --- a/azure/functions/blob.py +++ b/azure/functions/blob.py @@ -78,11 +78,11 @@ class BlobConverter(meta.InConverter, trigger='blobTrigger'): @classmethod def check_input_type_annotation(cls, pytype: type) -> bool: - return issubclass(pytype, (azf_abc.InputStream, bytes, str)) + return meta.is_class_type(pytype) and issubclass(pytype, (azf_abc.InputStream, bytes, str)) @classmethod def check_output_type_annotation(cls, pytype: type) -> bool: - return ( + return meta.is_class_type(pytype) and ( issubclass(pytype, (str, bytes, bytearray, azf_abc.InputStream)) or callable(getattr(pytype, 'read', None)) ) diff --git a/azure/functions/connectors.py b/azure/functions/connectors.py index 5c122614..a6fd3b00 100644 --- a/azure/functions/connectors.py +++ b/azure/functions/connectors.py @@ -15,7 +15,7 @@ def check_input_type_annotation(cls, pytype: type) -> bool: valid_types = (str, dict, bytes) return ( meta.is_iterable_type_annotation(pytype, valid_types) - or (isinstance(pytype, type) and issubclass(pytype, valid_types)) + or (meta.is_class_type(pytype) and issubclass(pytype, valid_types)) ) @classmethod diff --git a/azure/functions/cosmosdb.py b/azure/functions/cosmosdb.py index cb008f8b..0caa5b22 100644 --- a/azure/functions/cosmosdb.py +++ b/azure/functions/cosmosdb.py @@ -22,11 +22,11 @@ class CosmosDBConverter(meta.InConverter, meta.OutConverter, @classmethod def check_input_type_annotation(cls, pytype: type) -> bool: - return issubclass(pytype, cdb.DocumentList) + return meta.is_class_type(pytype) and issubclass(pytype, cdb.DocumentList) @classmethod def check_output_type_annotation(cls, pytype: type) -> bool: - return issubclass(pytype, (cdb.DocumentList, cdb.Document)) + return meta.is_class_type(pytype) and issubclass(pytype, (cdb.DocumentList, cdb.Document)) @classmethod def decode(cls, diff --git a/azure/functions/decorators/function_app.py b/azure/functions/decorators/function_app.py index 363415f8..8409b1bf 100644 --- a/azure/functions/decorators/function_app.py +++ b/azure/functions/decorators/function_app.py @@ -1597,9 +1597,9 @@ def connector_trigger(self, :param trigger_config_name: Connector trigger configuration name. Required for Poll. Supports app setting expressions. :param cardinality: Set to many to enable batching, equivalent to the - extension's IsBatched option. Batch parameters may use typing.List[str] - or typing.List[bytes] annotations. If omitted, a single event is passed - to the function. + extension's IsBatched option. Batch parameters may use list[str] or + list[bytes] annotations, or their typing.List equivalents. If omitted, + a single event is passed to the function. :param max_batch_size: Optional per-function override for the maximum number of events per batch. If omitted, Poll delivery uses the extension's app-wide host.json default. diff --git a/azure/functions/durable_functions.py b/azure/functions/durable_functions.py index b7ac47e9..16ece37f 100644 --- a/azure/functions/durable_functions.py +++ b/azure/functions/durable_functions.py @@ -15,7 +15,10 @@ class OrchestrationTriggerConverter(meta.InConverter, trigger=True): @classmethod def check_input_type_annotation(cls, pytype): - return issubclass(pytype, _durable_functions.OrchestrationContext) + return ( + meta.is_class_type(pytype) + and issubclass(pytype, _durable_functions.OrchestrationContext) + ) @classmethod def check_output_type_annotation(cls, pytype): @@ -45,7 +48,7 @@ class EnitityTriggerConverter(meta.InConverter, trigger=True): @classmethod def check_input_type_annotation(cls, pytype): - return issubclass(pytype, _durable_functions.EntityContext) + return meta.is_class_type(pytype) and issubclass(pytype, _durable_functions.EntityContext) @classmethod def check_output_type_annotation(cls, pytype): @@ -149,11 +152,11 @@ def has_trigger_support(cls) -> bool: @classmethod def check_input_type_annotation(cls, pytype: type) -> bool: - return issubclass(pytype, (str, bytes)) + return meta.is_class_type(pytype) and issubclass(pytype, (str, bytes)) @classmethod def check_output_type_annotation(cls, pytype: type) -> bool: - return issubclass(pytype, (str, bytes, bytearray)) + return meta.is_class_type(pytype) and issubclass(pytype, (str, bytes, bytearray)) @classmethod def encode(cls, obj: typing.Any, *, diff --git a/azure/functions/eventgrid.py b/azure/functions/eventgrid.py index 77ad180f..39538f4e 100644 --- a/azure/functions/eventgrid.py +++ b/azure/functions/eventgrid.py @@ -25,7 +25,7 @@ def check_input_type_annotation(cls, pytype: type) -> bool: (CloudEvents v1.0 schema). """ valid_types = (azf_eventgrid.EventGridEvent, azf_eventgrid.CloudEvent) - return isinstance(pytype, type) and issubclass(pytype, valid_types) + return meta.is_class_type(pytype) and issubclass(pytype, valid_types) @classmethod def decode(cls, data: meta.Datum, *, @@ -70,12 +70,11 @@ def decode(cls, data: meta.Datum, *, class EventGridEventOutConverter(meta.OutConverter, binding="eventGrid"): @classmethod def check_output_type_annotation(cls, pytype: type) -> bool: - valid_types = (str, bytes, azf_eventgrid.EventGridOutputEvent, - List[azf_eventgrid.EventGridOutputEvent]) + valid_types = (str, bytes, azf_eventgrid.EventGridOutputEvent) return (meta.is_iterable_type_annotation(pytype, str) or meta. is_iterable_type_annotation(pytype, azf_eventgrid.EventGridOutputEvent) - or (isinstance(pytype, type) + or (meta.is_class_type(pytype) and issubclass(pytype, valid_types))) @classmethod diff --git a/azure/functions/eventhub.py b/azure/functions/eventhub.py index 8888aaed..d837c0f5 100644 --- a/azure/functions/eventhub.py +++ b/azure/functions/eventhub.py @@ -17,7 +17,7 @@ def check_input_type_annotation(cls, pytype: type) -> bool: valid_types = (_eventhub.EventHubEvent) return ( meta.is_iterable_type_annotation(pytype, valid_types) - or (isinstance(pytype, type) and issubclass(pytype, valid_types)) + or (meta.is_class_type(pytype) and issubclass(pytype, valid_types)) ) @classmethod @@ -25,7 +25,7 @@ def check_output_type_annotation(cls, pytype: type) -> bool: valid_types = (str, bytes) return ( meta.is_iterable_type_annotation(pytype, str) - or (isinstance(pytype, type) and issubclass(pytype, valid_types)) + or (meta.is_class_type(pytype) and issubclass(pytype, valid_types)) ) @classmethod diff --git a/azure/functions/http.py b/azure/functions/http.py index 5a9e10c3..7fedc5f9 100644 --- a/azure/functions/http.py +++ b/azure/functions/http.py @@ -72,7 +72,7 @@ class HttpResponseConverter(meta.OutConverter, binding='http'): @classmethod def check_output_type_annotation(cls, pytype: type) -> bool: - return issubclass(pytype, (azf_abc.HttpResponse, str)) + return meta.is_class_type(pytype) and issubclass(pytype, (azf_abc.HttpResponse, str)) @classmethod def encode(cls, obj: typing.Any, *, @@ -136,7 +136,7 @@ class HttpRequestConverter(meta.InConverter, @classmethod def check_input_type_annotation(cls, pytype: type) -> bool: - return issubclass(pytype, azf_abc.HttpRequest) + return meta.is_class_type(pytype) and issubclass(pytype, azf_abc.HttpRequest) @classmethod def decode(cls, data: meta.Datum, *, diff --git a/azure/functions/kafka.py b/azure/functions/kafka.py index 68efa142..91efd5a1 100644 --- a/azure/functions/kafka.py +++ b/azure/functions/kafka.py @@ -113,7 +113,7 @@ def check_input_type_annotation(cls, pytype) -> bool: return ( meta.is_iterable_type_annotation(pytype, valid_types) - or (isinstance(pytype, type) and issubclass(pytype, valid_types)) + or (meta.is_class_type(pytype) and issubclass(pytype, valid_types)) ) @classmethod @@ -121,7 +121,7 @@ def check_output_type_annotation(cls, pytype) -> bool: valid_types = (str, bytes) return ( meta.is_iterable_type_annotation(pytype, str) - or (isinstance(pytype, type) and issubclass(pytype, valid_types)) + or (meta.is_class_type(pytype) and issubclass(pytype, valid_types)) ) @classmethod diff --git a/azure/functions/mcp.py b/azure/functions/mcp.py index f10cdc61..0771d00d 100644 --- a/azure/functions/mcp.py +++ b/azure/functions/mcp.py @@ -182,7 +182,7 @@ class _MCPToolTriggerConverter(meta.InConverter, binding='mcpToolTrigger', @classmethod def check_input_type_annotation(cls, pytype: type) -> bool: - return issubclass(pytype, (str, dict, bytes, MCPToolContext)) + return meta.is_class_type(pytype) and issubclass(pytype, (str, dict, bytes, MCPToolContext)) @classmethod def has_implicit_output(cls) -> bool: @@ -271,7 +271,7 @@ class MCPResourceTriggerConverter(meta.InConverter, binding='mcpResourceTrigger' @classmethod def check_input_type_annotation(cls, pytype: type) -> bool: - return issubclass(pytype, (str, dict, bytes)) + return meta.is_class_type(pytype) and issubclass(pytype, (str, dict, bytes)) @classmethod def has_implicit_output(cls) -> bool: @@ -318,7 +318,10 @@ class _MCPPromptTriggerConverter(meta.InConverter, binding='mcpPromptTrigger', @classmethod def check_input_type_annotation(cls, pytype: type) -> bool: - return issubclass(pytype, (str, dict, bytes, PromptInvocationContext)) + return ( + meta.is_class_type(pytype) + and issubclass(pytype, (str, dict, bytes, PromptInvocationContext)) + ) @classmethod def has_implicit_output(cls) -> bool: diff --git a/azure/functions/meta.py b/azure/functions/meta.py index 9c63a709..0cd0a014 100644 --- a/azure/functions/meta.py +++ b/azure/functions/meta.py @@ -5,10 +5,10 @@ import collections.abc import datetime import re -from typing import Dict, Optional, Union, Tuple, Mapping, Any +from types import GenericAlias +from typing import Dict, Optional, Union, Tuple, Mapping, Any, get_args, get_origin from ._jsonutils import json -from ._thirdparty import typing_inspect from ._utils import ( try_parse_datetime_with_formats, try_parse_timedelta_with_formats @@ -25,25 +25,44 @@ }) +def is_class_type(annotation: object) -> bool: + """Check for a class, excluding PEP 585 aliases even on Python 3.9/3.10.""" + return not isinstance(annotation, GenericAlias) and isinstance(annotation, type) + + +def _is_safe_subclass(pytype: object, target: object) -> bool: + """Allow valid union targets while rejecting unsupported annotations safely.""" + if not is_class_type(pytype): + return False + try: + return issubclass(pytype, target) + except TypeError: + # Parameterized generics and unresolved forward references are not + # valid subclass targets, even when nested inside a union. + return False + + def is_iterable_type_annotation(annotation: object, pytype: object) -> bool: + origin = get_origin(annotation) + # Tuples are fixed-shape records rather than batches. is_iterable_anno = ( - typing_inspect.is_generic_type(annotation) - and issubclass(typing_inspect.get_origin(annotation), - collections.abc.Iterable) + origin is not tuple + and is_class_type(origin) + and issubclass(origin, collections.abc.Iterable) ) if not is_iterable_anno: return False - args = typing_inspect.get_args(annotation) + args = get_args(annotation) if not args: return False if isinstance(pytype, tuple): - return any(isinstance(t, type) and issubclass(t, arg) + return any(_is_safe_subclass(t, arg) for t in pytype for arg in args) else: - return any(isinstance(pytype, type) and issubclass(pytype, arg) + return any(_is_safe_subclass(pytype, arg) for arg in args) diff --git a/azure/functions/mysql.py b/azure/functions/mysql.py index 00b8c716..5855bed6 100644 --- a/azure/functions/mysql.py +++ b/azure/functions/mysql.py @@ -15,11 +15,14 @@ class MySqlConverter(meta.InConverter, meta.OutConverter, @classmethod def check_input_type_annotation(cls, pytype: type) -> bool: - return issubclass(pytype, mysql.BaseMySqlRowList) + return meta.is_class_type(pytype) and issubclass(pytype, mysql.BaseMySqlRowList) @classmethod def check_output_type_annotation(cls, pytype: type) -> bool: - return issubclass(pytype, (mysql.BaseMySqlRowList, mysql.BaseMySqlRow)) + return ( + meta.is_class_type(pytype) + and issubclass(pytype, (mysql.BaseMySqlRowList, mysql.BaseMySqlRow)) + ) @classmethod def decode(cls, diff --git a/azure/functions/queue.py b/azure/functions/queue.py index 0ab87dcc..5beaa109 100644 --- a/azure/functions/queue.py +++ b/azure/functions/queue.py @@ -59,7 +59,7 @@ class QueueMessageInConverter(meta.InConverter, @classmethod def check_input_type_annotation(cls, pytype: type) -> bool: - return issubclass(pytype, azf_abc.QueueMessage) + return meta.is_class_type(pytype) and issubclass(pytype, azf_abc.QueueMessage) @classmethod def decode(cls, data: meta.Datum, *, @@ -101,7 +101,7 @@ def check_output_type_annotation(cls, pytype: type) -> bool: valid_types = (azf_abc.QueueMessage, str, bytes) return ( meta.is_iterable_type_annotation(pytype, valid_types) - or (isinstance(pytype, type) and issubclass(pytype, valid_types)) + or (meta.is_class_type(pytype) and issubclass(pytype, valid_types)) ) @classmethod diff --git a/azure/functions/servicebus.py b/azure/functions/servicebus.py index bc04bfde..202472e5 100644 --- a/azure/functions/servicebus.py +++ b/azure/functions/servicebus.py @@ -229,7 +229,7 @@ def check_input_type_annotation(cls, pytype: type) -> bool: valid_types = (azf_sbus.ServiceBusMessage) return ( meta.is_iterable_type_annotation(pytype, valid_types) - or (isinstance(pytype, type) and issubclass(pytype, valid_types)) + or (meta.is_class_type(pytype) and issubclass(pytype, valid_types)) ) @classmethod @@ -563,7 +563,7 @@ class ServiceBusMessageOutConverter(meta.OutConverter, binding='serviceBus'): @classmethod def check_output_type_annotation(cls, pytype: type) -> bool: - return issubclass(pytype, (str, bytes)) + return meta.is_class_type(pytype) and issubclass(pytype, (str, bytes)) @classmethod def encode(cls, obj: Any, *, diff --git a/azure/functions/sql.py b/azure/functions/sql.py index d385ee6e..52fab2c1 100644 --- a/azure/functions/sql.py +++ b/azure/functions/sql.py @@ -15,11 +15,14 @@ class SqlConverter(meta.InConverter, meta.OutConverter, @classmethod def check_input_type_annotation(cls, pytype: type) -> bool: - return issubclass(pytype, sql.BaseSqlRowList) + return meta.is_class_type(pytype) and issubclass(pytype, sql.BaseSqlRowList) @classmethod def check_output_type_annotation(cls, pytype: type) -> bool: - return issubclass(pytype, (sql.BaseSqlRowList, sql.BaseSqlRow)) + return ( + meta.is_class_type(pytype) + and issubclass(pytype, (sql.BaseSqlRowList, sql.BaseSqlRow)) + ) @classmethod def decode(cls, diff --git a/azure/functions/timer.py b/azure/functions/timer.py index 7f1cc09c..9f60ce9f 100644 --- a/azure/functions/timer.py +++ b/azure/functions/timer.py @@ -43,7 +43,7 @@ class TimerRequestConverter(meta.InConverter, @classmethod def check_input_type_annotation(cls, pytype: type) -> bool: - return issubclass(pytype, azf_abc.TimerRequest) + return meta.is_class_type(pytype) and issubclass(pytype, azf_abc.TimerRequest) @classmethod def decode(cls, data: meta.Datum, *, trigger_metadata) -> typing.Any: diff --git a/azure/functions/warmup.py b/azure/functions/warmup.py index 769230d9..89e1c1a7 100644 --- a/azure/functions/warmup.py +++ b/azure/functions/warmup.py @@ -15,7 +15,7 @@ class WarmUpTriggerConverter(meta.InConverter, binding='warmupTrigger', @classmethod def check_input_type_annotation(cls, pytype: type) -> bool: - return issubclass(pytype, WarmUpContext) + return meta.is_class_type(pytype) and issubclass(pytype, WarmUpContext) @classmethod def decode(cls, data: meta.Datum, *, trigger_metadata) -> typing.Any: diff --git a/setup.cfg b/setup.cfg index 59a50d5a..d7c35a2f 100644 --- a/setup.cfg +++ b/setup.cfg @@ -12,6 +12,3 @@ strict_optional = True warn_return_any = True disallow_subclassing_any = True ignore_missing_imports = True - -[mypy-azure.functions._thirdparty.*] -ignore_errors = True diff --git a/tests/test_connector.py b/tests/test_connector.py index 52f2f39e..2c194704 100644 --- a/tests/test_connector.py +++ b/tests/test_connector.py @@ -2,7 +2,8 @@ # Licensed under the MIT License. import unittest import json -from typing import Iterable, List, Sequence +import collections.abc +from typing import Iterable, List, Optional, Sequence, Union import azure.functions as func from azure.functions.meta import Datum from azure.functions.connectors import ConnectorTriggerConverter @@ -26,13 +27,27 @@ class Dummy: def test_check_input_type_annotation_batch_types(self): for annotation in (List[str], List[bytes], List[dict], - Iterable[str], Sequence[bytes]): + list[str], list[bytes], list[dict], + Iterable[str], Sequence[bytes], + collections.abc.Iterable[str], collections.abc.Sequence[bytes]): with self.subTest(annotation=annotation): self.assertTrue( ConnectorTriggerConverter.check_input_type_annotation(annotation)) + def test_check_input_type_annotation_union_batch_types(self): + for iterable in (List, list, Iterable, collections.abc.Iterable): + for element in (Union[str, dict], str | dict, Optional[str], + Union[str, bytes, dict]): + with self.subTest(iterable=iterable, element=element): + self.assertTrue(ConnectorTriggerConverter.check_input_type_annotation( + iterable[element])) + with self.subTest(iterable=iterable, element='unsupported union'): + self.assertFalse(ConnectorTriggerConverter.check_input_type_annotation( + iterable[Union[int, float]])) + def test_check_input_type_annotation_unsupported_batch_types(self): - for annotation in (List[int], List[float], Iterable[int], List, list): + for annotation in (List[int], List[float], Iterable[int], List, list, + list[int], list[float], collections.abc.Iterable[int]): with self.subTest(annotation=annotation): self.assertFalse( ConnectorTriggerConverter.check_input_type_annotation(annotation)) @@ -41,7 +56,11 @@ def test_decode_batches_with_valid_annotations(self): for annotation, data_type, collection, values in ( (List[str], 'collection_string', CollectionString, ['{"id": "first"}', '{"id": "second"}']), + (list[str], 'collection_string', CollectionString, + ['{"id": "first"}', '{"id": "second"}']), (List[bytes], 'collection_bytes', CollectionBytes, + [b'first', b'second']), + (list[bytes], 'collection_bytes', CollectionBytes, [b'first', b'second'])): for batch in (values, []): with self.subTest(annotation=annotation, batch=batch): diff --git a/tests/test_eventgrid.py b/tests/test_eventgrid.py index 09199b1b..92316bc3 100644 --- a/tests/test_eventgrid.py +++ b/tests/test_eventgrid.py @@ -15,6 +15,7 @@ def test_eventgrid_input_type(self): check_input_type_annotation self.assertTrue(check_input_type(func.EventGridEvent)) self.assertFalse(check_input_type(List[func.EventGridEvent])) + self.assertFalse(check_input_type(list[func.EventGridEvent])) self.assertFalse(check_input_type(str)) self.assertFalse(check_input_type(bytes)) @@ -23,6 +24,7 @@ def test_cloudevent_input_type(self): check_input_type_annotation self.assertTrue(check_input_type(func.CloudEvent)) self.assertFalse(check_input_type(List[func.CloudEvent])) + self.assertFalse(check_input_type(list[func.CloudEvent])) self.assertFalse(check_input_type(str)) self.assertFalse(check_input_type(bytes)) @@ -31,9 +33,14 @@ def test_eventgrid_output_type(self): check_output_type_annotation self.assertTrue(check_output_type(func.EventGridOutputEvent)) self.assertTrue(check_output_type(List[func.EventGridOutputEvent])) + self.assertTrue(check_output_type(list[func.EventGridOutputEvent])) self.assertTrue(check_output_type(str)) self.assertTrue(check_output_type(bytes)) self.assertTrue(check_output_type(List[str])) + self.assertTrue(check_output_type(list[str])) + for annotation in (List[int], list[int], List[bytes], list[bytes], int): + with self.subTest(annotation=annotation): + self.assertFalse(check_output_type(annotation)) def test_eventgrid_decode(self): eventGridEvent = azf_event_grid.EventGridEventInConverter.decode( diff --git a/tests/test_eventhub.py b/tests/test_eventhub.py index 737438f1..d51a209f 100644 --- a/tests/test_eventhub.py +++ b/tests/test_eventhub.py @@ -23,9 +23,11 @@ def test_eventhub_input_type(self): ) self.assertTrue(check_input_type(func.EventHubEvent)) self.assertTrue(check_input_type(List[func.EventHubEvent])) + self.assertTrue(check_input_type(list[func.EventHubEvent])) self.assertFalse(check_input_type(str)) self.assertFalse(check_input_type(bytes)) self.assertFalse(check_input_type(List[str])) + self.assertFalse(check_input_type(list[str])) def test_eventhub_output_type(self): check_output_type = ( @@ -34,9 +36,12 @@ def test_eventhub_output_type(self): self.assertTrue(check_output_type(bytes)) self.assertTrue(check_output_type(str)) self.assertTrue(check_output_type(List[str])) + self.assertTrue(check_output_type(list[str])) self.assertFalse(check_output_type(func.EventHubEvent)) self.assertFalse(check_output_type(List[bytes])) + self.assertFalse(check_output_type(list[bytes])) self.assertFalse(check_output_type(List[func.EventHubEvent])) + self.assertFalse(check_output_type(list[func.EventHubEvent])) @patch('azure.functions.eventhub.EventHubTriggerConverter' '.decode_single_event') diff --git a/tests/test_kafka.py b/tests/test_kafka.py index 6776c946..8c0037f0 100644 --- a/tests/test_kafka.py +++ b/tests/test_kafka.py @@ -36,9 +36,11 @@ def test_kafka_input_type(self): ) self.assertTrue(check_input_type(func.KafkaEvent)) self.assertTrue(check_input_type(List[func.KafkaEvent])) + self.assertTrue(check_input_type(list[func.KafkaEvent])) self.assertFalse(check_input_type(str)) self.assertFalse(check_input_type(bytes)) self.assertFalse(check_input_type(List[str])) + self.assertFalse(check_input_type(list[str])) def test_kafka_output_type(self): check_output_type = ( @@ -47,9 +49,12 @@ def test_kafka_output_type(self): self.assertTrue(check_output_type(bytes)) self.assertTrue(check_output_type(str)) self.assertTrue(check_output_type(List[str])) + self.assertTrue(check_output_type(list[str])) self.assertFalse(check_output_type(func.KafkaEvent)) self.assertFalse(check_output_type(List[bytes])) + self.assertFalse(check_output_type(list[bytes])) self.assertFalse(check_output_type(List[func.KafkaEvent])) + self.assertFalse(check_output_type(list[func.KafkaEvent])) @patch('azure.functions.kafka.KafkaTriggerConverter' '.decode_single_event') diff --git a/tests/test_mcp.py b/tests/test_mcp.py index 66f92cfc..87f62d74 100644 --- a/tests/test_mcp.py +++ b/tests/test_mcp.py @@ -19,8 +19,9 @@ def test_check_input_type_annotation_valid_types(self): self.assertTrue(_MCPToolTriggerConverter.check_input_type_annotation(func.MCPToolContext)) def test_check_input_type_annotation_invalid_type(self): - with self.assertRaises(TypeError): - _MCPToolTriggerConverter.check_input_type_annotation(123) # not a type + for annotation in (123, list[str], dict[str, list[int]]): + with self.subTest(annotation=annotation): + self.assertFalse(_MCPToolTriggerConverter.check_input_type_annotation(annotation)) class Dummy: pass @@ -162,9 +163,10 @@ def test_check_input_type_annotation_valid_types(self): PromptInvocationContext)) def test_check_input_type_annotation_invalid_type(self): - with self.assertRaises(TypeError): - _MCPPromptTriggerConverter.check_input_type_annotation( - 123) # not a type + for annotation in (123, list[str], dict[str, list[int]]): + with self.subTest(annotation=annotation): + self.assertFalse( + _MCPPromptTriggerConverter.check_input_type_annotation(annotation)) class Dummy: pass diff --git a/tests/test_meta.py b/tests/test_meta.py index 18522c29..118cb056 100644 --- a/tests/test_meta.py +++ b/tests/test_meta.py @@ -4,10 +4,112 @@ from typing import Mapping, List import unittest import datetime +import collections.abc +import typing +import azure.functions as func from azure.functions import meta +class TestTypeAnnotations(unittest.TestCase): + def test_iterable_annotation_rejects_special_forms(self): + for annotation in (tuple[str, ...], typing.Tuple[str, ...], tuple[str], + typing.Tuple[str, bytes], collections.abc.Callable[[str], str], + typing.Callable[[str], str], typing.Optional[List[str]], + list[str] | None, typing.Union[List[str], str], + typing.ClassVar[List[str]]): + for valid_types in (str, (str, bytes)): + with self.subTest(annotation=annotation, valid_types=valid_types): + self.assertFalse(meta.is_iterable_type_annotation(annotation, valid_types)) + + def test_iterable_annotation_element_types(self): + for iterable in (List, list, typing.Iterable, collections.abc.Iterable, + typing.Sequence, collections.abc.Sequence, set): + with self.subTest(iterable=iterable): + self.assertTrue(meta.is_iterable_type_annotation( + iterable[func.EventHubEvent], func.EventHubEvent)) + self.assertFalse(meta.is_iterable_type_annotation( + iterable[str], func.EventHubEvent)) + self.assertTrue(meta.is_iterable_type_annotation(iterable[str], (str, bytes))) + self.assertFalse(meta.is_iterable_type_annotation(iterable[int], (str, bytes))) + + def test_iterable_annotation_union_elements(self): + for iterable in (List, list, typing.Iterable, collections.abc.Iterable, + typing.Sequence, collections.abc.Sequence): + for element in (typing.Union[str, dict], str | dict, + typing.Optional[str], str | None): + for valid_types in (str, (str, bytes)): + with self.subTest(iterable=iterable, element=element, + valid_types=valid_types): + self.assertTrue(meta.is_iterable_type_annotation( + iterable[element], valid_types)) + self.assertFalse(meta.is_iterable_type_annotation( + iterable[typing.Union[int, float]], valid_types)) + + def test_iterable_annotation_invalid_union_elements(self): + unresolved = typing.ForwardRef('UnresolvedType') + for iterable in (List, list): + for element in (typing.Union[int, List[str]], + typing.Union[int, list[str]], + typing.Union[int, unresolved], + typing.Union[int, typing.Literal['value']]): + for valid_types in (str, (str, bytes)): + with self.subTest(iterable=iterable, element=element, + valid_types=valid_types): + self.assertFalse(meta.is_iterable_type_annotation( + iterable[element], valid_types)) + + def test_iterable_annotation_rejects_non_class_elements(self): + for annotation in (list[list[str]], List[List[str]], dict[int, list[str]], + typing.Dict[int, List[str]], list["str"], List["str"], + list, List, typing.Generic): + for valid_types in (str, (str, bytes)): + with self.subTest(annotation=annotation, valid_types=valid_types): + self.assertFalse(meta.is_iterable_type_annotation(annotation, valid_types)) + + def test_converters_reject_unsupported_generic_aliases(self): + bindings_by_direction = { + 'input': ('eventHub', 'kafka', 'serviceBusTrigger', 'queueTrigger', + 'connectorTrigger', 'eventGridTrigger', 'blob', 'cosmosDB', + 'sql', 'mysql', 'httpTrigger', 'timerTrigger', 'warmupTrigger', + 'orchestrationTrigger', 'entityTrigger', 'durableClient', + 'mcpToolTrigger', 'mcpResourceTrigger', 'mcpPromptTrigger'), + 'output': ('eventHub', 'kafka', 'queue', 'eventGrid', 'serviceBus', 'blob', + 'cosmosDB', 'sql', 'mysql', 'http', 'durableClient'), + } + for direction, bindings in bindings_by_direction.items(): + for binding in bindings: + converter = meta.get_binding_registry().get(binding) + check = getattr(converter, f'check_{direction}_type_annotation') + for annotation in (list[int], dict[int, list[str]], list[list[str]]): + with self.subTest(binding=binding, direction=direction, annotation=annotation): + self.assertFalse(check(annotation)) + + def test_out_list_annotations(self): + for binding in ('eventHub', 'kafka', 'queue', 'eventGrid'): + converter = meta.get_binding_registry().get(binding) + for annotation in (func.Out[List[str]], func.Out[list[str]], + func.Out[List[typing.Union[str, dict]]], + func.Out[list[str | dict]], + func.Out[List[typing.Optional[str]]]): + with self.subTest(binding=binding, annotation=annotation): + # The worker unwraps Out[T] before calling the converter. + element_type, = typing.get_args(annotation) + self.assertTrue(converter.check_output_type_annotation(element_type)) + + def test_list_output_encoding(self): + for binding in ('eventHub', 'queue', 'eventGrid'): + converter = meta.get_binding_registry().get(binding) + for annotation in (List[str], list[str], List[typing.Union[str, dict]], + list[str | dict], List[typing.Optional[str]]): + with self.subTest(binding=binding, annotation=annotation): + self.assertTrue(converter.check_output_type_annotation(annotation)) + for values in ([], ['first', 'second']): + encoded = converter.encode(values, expected_type=annotation) + self.assertEqual(encoded.type, 'json') + self.assertEqual(encoded.python_value, values) + + class TestMeta(unittest.TestCase): def test_parsed_datetime_none(self): parsed = self._parse_datetime(None) diff --git a/tests/test_queue.py b/tests/test_queue.py index a15a0e36..813db885 100644 --- a/tests/test_queue.py +++ b/tests/test_queue.py @@ -4,6 +4,7 @@ import json import unittest from datetime import date +from typing import List import azure.functions as func import azure.functions.queue as azf_q from azure.functions.meta import Datum, _BaseConverter @@ -65,6 +66,8 @@ def test_QueueMessage_input_type(self): self.assertTrue(check_input_type(func.QueueMessage)) self.assertFalse(check_input_type(str)) self.assertFalse(check_input_type(bytes)) + self.assertFalse(check_input_type(List[func.QueueMessage])) + self.assertFalse(check_input_type(list[func.QueueMessage])) def test_QueueMessage_output_type(self): # when @@ -76,6 +79,13 @@ def test_QueueMessage_output_type(self): self.assertTrue(check_output_type(func.QueueMessage)) self.assertTrue(check_output_type(str)) self.assertTrue(check_output_type(bytes)) + for annotation in (List[func._abc.QueueMessage], list[func._abc.QueueMessage], + List[str], list[str]): + with self.subTest(annotation=annotation): + self.assertTrue(check_output_type(annotation)) + for annotation in (List[int], list[int]): + with self.subTest(annotation=annotation): + self.assertFalse(check_output_type(annotation)) def test_queue_message_initialize_all_args(self): # given diff --git a/tests/test_servicebus.py b/tests/test_servicebus.py index 88592f7f..a3dae25a 100644 --- a/tests/test_servicebus.py +++ b/tests/test_servicebus.py @@ -219,6 +219,7 @@ def test_servicebus_input_type(self): # Should accept a multiple service bus message as trigger input type self.assertTrue(check_input_type(List[func.ServiceBusMessage])) + self.assertTrue(check_input_type(list[func.ServiceBusMessage])) # Should accept a message class derived from service bus class ServiceBusMessageChild(func.ServiceBusMessage): @@ -230,6 +231,8 @@ class ServiceBusMessageChild(func.ServiceBusMessage): self.assertFalse(check_input_type(func.EventHubEvent)) self.assertFalse(check_input_type(str)) self.assertFalse(check_input_type(type(None))) + self.assertFalse(check_input_type(List[str])) + self.assertFalse(check_input_type(list[str])) def test_servicebus_output_type(self): check_output_type = ( @@ -245,6 +248,8 @@ def test_servicebus_output_type(self): # Should be false if a message type does not match expectation self.assertFalse(check_output_type(func.EventGridEvent)) self.assertFalse(check_output_type(type(None))) + self.assertFalse(check_output_type(List[str])) + self.assertFalse(check_output_type(list[str])) def test_servicebus_data(self): servicebus_msg = azf_sb.ServiceBusMessageInConverter.decode(