Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
nycholas committed Jan 2, 2025
1 parent b3e2ae9 commit d38fa6f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
14 changes: 10 additions & 4 deletions src/flask_jsonrpc/descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def __init__(self: Self, jsonrpc_site: JSONRPCSite) -> None:
def _python_type_name(self: Self, pytype: t.Any) -> str: # noqa: ANN401
return str(from_python_type(pytype))

def _fillup_field(self: Self, field: fjt.Field, annotations: t.Annotated) -> fjt.Field: # noqa: C901
def _fillup_field(self: Self, field: fjt.Field, annotations: t.Annotated[t.Any, ...]) -> fjt.Field: # noqa: ANN401, C901
for annotation in annotations:
if isinstance(annotation, types_params.Summary):
field.summary = annotation.summary
Expand Down Expand Up @@ -107,7 +107,7 @@ def _fillup_field(self: Self, field: fjt.Field, annotations: t.Annotated) -> fjt
return field

def _properties_to_fields(
self: Self, annotations: dict[str, t.Annotated | types_params.Properties]
self: Self, annotations: dict[str, t.Annotated[t.Any, ...] | types_params.Properties]
) -> dict[str, fjt.Field]:
fields = {}
for name, annotation in annotations.items():
Expand Down Expand Up @@ -151,7 +151,11 @@ def _service_methods_desc(self: Self) -> t.OrderedDict[str, fjt.Method]: # noqa
methods: t.OrderedDict[str, fjt.Method] = OrderedDict()
for name, view_func in self.jsonrpc_site.view_funcs.items():
method_name = getattr(view_func, 'jsonrpc_method_name', name)
method_annotation = getattr(view_func, 'jsonrpc_method_annotations', types_methods.MethodAnnotated[None])
method_annotation: t.Any | type[types_methods.MethodAnnotated] = getattr(
view_func,
'jsonrpc_method_annotations',
types_methods.MethodAnnotated[None], # type: ignore[misc]
)
method_metadata = getattr(method_annotation, '__metadata__', ())
method_options = getattr(view_func, 'jsonrpc_options', {})
method = fjt.Method(
Expand Down Expand Up @@ -253,7 +257,9 @@ def register(self: Self, jsonrpc_site: JSONRPCSite) -> None:
def describe() -> fjt.ServiceDescribe:
return self.service_describe()

typing_annotations = types_methods.MethodAnnotated[types_methods.Summary('RPC Describe')]
typing_annotations: type[types_methods.MethodAnnotated] = types_methods.MethodAnnotated[ # type: ignore[misc]
types_methods.Summary('RPC Describe')
]
fn_annotations = {'return': t.Annotated[fjt.ServiceDescribe, None]}
setattr(describe, 'jsonrpc_method_name', JSONRPC_DESCRIBE_METHOD_NAME) # noqa: B010
setattr(describe, 'jsonrpc_method_sig', fn_annotations.copy()) # noqa: B010
Expand Down
16 changes: 8 additions & 8 deletions src/flask_jsonrpc/funcutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,24 +106,24 @@ def loads(param_type: t.Any, param_value: t.Any) -> t.Any: # noqa: ANN401, C901
loaded_key = loads(key_type, key)
loaded_value = loads(value_type, value)
loaded_dict[loaded_key] = loaded_value
param_type_origin: t.Any = t.get_origin(param_type)
if param_type_origin is defaultdict:
dict_param_type_origin: t.Any = t.get_origin(param_type)
if dict_param_type_origin is defaultdict:
return defaultdict(None, loaded_dict)
if any(param_type_origin is tp for tp in (Mapping, MutableMapping)):
if any(dict_param_type_origin is tp for tp in (Mapping, MutableMapping)):
return loaded_dict
return param_type_origin(loaded_dict)
return dict_param_type_origin(loaded_dict)

if jsonrpc_types.Array.name == jsonrpc_type.name:
loaded_list = []
item_type = t.get_args(param_type)[0]
for item in param_value:
loaded_list.append(loads(item_type, item))
param_type_origin: t.Any = t.get_origin(param_type)
if any(param_type_origin is tp for tp in (Sequence, MutableSequence, Collection)):
list_param_type_origin: t.Any = t.get_origin(param_type)
if any(list_param_type_origin is tp for tp in (Sequence, MutableSequence, Collection)):
return loaded_list
if any(param_type_origin is tp for tp in (Set, MutableSet)):
if any(list_param_type_origin is tp for tp in (Set, MutableSet)):
return set(loaded_list)
return param_type_origin(loaded_list)
return list_param_type_origin(loaded_list)

if typing_inspect.is_literal_type(param_type):
return param_value
Expand Down
2 changes: 1 addition & 1 deletion src/flask_jsonrpc/types/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class Description(DefaultTypeCheckMixin, BaseAnnotatedMetadata):

@dataclass(frozen=True, **SLOTS)
class Properties(DefaultTypeCheckMixin, BaseAnnotatedMetadata):
annotations: dict[str, t.Annotated | t.Any]
annotations: dict[str, t.Annotated[t.Any, ...] | t.Any]


@dataclass(frozen=True, **SLOTS)
Expand Down
8 changes: 4 additions & 4 deletions src/flask_jsonrpc/types/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import typing as t
import decimal
import inspect
from numbers import Number
from numbers import Number as NumberType
import builtins
from collections import OrderedDict, deque, defaultdict
import dataclasses
Expand Down Expand Up @@ -180,7 +180,7 @@ def to_dict(obj: t.Any, *, level: int = 0, max_level: int = 5) -> t.Any: # noqa
return {f'<JSONRPCType:{type_found}>': to_dict(kv_args[1], level=level, max_level=max_level)}

if Array.check_type(origin_type):
list_args = list(t.get_args(obj))
list_args = t.get_args(obj)
return to_dict(list_args, level=level, max_level=max_level)

if hasattr(obj, '__annotations__') and getattr(obj, '__annotations__', None):
Expand Down Expand Up @@ -266,7 +266,7 @@ def __str__(self: Self) -> str:


String = JSONRPCNewType('String', str, bytes, bytearray, memoryview, Buffer)
Number = JSONRPCNewType('Number', int, float, Number, decimal.Decimal)
Number = JSONRPCNewType('Number', int, float, NumberType, decimal.Decimal)
Object = JSONRPCNewType(
'Object',
dict,
Expand All @@ -284,7 +284,7 @@ def __str__(self: Self) -> str:
set,
t.Set, # noqa: UP006
tuple,
t.Tuple, # noqa: UP006
t.Tuple, # type: ignore[arg-type] # noqa: UP006
frozenset,
t.FrozenSet, # noqa: UP006
GeneratorType,
Expand Down

0 comments on commit d38fa6f

Please sign in to comment.