Skip to content

Commit

Permalink
FIX(AUTOG) needs with ... skipped inspect func params >
Browse files Browse the repository at this point in the history
+ FEAT: override-needs accept [arg1, arg2, ..., arg3, ...]
  • Loading branch information
ankostis committed Apr 27, 2023
1 parent 9fc469f commit da4877c
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions graphtik/autograph.py
Expand Up @@ -10,6 +10,7 @@
"""
import functools as fnt
import inspect
import itertools as itt
import logging
import re
import sys
Expand Down Expand Up @@ -407,10 +408,15 @@ class Autograph(Prefkey):
>>> def calc_sum_ab(a, b=0):
... return a + b
>>> aug = Autograph(out_patterns=['calc_', 'upd_'], renames={"a": "A"})
>>> aug.wrap_funcs([autographed, get_autograph_decors, is_regular_class])
>>> aug = Autograph(
... out_patterns=['calc_', 'upd_'], renames={"a": "A"},
... overrides={
... "autographed": {"needs": [keyword("fn", "other_fn"), ...]},
... })
>>> aug.wrap_funcs(
... [autographed, get_autograph_decors, is_regular_class])
[FnOp(name='autographed',
needs=['fn'(?),
needs=['fn'(>'other_fn'),
'name'(?),
'needs'(?),
'provides'(?),
Expand Down Expand Up @@ -624,6 +630,7 @@ def param_to_modifier(name: str, param: inspect.Parameter) -> str:
continue
overrides = self._from_overrides(decor_path)

# TODO: support an extra overrides source, in ``wrap_funcs()``.
op_data = (
ChainMap(overrides, decors)
if (overrides and decors)
Expand All @@ -639,10 +646,12 @@ def param_to_modifier(name: str, param: inspect.Parameter) -> str:
op_data.get(a, UNSET) for a in op_props
)

sig = None
if needs is UNSET:
needs = [...]
needs = aslist(needs, "needs")
if ... in needs:
sig = inspect.signature(fun)
needs = [
fun_needs = [
param_to_modifier(name, param)
for name, param in sig.parameters.items()
if name != "self" and param.kind is not Parameter.VAR_KEYWORD
Expand All @@ -655,15 +664,11 @@ def param_to_modifier(name: str, param: inspect.Parameter) -> str:
class_name = name_path[-2] if len(name_path) > 1 else clazz.__name__
if is_regular_class(class_name, clazz):
log.debug("Object-method %s.%s", class_name, fn_name)
needs.insert(0, camel_2_snake_case(class_name))
fun_needs.insert(0, camel_2_snake_case(class_name))

needs = aslist(needs, "needs")
if ... in needs:
if sig is None:
sig = inspect.signature(fun)
needs = [
arg_name if n is ... else n
for n, arg_name in zip(needs, sig.parameters)
fneed if n is ... else n
for n, fneed in itt.zip_longest(needs, fun_needs, fillvalue=...)
]

if provides is UNSET:
Expand Down

0 comments on commit da4877c

Please sign in to comment.