You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
My solution here is to modify delegates to output kwargs with Param type hints, thus propagating the docments through to call_parse. This solution is a tad ugly since cast_to_call_parse=True needs to be passed so that the delegates knows that it
is supposed to change everything to Param's.
Problem
call_parse does not work if using a delegates+docments combo. For example:
classA(object):
"something"def__init__(self,
a:int=5# This is a parameter
):
pass@call_parse@delegates(A)defsome_callable(
b:int=8, # A paramter that is defined in the call method.**kwargs
):
"Some doc"passshow_doc(A)
show_doc(some_callable)
print(anno_parser(some_callable).format_help())
Outputs:
TypeError Traceback (most recent call last)
Input In [19], in<module>
17 show_doc(A)
18 show_doc(some_callable)
---> 19 anno_parser(some_callable)
File ~/.local/lib/python3.9/site-packages/fastcore/script.py:75, in anno_parser(func, prog, from_name)
73 param = v.anno
74 if not isinstance(param,Param): param = Param(v.docment, v.anno)
---> 75 param.set_default(v.default)
76 p.add_argument(f"{param.pre}{k}", **param.kwargs)
77 p.add_argument(f"--pdb", help=argparse.SUPPRESS, action='store_true')
File ~/.local/lib/python3.9/site-packages/fastcore/script.py:48, in Param.set_default(self, d)
46 if d==inspect.Parameter.empty: self.opt = False
47 else: self.default = d
---> 48 if self.default is not None: self.help += f" (default: {self.default})"
TypeError: unsupported operand type(s) for +=: 'NoneType' and 'str'
Slightly Dirty Solution
*also buggy, it doesnt handle default values very well
# exportimportinspectfrominspectimportParameterfromfastcore.docmentsimportdocmentsdefdelegates(to=None, keep=False, but=None, cast_to_call_parse=False):
"Decorator: replace `**kwargs` in signature with params from `to`"ifbutisNone: but= []
def_f(f):
iftoisNone: to_f,from_f=f.__base__.__init__,f.__init__else: to_f,from_f=to.__init__ifisinstance(to,type) elseto,ffrom_f=getattr(from_f,'__func__',from_f)
to_f=getattr(to_f,'__func__',to_f)
ifhasattr(from_f,'__delwrap__'): returnfsig=inspect.signature(from_f)
sigd=dict(sig.parameters)
k=sigd.pop('kwargs')
s2= {k:vfork,vininspect.signature(to_f).parameters.items()
ifv.default!=inspect.Parameter.emptyandknotinsigdandknotinbut}
ifcast_to_call_parse:
fork,vindocments(to_f, full=True, returns=False).items():
param=v.annoifv.default==inspect.Parameter.emptyorkinsigdorkinbut: continueifnotisinstance(param,Param): param=Param(v.docment, v.anno)
# if v.default is Noneparam.set_default(ifnone(v.default,'None'))
s2[k]=Parameter(k,Parameter.KEYWORD_ONLY,annotation=param)
sigd.update(s2)
ifkeep: sigd['kwargs'] =kelse: from_f.__delwrap__=to_ffrom_f.__signature__=sig.replace(parameters=sigd.values())
returnfreturn_f
then if we set the flag:
@call_parse@delegates(A,cast_to_call_parse=True)defsome_callable(
b:int=8, # A paramter that is defined in the call method.**kwargs
):
"Some doc"passshow_doc(A)
show_doc(some_callable)
print(anno_parser(some_callable).format_help())
Outputs:
optional arguments:
-h, --help show this help message and exit
--b B A paramter that is defined in the call method. (default: 8)
--a A This is a parameter (default: 5) (default: 5)
The text was updated successfully, but these errors were encountered:
My solution here is to modify
delegates
to output kwargs withParam
type hints, thus propagating the docments through to call_parse. This solution is a tad ugly sincecast_to_call_parse=True
needs to be passed so that the delegates knows that itis supposed to change everything to Param's.
Problem
call_parse does not work if using a delegates+docments combo. For example:
Outputs:
Slightly Dirty Solution
*also buggy, it doesnt handle default values very well
then if we set the flag:
Outputs:
The text was updated successfully, but these errors were encountered: