Improve argument typechecking
This commit is contained in:
parent
0ff0b49a79
commit
c8fbced859
2 changed files with 6 additions and 5 deletions
|
|
@ -38,6 +38,7 @@ install_requires =
|
||||||
fastapi >= 0.70.0
|
fastapi >= 0.70.0
|
||||||
uvicorn[standard] >= 0.18.0
|
uvicorn[standard] >= 0.18.0
|
||||||
watchdog >= 2.1.0
|
watchdog >= 2.1.0
|
||||||
|
typeguard >= 2.10.0
|
||||||
pymongo >= 3.0.0
|
pymongo >= 3.0.0
|
||||||
dill >= 0.3.5.0
|
dill >= 0.3.5.0
|
||||||
aiohttp[speedups] >= 3.8.0
|
aiohttp[speedups] >= 3.8.0
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
from typing import Any, Callable, Dict, TypeVar, cast
|
from typing import Any, Callable, Dict, TypeVar, cast
|
||||||
|
|
||||||
|
from typeguard import check_type
|
||||||
|
|
||||||
from ..exceptions import ArgumentValidationError
|
from ..exceptions import ArgumentValidationError
|
||||||
from ..helper import get_arguments, get_function_metadata_store
|
from ..helper import get_arguments, get_function_metadata_store
|
||||||
from ..helper.assert_function_is_not_finalised import assert_function_is_not_finalised
|
from ..helper.assert_function_is_not_finalised import assert_function_is_not_finalised
|
||||||
|
|
@ -24,14 +26,12 @@ def parameter(
|
||||||
@wraps(func)
|
@wraps(func)
|
||||||
def wrapper(*args: Any, **kwargs: Dict[str, Any]) -> Any:
|
def wrapper(*args: Any, **kwargs: Dict[str, Any]) -> Any:
|
||||||
arguments = get_arguments(func, args, kwargs)
|
arguments = get_arguments(func, args, kwargs)
|
||||||
argument = arguments[parameter_name]
|
argument = arguments.get(parameter_name)
|
||||||
|
|
||||||
expected_type = func.__annotations__.get(parameter_name)
|
expected_type = func.__annotations__.get(parameter_name)
|
||||||
|
|
||||||
if expected_type is not None and not isinstance(argument, expected_type):
|
if expected_type is not None:
|
||||||
raise ArgumentValidationError(
|
check_type(parameter_name, argument, expected_type)
|
||||||
f"Argument {parameter_name} in {func.__name__} has the wrong type, expected: {expected_type.__name__}, got: {type(argument).__name__}"
|
|
||||||
)
|
|
||||||
|
|
||||||
if not validator(argument):
|
if not validator(argument):
|
||||||
raise ArgumentValidationError(
|
raise ArgumentValidationError(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue