|
4 | 4 |
|
5 | 5 | # %% auto #0 |
6 | 6 | __all__ = ['test_sig', 'FixSigMeta', 'PrePostInitMeta', 'AutoInit', 'NewChkMeta', 'BypassNewMeta', 'empty2none', 'anno_dict', |
7 | | - 'use_kwargs_dict', 'use_kwargs', 'delegates', 'method', 'funcs_kwargs'] |
| 7 | + 'use_kwargs_dict', 'use_kwargs', 'delegates', 'method', 'funcs_kwargs', 'splice_sig'] |
8 | 8 |
|
9 | 9 | # %% ../nbs/05_meta.ipynb #d26e95dd |
10 | 10 | from .imports import * |
11 | 11 | from .basics import * |
12 | 12 | from contextlib import contextmanager |
| 13 | +from functools import wraps |
| 14 | +from inspect import signature, Parameter |
13 | 15 | from copy import copy |
| 16 | + |
14 | 17 | import inspect |
15 | 18 |
|
16 | 19 | # %% ../nbs/05_meta.ipynb #8f330096 |
@@ -159,3 +162,14 @@ def funcs_kwargs(as_method=False): |
159 | 162 | "Replace methods in `cls._methods` with those from `kwargs`" |
160 | 163 | if callable(as_method): return _funcs_kwargs(as_method, False) |
161 | 164 | return partial(_funcs_kwargs, as_method=as_method) |
| 165 | + |
| 166 | +# %% ../nbs/05_meta.ipynb #b69b0276 |
| 167 | +def splice_sig(wrapper, fn, *skips): |
| 168 | + "Replace *args/**kwargs in wrapper's sig with fn's params (minus skips)" |
| 169 | + w_ps = list(signature(wrapper).parameters.values()) |
| 170 | + f_ps = [p for p in signature(fn).parameters.values() if p.name not in skips] |
| 171 | + split = next((i for i, p in enumerate(w_ps) if p.kind==Parameter.VAR_POSITIONAL), len(w_ps)) |
| 172 | + pre = w_ps[:split] |
| 173 | + post = [p for p in w_ps[split+1:] if p.kind != Parameter.VAR_KEYWORD] |
| 174 | + wrapper.__signature__ = signature(wrapper).replace(parameters=[*pre, *f_ps, *post]) |
| 175 | + return wraps(fn)(wrapper) |
0 commit comments