From 28aafcd708e95d5f560d9ad2c66edb5a3af83138 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Mon, 6 Jan 2025 11:20:37 +0530 Subject: [PATCH 1/2] perf: Don't validate types if there are no input types Many functions just have return type annotated, in such cases running validation code is not necessary. --- frappe/utils/typing_validations.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/frappe/utils/typing_validations.py b/frappe/utils/typing_validations.py index d01f3fe15d..526e5bce3a 100644 --- a/frappe/utils/typing_validations.py +++ b/frappe/utils/typing_validations.py @@ -94,7 +94,12 @@ def transform_parameter_types(func: Callable, args: tuple, kwargs: dict): defined on the function. """ - if not (args or kwargs) or not func.__annotations__: + if ( + not (args or kwargs) + or not func.__annotations__ + # No input validations to perform + or (len(func.__annotations__) == 1 and func.__annotations__.get("return")) + ): return args, kwargs from pydantic import ValidationError as PyValidationError From 98cbe05c9adbec894d3684b33ca1631e170132ec Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Mon, 6 Jan 2025 11:23:30 +0530 Subject: [PATCH 2/2] perf: use cached function signature --- frappe/utils/typing_validations.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/frappe/utils/typing_validations.py b/frappe/utils/typing_validations.py index 526e5bce3a..5a7d40d8d6 100644 --- a/frappe/utils/typing_validations.py +++ b/frappe/utils/typing_validations.py @@ -1,6 +1,6 @@ from collections.abc import Callable from functools import lru_cache, wraps -from inspect import _empty, isclass, signature +from inspect import _empty, isclass from types import EllipsisType from typing import ForwardRef, TypeVar, Union from unittest import mock @@ -104,6 +104,8 @@ def transform_parameter_types(func: Callable, args: tuple, kwargs: dict): from pydantic import ValidationError as PyValidationError + import frappe + annotations = func.__annotations__ new_args, new_kwargs = list(args), kwargs @@ -122,7 +124,7 @@ def transform_parameter_types(func: Callable, args: tuple, kwargs: dict): prepared_args = dict(zip(arg_names, args, strict=False)) # check if type hints dont match the default values - func_signature = signature(func) + func_signature = frappe._cached_inspect_signature(func) func_params = dict(func_signature.parameters) # check if the argument types are correct