Improve argument typechecking

This commit is contained in:
Andras Schmelczer 2022-07-01 19:50:03 +02:00
parent 0ff0b49a79
commit c8fbced859
2 changed files with 6 additions and 5 deletions

View file

@ -1,6 +1,8 @@
from functools import wraps
from typing import Any, Callable, Dict, TypeVar, cast
from typeguard import check_type
from ..exceptions import ArgumentValidationError
from ..helper import get_arguments, get_function_metadata_store
from ..helper.assert_function_is_not_finalised import assert_function_is_not_finalised
@ -24,14 +26,12 @@ def parameter(
@wraps(func)
def wrapper(*args: Any, **kwargs: Dict[str, Any]) -> Any:
arguments = get_arguments(func, args, kwargs)
argument = arguments[parameter_name]
argument = arguments.get(parameter_name)
expected_type = func.__annotations__.get(parameter_name)
if expected_type is not None and not isinstance(argument, expected_type):
raise ArgumentValidationError(
f"Argument {parameter_name} in {func.__name__} has the wrong type, expected: {expected_type.__name__}, got: {type(argument).__name__}"
)
if expected_type is not None:
check_type(parameter_name, argument, expected_type)
if not validator(argument):
raise ArgumentValidationError(