[+] add type checking decorator
1. add pydantic based decorator, that checks typing for incoming arguments; 1.1. helps when some wrapping decorator uses *args, **kwargs, since pyright just ignores what happens after wards;
This commit is contained in:
parent
3b90dbb347
commit
91dbbde50d
@ -5,7 +5,7 @@ project(
|
|||||||
).stdout().strip('\n'),
|
).stdout().strip('\n'),
|
||||||
# 'online.fxreader.uv',
|
# 'online.fxreader.uv',
|
||||||
# ['c', 'cpp'],
|
# ['c', 'cpp'],
|
||||||
version: '0.1.5.17+27.21',
|
version: '0.1.5.17+27.22',
|
||||||
# default_options: [
|
# default_options: [
|
||||||
# 'cpp_std=c++23',
|
# 'cpp_std=c++23',
|
||||||
# # 'prefer_static=true',
|
# # 'prefer_static=true',
|
||||||
|
82
python/online/fxreader/pr34/commands_typed/pydantic.py
Normal file
82
python/online/fxreader/pr34/commands_typed/pydantic.py
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
import pydantic
|
||||||
|
import inspect
|
||||||
|
import collections
|
||||||
|
|
||||||
|
from typing import (
|
||||||
|
TypeVar, Callable, Any, Optional,
|
||||||
|
Mapping, cast,
|
||||||
|
)
|
||||||
|
|
||||||
|
P = TypeVar('P')
|
||||||
|
R = TypeVar('R')
|
||||||
|
|
||||||
|
def validate_params(
|
||||||
|
view: Callable[..., R]
|
||||||
|
) -> Callable[..., R]:
|
||||||
|
import inspect
|
||||||
|
|
||||||
|
class Parameter:
|
||||||
|
kind: Any
|
||||||
|
annotation: Any
|
||||||
|
|
||||||
|
parameters = cast(
|
||||||
|
Mapping[str, Parameter],
|
||||||
|
inspect.signature(view).parameters
|
||||||
|
)
|
||||||
|
|
||||||
|
positional_parameters: collections.OrderedDict[str, type[Any]] = \
|
||||||
|
collections.OrderedDict((
|
||||||
|
(k, v.annotation)
|
||||||
|
for k, v in parameters.items()
|
||||||
|
if v.kind in (
|
||||||
|
inspect.Parameter.POSITIONAL_ONLY,
|
||||||
|
inspect.Parameter.POSITIONAL_OR_KEYWORD,
|
||||||
|
)
|
||||||
|
))
|
||||||
|
positional_names = list(positional_parameters)
|
||||||
|
|
||||||
|
model = pydantic.create_model(
|
||||||
|
getattr(view, '__name__'),
|
||||||
|
**{
|
||||||
|
k : v.annotation
|
||||||
|
for k, v in parameters.items()
|
||||||
|
if v.kind in (
|
||||||
|
inspect.Parameter.POSITIONAL_OR_KEYWORD,
|
||||||
|
inspect.Parameter.POSITIONAL_ONLY,
|
||||||
|
inspect.Parameter.KEYWORD_ONLY,
|
||||||
|
)
|
||||||
|
},
|
||||||
|
__config__=pydantic.ConfigDict(
|
||||||
|
arbitrary_types_allowed=True,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
def wrapper(*args: Any, **kwargs: Any) -> R:
|
||||||
|
#data = model.model_validate(
|
||||||
|
|
||||||
|
kwargs_to_check : dict[str, Any] = {
|
||||||
|
k : v
|
||||||
|
for k, v in kwargs.items()
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, o in enumerate(args):
|
||||||
|
k = positional_names[i]
|
||||||
|
|
||||||
|
parameter = positional_parameters[k]
|
||||||
|
|
||||||
|
assert not k in kwargs_to_check
|
||||||
|
|
||||||
|
kwargs_to_check[k] = o
|
||||||
|
|
||||||
|
model.model_validate(
|
||||||
|
kwargs_to_check,
|
||||||
|
)
|
||||||
|
#).dict()
|
||||||
|
|
||||||
|
return view(
|
||||||
|
#**data,
|
||||||
|
*args,
|
||||||
|
**kwargs,
|
||||||
|
)
|
||||||
|
|
||||||
|
return wrapper
|
Loading…
Reference in New Issue
Block a user