def __init__(self, version, proxy_object, docstring="", labels=None): """ Construct a VersionedGraft object from a proxy object. You shouldn't construct a `VersionedGraft` directly; use `Workflow.set_version` or `wf.publish <.models.publish>` instead. Parameters ---------- version: str Version of the graft. This should adhere to the semantic versioning schema (https://semver.org). proxy_object: Proxytype The proxy object source of the graft. docstring: str, default "" Docstring for the VersionedGraft. labels: dict, optional Key-value pair labels to add to the VersionedGraft. Returns ------- VersionedGraft """ typespec = serialize_typespec(type(proxy_object)) graft = proxy_object.graft message = workflow_pb2.VersionedGraft( version=version, serialized_graft=json.dumps(graft), channel=_channel.__channel__, typespec=typespec, docstring=textwrap.dedent(docstring), labels=labels, ) self._object = proxy_object self._message = message
def test_init(self, stub): obj = Int(42) version = "0.0.1" docstring = "int 42" labels = { "github_url": "http://github.com/someurl", "project": "some important project", } viz_options = [ VizOption( id="viz1", bands=["red", "green", "blue"], scales=[[0, 0.4], [0, 0.4], [0, 0.4]], ), ] vg = VersionedGraft( version, obj, docstring=docstring, labels=labels, viz_options=viz_options, ) assert vg.version == version assert vg.object is obj assert vg.docstring == docstring assert vg.labels == labels assert vg._message.serialized_graft == json.dumps(obj.graft) assert vg._message.typespec == serialize_typespec(type(obj)) assert vg.channel == _channel.__channel__ assert vg._message.client_version == __version__ assert vg.type == type(obj) assert vg.viz_options == viz_options
def test_param_to_proto_non_widget(self): name = "foo" param = parameter(name, List[Int]) proto = widget.param_to_proto(param) assert isinstance(proto, widgets_pb2.Parameter) assert proto.name == name assert proto.label == "" assert proto.typespec == cereal.serialize_typespec(type(param)) assert proto.WhichOneof("widget") is None
def test_proto_to_param_non_widget(self): name = "foo" type_ = List[Int] proto = widgets_pb2.Parameter( name=name, typespec=cereal.serialize_typespec(type_), ) param = widget.proto_to_param(proto) assert type(param) is type_ assert param._name == name assert param.params == (param,) assert param.graft["returns"] == name
def test_param_to_proto_widget(self, StrWidget): name = "foo" default = "bar" label = "baz" instance = StrWidget(name, default, label) proto = widget.param_to_proto(instance) assert isinstance(proto, widgets_pb2.Parameter) assert proto.name == name assert proto.label == label assert proto.typespec == cereal.serialize_typespec(Str) which_oneof = proto.WhichOneof("widget") assert ( which_oneof == widget.WIDGET_MSG_TYPE_TO_ONEOF_NAME[StrWidget._proto_type] ) assert getattr(proto, which_oneof).default == default
def test_build(self, stub): obj = Int(42) version = "0.0.1" docstring = "int 42" labels = { "github_url": "http://github.com/someurl", "project": "some important project", } vg = VersionedGraft(version, obj, docstring=docstring, labels=labels) assert vg.version == version assert vg.object is obj assert vg.docstring == docstring assert vg.labels == labels assert vg._message.serialized_graft == json.dumps(obj.graft) assert vg._message.typespec == serialize_typespec(type(obj)) assert vg.channel == _channel.__channel__ assert vg.type == type(obj)
def test_proto_to_param(self, StrWidget): name = "foo" default = "bar" label = "baz" proto = widgets_pb2.Parameter( name=name, label=label, typespec=cereal.serialize_typespec(Str), ) sub_msg = getattr( proto, widget.WIDGET_MSG_TYPE_TO_ONEOF_NAME[StrWidget._proto_type] ) sub_msg.default = default param = widget.proto_to_param(proto) assert type(param) is StrWidget assert param._name == name assert param._label == label assert param._default == default assert param.graft["returns"] == name assert param.params == (param,)
def __init__( self, proxy_object: Proxytype, viz_options: Optional[Sequence[VizOption]] = None, client: Optional[Client] = None, ): """ Construct a PublishedGraft object from a proxy object. If the proxy object depends on any parameters (``proxy_object.params`` is not empty), it's first internally converted to a `.Function` that takes those parameters (using `.Function.from_object`). Parameters ---------- proxy_object The proxy object to publish. If it depends on parameters, ``proxy_obj`` is first converted to a `.Function` that takes those parameters. viz_options: list, default None List of `~.models.VizOption` visualization option sets. client Allows you to use a specific client instance with non-default auth and parameters """ if client is None: client = get_global_grpc_client() proxy_object = proxify(proxy_object) if len(proxy_object.params) > 0: # turn objects that depend on parameters into Functions; # use their current `.params` as the parameter annotations params = proxy_object.params proxy_object = Function.from_object(proxy_object) elif isinstance(proxy_object, Function): # generate parameter annotations from the arguments of the Function. try: # try to get parameter names directly from the graft---this will work # even for unnamed positional arguments in the typespec names = proxy_object.graft["parameters"] except KeyError: # but if it's not a function graft (likely a higher-order function returned # from another function), use the `__signature__` so positional-only arguments # still get reasonable-ish (if inaccurate) names. names = list(proxy_object.__signature__.parameters) params = tuple( parameter(name, type_) for name, type_ in zip(names, proxy_object.all_arg_types)) else: params = () typespec = serialize_typespec(type(proxy_object)) graft = proxy_object.graft proto_params = serialize_params(params) message = self._message_type( serialized_graft=json.dumps(graft), channel=client._wf_channel, client_version=__version__, typespec=typespec, parameters=proto_params, ) if viz_options: if not isinstance(viz_options, (list, tuple)): viz_options = [viz_options] message.viz_options.extend([v._message for v in viz_options]) self._client = client self._object = proxy_object self._params = params self._message = message