Skip to content

Commit 494a1a1

Browse files
committed
Add method signature inspection to filter kwargs
Add method/function to check if the called method accepts kwargs or not.
1 parent 666952f commit 494a1a1

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

src/scyjava/_convert.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
import collections
6+
import inspect
67
import typing
78
from pathlib import Path
89
from typing import Any, Callable, NamedTuple
@@ -36,12 +37,23 @@ class Converter(NamedTuple):
3637
def _convert(obj: Any, converters: typing.List[Converter], **kwargs: dict) -> Any:
3738
suitable_converters = filter(lambda c: c.predicate(obj), converters)
3839
prioritized = max(suitable_converters, key=lambda c: c.priority)
39-
if kwargs:
40+
# check if select converter supports kwargs
41+
if _check_func_uses_kwargs(prioritized.converter):
4042
return prioritized.converter(obj, **kwargs)
4143
else:
4244
return prioritized.converter(obj)
4345

4446

47+
def _check_func_uses_kwargs(func):
48+
if isjava(func):
49+
return False
50+
else:
51+
return any(
52+
p.kind == inspect.Parameter.VAR_KEYWORD
53+
for p in inspect.signature(func).parameters.values()
54+
)
55+
56+
4557
# -- Python to Java --
4658

4759
# Adapted from code posted by vslotman on GitHub:

0 commit comments

Comments
 (0)