MCPcopy
hub / github.com/pyodide/pyodide / test_bind_arg_checking

Function test_bind_arg_checking

src/tests/test_jsproxy.py:2873–2945  ·  view source on GitHub ↗
(selenium, sig)

Source from the content-addressed store, hash-verified

2871)
2872@run_in_pyodide
2873def test_bind_arg_checking(selenium, sig):
2874 from inspect import Parameter, signature
2875
2876 import pytest
2877
2878 from pyodide.code import run_js
2879
2880 d = {}
2881 exec(f"def func({sig}): ...", d)
2882 func = d.pop("func")
2883
2884 f = run_js("(...args) => pyodide.toPy(args)").bind_sig(func)
2885
2886 def check(*args, **kwargs):
2887 err = None
2888 try:
2889 try:
2890 func(*args, **kwargs)
2891 except TypeError as e:
2892 err = e
2893
2894 if err:
2895 with pytest.raises(TypeError) as e:
2896 f(*args, **kwargs)
2897 assert e.value.args[0] == err.args[0]
2898 return
2899
2900 sig = signature(func)
2901 # rename positional parameters to work around bug in bind
2902 # https://github.com/python/cpython/issues/87106
2903 new_params = []
2904 for p in sig.parameters.values():
2905 if p.kind == Parameter.POSITIONAL_ONLY:
2906 p = p.replace(name=f"__{p.name}")
2907 new_params.append(p)
2908 sig = sig.replace(parameters=new_params)
2909 res = sig.bind(*args, **kwargs)
2910 res.apply_defaults()
2911 expected = [*res.args]
2912 reskwargs = dict(res.kwargs)
2913 if reskwargs:
2914 expected.append(reskwargs)
2915 for k, v in list(reskwargs.items()):
2916 if v is None:
2917 del reskwargs[k]
2918 assert f(*args, **kwargs) == expected
2919 finally:
2920 err = None
2921
2922 check()
2923 check(1)
2924 check(1, 2)
2925 check(1, 2, 3)
2926
2927 check(x=1)
2928 check(1, x=1)
2929 check(1, 2, 3, x=1)
2930

Callers

nothing calls this directly

Calls 4

run_jsFunction · 0.90
bind_sigMethod · 0.80
checkFunction · 0.70
popMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…