Exemplo n.º 1
0
        def call(*args, **kwargs):
            def apply_method(func: self.func_type) -> obj._type.return_type:
                return utils.wrap_function_call(func, args, kwargs)

            res = obj._inst.map(
                utils.native_function(apply_method, registry=obj._registry))
            return Object(res, frame=stack.frame_snapshot(1))
Exemplo n.º 2
0
        def apply_method(other: Any) -> return_type:
            def operate(inst: self.instance_type,
                        other: self.instance_type) -> return_type:
                return self.operation(inst, other)

            operate.__name__ = self.name
            out = utils.native_function(operate)(obj, other)
            return Object(out, frame=stack.frame_snapshot(1))
Exemplo n.º 3
0
    def __class_getitem__(self, item: Any) -> Object:
        """
        Get an Unknown object with the given type
        """
        import statey as st

        typ = st.registry.get_type(item)
        return st.Object(Unknown(return_type=typ),
                         frame=stack.frame_snapshot(1))
Exemplo n.º 4
0
 def wrapper(*args, **kwargs):
     res = result(*args, **kwargs)
     expected_res = self.expected[attr](*args, **kwargs)
     new_impl = ExpectedValue(res, expected_res)
     return Object(
         new_impl,
         res._type,
         res._registry,
         frame=stack.frame_snapshot(1),
     )
Exemplo n.º 5
0
 def new(self, key: str, type: types.Type) -> Object:
     """
     Create a new symbol for the given key and schema and add it to the current namespace
     """
     self.path_parser.validate_name(key)
     if key in self.types:
         raise exc.DuplicateSymbolKey(key, self)
     self.types[key] = type
     ref = self.ref(key)
     return Object(ref, frame=stack.frame_snapshot(1))
Exemplo n.º 6
0
def wrap_function_call(
    func: Union["Function", Callable[[Any], Any]],
    args: Sequence[Any] = (),
    kwargs: Optional[Dict[str, Any]] = None,
    registry: Optional["Registry"] = None,
    return_annotation: Any = MISSING,
    frame_depth: int = 0,
) -> "Object":

    from statey.syms import api, impl, types, func as func_module, Object, stack

    if registry is None:
        from statey import registry

    if kwargs is None:
        kwargs = {}

    args = tuple(map(lambda x: Object(x, registry=registry), args))
    kwargs = {key: Object(val, registry=registry) for key, val in kwargs.items()}

    if isinstance(func, func_module.Function):
        function_obj = func
    else:
        try:
            function_obj = native_function(func, registry=registry)
        except ValueError:
            return_annotation = (
                func.return_annotation
                if is_missing(return_annotation)
                else return_annotation
            )
            return_type = registry.get_type(return_annotation)

            fields = []
            for idx, arg in enumerate(args):
                fields.append(types.Field(str(idx), arg._type))

            for name, val in kwargs.items():
                fields.append(types.Field(name, val._type))

            func_type = types.NativeFunctionType(tuple(fields), return_type)
            function_obj = func_module.NativeFunction(func_type, func)

    arguments = bind_function_args(function_obj.type, args, kwargs)
    args_encoder = registry.get_encoder(function_obj.type.args_type)
    encoded_arguments = args_encoder.encode(arguments)

    return Object(
        impl.FunctionCall(function_obj, encoded_arguments),
        registry=registry,
        frame=stack.frame_snapshot(1 + frame_depth),
    )
Exemplo n.º 7
0
 def expect(value: Any) -> Object:
     expected_obj = Object(value, obj._type, obj._registry)
     return Object(impl.ExpectedValue(obj, expected_obj),
                   frame=stack.frame_snapshot(1))
Exemplo n.º 8
0
    def __post_init__(
        self,
        impl: Any,
        type: Optional["Type"],
        registry: Optional["Registry"],
        frame: Optional[stack.FrameSnapshot],
    ) -> None:
        """
        Set up the Object instance
        """
        import statey as st
        from statey.syms.impl import ObjectImplementation

        if isinstance(impl, Object):
            obj = impl
            impl = obj._impl
            type = type or obj._type
            registry = registry or obj._registry
            frame = frame or obj._frame

        elif not isinstance(impl, ObjectImplementation):
            if registry is None:
                registry = st.registry
            obj = registry.get_object(impl)
            impl = obj._impl
            type = type or obj._type

        if registry is None:
            try:
                registry = impl.registry()
            except NotImplementedError:
                pass

        if registry is None:
            registry = st.registry

        if type is not None and not isinstance(type, st.Type):
            # Convert an annotation to a type
            type = registry.get_type(type)

        if type is None:
            try:
                type = impl.type()
            except NotImplementedError:
                pass

        if type is None:
            raise ValueError(
                f"Object implementation {impl} does not have an implied type, one must "
                f"be passed manually.")

        if frame is None:
            try:
                frame = impl.frame()
            except NotImplementedError:
                pass

        if frame is None:
            frame = stack.frame_snapshot(2)

        self.__dict__["_impl"] = impl
        self.__dict__["_type"] = type
        self.__dict__["_registry"] = registry
        self.__dict__["_frame"] = frame

        impl_accessor = base.GetattrBasedAttributeAccess(self._impl)
        self.__dict__["_inst"] = base.BoundAttributeAccess(self, impl_accessor)