def main(): with open(api.__file__.rstrip('c') + 'i', 'w') as stub: # Imports so that Asset et al can be resolved. # "from MOD import *" will re-export the imports from the stub, so # explicitly importing. stub.write(dedent("""\ from zipline.assets import Asset, Equity, Future from zipline.assets.futures import FutureChain from zipline.finance.cancel_policy import CancelPolicy from zipline.pipeline import Pipeline from zipline.protocol import Order from zipline.utils.events import EventRule """)) # Sort to generate consistent stub file: for api_func in sorted(TradingAlgorithm.all_api_methods(), key=attrgetter('__name__')): sig = inspect._signature_bound_method(inspect.signature(api_func)) indent = ' ' * 4 stub.write(dedent('''\ def {func_name}{func_sig}: """'''.format(func_name=api_func.__name__, func_sig=sig))) stub.write(dedent('{indent}{func_doc}'.format( func_doc=api_func.__doc__ or '\n', # handle None docstring indent=indent, ))) stub.write('{indent}"""\n\n'.format(indent=indent))
def main(): with open(api.__file__.rstrip('c') + 'i', 'w') as stub: # Imports so that Asset et al can be resolved. # "from MOD import *" will re-export the imports from the stub, so # explicitly importing. stub.write( dedent("""\ from zipline.assets import Asset, Equity, Future from zipline.assets.futures import FutureChain from zipline.finance.cancel_policy import CancelPolicy from zipline.pipeline import Pipeline from zipline.protocol import Order from zipline.utils.events import EventRule """)) # Sort to generate consistent stub file: for api_func in sorted(TradingAlgorithm.all_api_methods(), key=attrgetter('__name__')): sig = inspect._signature_bound_method(inspect.signature(api_func)) indent = ' ' * 4 stub.write( dedent('''\ def {func_name}{func_sig}: """'''.format(func_name=api_func.__name__, func_sig=sig))) stub.write( dedent('{indent}{func_doc}'.format( func_doc=api_func.__doc__ or '\n', # handle None docstring indent=indent, ))) stub.write('{indent}"""\n\n'.format(indent=indent))
def main(): with open(api.__file__.rstrip("c") + "i", "w") as stub: # Imports so that Asset et al can be resolved. # "from MOD import *" will re-export the imports from the stub, so # explicitly importing. stub.write( dedent( """\ import collections from zipline.assets import Asset, Equity, Future from zipline.assets.futures import FutureChain from zipline.finance.asset_restrictions import Restrictions from zipline.finance.cancel_policy import CancelPolicy from zipline.pipeline import Pipeline from zipline.protocol import Order from zipline.utils.events import EventRule from zipline.utils.security_list import SecurityList """ ) ) # Sort to generate consistent stub file: for api_func in sorted( TradingAlgorithm.all_api_methods(), key=attrgetter("__name__") ): stub.write("\n") sig = inspect._signature_bound_method(inspect.signature(api_func)) indent = " " * 4 stub.write( dedent( '''\ def {func_name}{func_sig}: """'''.format( func_name=api_func.__name__, func_sig=sig ) ) ) stub.write( dedent( "{indent}{func_doc}".format( # `or '\n'` is to handle a None docstring: func_doc=dedent(api_func.__doc__.lstrip()) or "\n", indent=indent, ) ) ) stub.write('{indent}"""\n'.format(indent=indent))
def _signature_from_callable( obj, follow_wrapper_chains=True, skip_bound_arg=True, sigcls=None ): """Private helper function to get signature for arbitrary callable objects. """ # We don't want to import pyobj, since that imports us if not xCallable(obj): raise TypeError("%r is not a callable object" % obj) if isinstance(obj, types.MethodType): # In this case we skip the first parameter of the underlying # function (usually `self` or `cls`). sig = _signature_from_callable( obj.__func__, follow_wrapper_chains=follow_wrapper_chains, skip_bound_arg=skip_bound_arg, sigcls=sigcls, ) if skip_bound_arg: return _signature_bound_method(sig) else: return sig # Was this function wrapped by a decorator? if follow_wrapper_chains: obj = unwrap(obj, stop=(lambda f: hasattr(f, "__signature__"))) if isinstance(obj, types.MethodType): # If the unwrapped object is a *method*, we might want to # skip its first parameter (self). # See test_signature_wrapped_bound_method for details. return _signature_from_callable( obj, follow_wrapper_chains=follow_wrapper_chains, skip_bound_arg=skip_bound_arg, sigcls=sigcls, ) try: sig = obj.__signature__ except AttributeError: pass else: if sig is not None: if not isinstance(sig, Signature): raise TypeError( "unexpected object %r in __signature__ " "attribute" % sig ) return sig try: partialmethod = obj._partialmethod except AttributeError: pass else: if isinstance(partialmethod, functools.partialmethod): # Unbound partialmethod (see functools.partialmethod) # This means, that we need to calculate the signature # as if it's a regular partial object, but taking into # account that the first positional argument # (usually `self`, or `cls`) will not be passed # automatically (as for boundmethods) wrapped_sig = _signature_from_callable( partialmethod.func, follow_wrapper_chains=follow_wrapper_chains, skip_bound_arg=skip_bound_arg, sigcls=sigcls, ) sig = _signature_get_partial(wrapped_sig, partialmethod, (None,)) first_wrapped_param = tuple(wrapped_sig.parameters.values())[0] if first_wrapped_param.kind is Parameter.VAR_POSITIONAL: # First argument of the wrapped callable is `*args`, as in # `partialmethod(lambda *args)`. return sig else: sig_params = tuple(sig.parameters.values()) assert not sig_params or first_wrapped_param is not sig_params[0] new_params = (first_wrapped_param,) + sig_params return sig.replace(parameters=new_params) if isfunction(obj) or _signature_is_functionlike(obj): # If it's a pure Python function, or an object that is duck type # of a Python function (Cython functions, for instance), then: return _signature_from_function(sigcls, obj) if _signature_is_builtin(obj): return _signature_from_builtin(sigcls, obj, skip_bound_arg=skip_bound_arg) if isinstance(obj, functools.partial): wrapped_sig = _signature_from_callable( obj.func, follow_wrapper_chains=follow_wrapper_chains, skip_bound_arg=skip_bound_arg, sigcls=sigcls, ) return _signature_get_partial(wrapped_sig, obj) sig = None if isinstance(obj, type): # obj is a class or a metaclass # First, let's see if it has an overloaded __call__ defined # in its metaclass call = _signature_get_user_defined_method(type(obj), "__call__") if call is not None: sig = _signature_from_callable( call, follow_wrapper_chains=follow_wrapper_chains, skip_bound_arg=skip_bound_arg, sigcls=sigcls, ) else: # Now we check if the 'obj' class has a '__new__' method new = _signature_get_user_defined_method(obj, "__new__") if new is not None: sig = _signature_from_callable( new, follow_wrapper_chains=follow_wrapper_chains, skip_bound_arg=skip_bound_arg, sigcls=sigcls, ) else: # Finally, we should have at least __init__ implemented init = _signature_get_user_defined_method(obj, "__init__") if init is not None: sig = _signature_from_callable( init, follow_wrapper_chains=follow_wrapper_chains, skip_bound_arg=skip_bound_arg, sigcls=sigcls, ) if sig is None: # At this point we know, that `obj` is a class, with no user- # defined '__init__', '__new__', or class-level '__call__' for base in obj.__mro__[:-1]: # Since '__text_signature__' is implemented as a # descriptor that extracts text signature from the # class docstring, if 'obj' is derived from a builtin # class, its own '__text_signature__' may be 'None'. # Therefore, we go through the MRO (except the last # class in there, which is 'object') to find the first # class with non-empty text signature. try: text_sig = base.__text_signature__ except AttributeError: pass else: if text_sig: # If 'obj' class has a __text_signature__ attribute: # return a signature based on it return _signature_fromstr(sigcls, obj, text_sig) # No '__text_signature__' was found for the 'obj' class. # Last option is to check if its '__init__' is # object.__init__ or type.__init__. if type not in obj.__mro__: # We have a class (not metaclass), but no user-defined # __init__ or __new__ for it if obj.__init__ is object.__init__ and obj.__new__ is object.__new__: # Return a signature of 'object' builtin. return signature(object) else: raise ValueError( "no signature found for builtin type %r" % obj ) elif not isinstance(obj, _NonUserDefinedCallables): # An object with __call__ # We also check that the 'obj' is not an instance of # _WrapperDescriptor or _MethodWrapper to avoid # infinite recursion (and even potential segfault) call = _signature_get_user_defined_method(type(obj), "__call__") if call is not None: try: sig = _signature_from_callable( call, follow_wrapper_chains=follow_wrapper_chains, skip_bound_arg=skip_bound_arg, sigcls=sigcls, ) except ValueError: msg = "no signature found for %r" % obj raise ValueError(msg) # from ex if sig is not None: # For classes and objects we skip the first parameter of their # __call__, __new__, or __init__ methods if skip_bound_arg: return _signature_bound_method(sig) else: return sig if isinstance(obj, types.BuiltinFunctionType): # Raise a nicer error message for builtins msg = "no signature found for builtin function %r" % obj raise ValueError(msg) raise ValueError("callable %r is not supported by signature" % obj)