Exemplo n.º 1
0
    def wrap(self, err: UntypyTypeError) -> UntypyTypeError:
        (original_expected, _ind) = err.next_type_and_indicator()
        err = ArgumentExecutionContext(self.wf, None, self.arg_name).wrap(err)

        responsable = WrappedFunction.find_location(self.wf)

        (decl, ind) = err.next_type_and_indicator()
        err = err.with_frame(
            Frame(decl,
                  ind,
                  declared=self.wf.declared(),
                  responsable=responsable))

        err = err.with_note(
            f"Argument {self.arg_name} of method {WrappedFunction.find_original(self.wf).__name__} violates the type declared by the {self.wf.protocol.protocol_type()} {self.wf.protocol.proto.__name__}."
        )
        err = err.with_note(
            f"Annotation {original_expected} is incompatible with the {self.wf.protocol.protocol_type()}'s annotation {self.wf.checker_for(self.arg_name).describe()}."
        )

        previous_chain = UntypyTypeError(
            self.me, f"{self.wf.protocol.protoname()}"
        ).with_header(
            f"{type(self.me).__name__} does not implement {self.wf.protocol.protocol_type()} {self.wf.protocol.protoname()} correctly."
        )

        previous_chain = self.ctx.wrap(previous_chain)
        # err = err.with_inverted_responsibility_type()

        return err.with_previous_chain(previous_chain)
Exemplo n.º 2
0
    def wrap(self, err: UntypyTypeError) -> UntypyTypeError:
        (original_expected, _ind) = err.next_type_and_indicator()
        err = ArgumentExecutionContext(self.tc.inner, None,
                                       self.arg_name).wrap(err)

        func_decl = WrappedFunction.find_location(self.tc.inner)

        name = WrappedFunction.find_original(self.tc.inner).__name__

        (decl, ind) = err.next_type_and_indicator()
        err = err.with_frame(
            Frame(decl, ind, declared=None, responsable=func_decl))

        previous_chain = UntypyTypeError(
            f"def {name}{self.tc.inner.describe()}", f"{self.tc.describe()}")
        (decl, ind) = previous_chain.next_type_and_indicator()
        previous_chain = previous_chain.with_frame(
            Frame(decl, ind, declared=func_decl, responsable=None))

        err = err.with_note(
            f"The argument '{self.arg_name}' of method '{name}' violates the signature of {self.tc.describe()}."
        )

        previous_chain = self.upper.wrap(previous_chain)

        return err.with_previous_chain(previous_chain)
Exemplo n.º 3
0
    def wrap(self, err: UntypyTypeError) -> UntypyTypeError:
        (original_expected, _ind) = err.next_type_and_indicator()
        err = ArgumentExecutionContext(self.wf, None, self.arg_name).wrap(err)

        responsable = WrappedFunction.find_location(self.wf)

        (decl, ind) = err.next_type_and_indicator()
        err = err.with_frame(
            Frame(decl,
                  ind,
                  declared=self.wf.declared(),
                  responsable=responsable))

        err = err.with_note(
            f"The argument '{self.arg_name}' of method '{WrappedFunction.find_original(self.wf).__name__}' violates the {self.wf.protocol.protocol_type()} '{self.wf.protocol.proto.__name__}'."
        )
        err = err.with_note(
            f"The annotation '{original_expected}' is incompatible with the {self.wf.protocol.protocol_type()}'s annotation '{self.wf.checker_for(self.arg_name).describe()}'\nwhen checking against the following value:"
        )

        previous_chain = UntypyTypeError(
            self.me, f"{self.wf.protocol.protoname()}"
        ).with_note(
            f"Type '{type(self.me).__name__}' does not implement {self.wf.protocol.protocol_type()} '{self.wf.protocol.protoname()}' correctly."
        )

        previous_chain = self.ctx.wrap(previous_chain)
        # err = err.with_inverted_responsibility_type()

        return err.with_previous_chain(previous_chain)
Exemplo n.º 4
0
    def wrap(self, err: UntypyTypeError) -> UntypyTypeError:
        err = ReturnExecutionContext(self.wf).wrap(err)

        if err.responsibility_type is self.invert:
            return err

        responsable = WrappedFunction.find_location(self.wf)
        (decl, ind) = err.next_type_and_indicator()
        err = err.with_inverted_responsibility_type()
        err = err.with_frame(
            Frame(decl,
                  ind,
                  declared=self.wf.declared(),
                  responsable=responsable))

        inner = self.wf.inner
        if isinstance(inner, WrappedFunction):
            err = err.with_note(
                f"The return value of method '{WrappedFunction.find_original(self.wf).__name__}' does violate the {self.wf.protocol.protocol_type()} '{self.wf.protocol.proto.__name__}'."
            )
            err = err.with_note(
                f"The annotation '{inner.checker_for('return').describe()}' is incompatible with the {self.wf.protocol.protocol_type()}'s annotation '{self.wf.checker_for('return').describe()}'\nwhen checking against the following value:"
            )

        previous_chain = UntypyTypeError(
            self.me, f"{self.wf.protocol.protoname()}"
        ).with_note(
            f"Type '{type(self.me).__name__}' does not implement {self.wf.protocol.protocol_type()} '{self.wf.protocol.protoname()}' correctly."
        )

        previous_chain = self.ctx.wrap(previous_chain)
        return err.with_previous_chain(previous_chain)
Exemplo n.º 5
0
    def wrap(self, err: UntypyTypeError) -> UntypyTypeError:
        next_type, indicator = err.next_type_and_indicator()

        err = err.with_frame(
            Frame(f"list[{next_type}]", (" " * len("list[") + indicator), None,
                  None))
        return self.upper.wrap(err)
Exemplo n.º 6
0
    def wrap(self, err: UntypyTypeError) -> UntypyTypeError:
        (next_ty, indicator) = err.next_type_and_indicator()
        return_id = IndicatorStr(next_ty, indicator)

        original = WrappedFunction.find_original(self.fn)

        try:
            signature = inspect.signature(original)  # TODO:!!! FIX BUILTINS
            front_sig = []
            for name in signature.parameters:
                front_sig.append(
                    f"{name}: {self.fn.checker_for(name).describe()}")
            front_sig = f"{original.__name__}(" + (
                ", ".join(front_sig)) + ") -> "
            return_id = IndicatorStr(front_sig) + return_id
        except:
            return_id = IndicatorStr("???")

        declared = WrappedFunction.find_location(self.fn)
        return err.with_frame(
            Frame(
                return_id.ty,
                return_id.indicator,
                declared=declared,
                responsable=declared,
            ))
Exemplo n.º 7
0
    def wrap(self, err: UntypyTypeError) -> UntypyTypeError:
        declared = []
        if isinstance(self.declared, Location):
            declared.append(self.declared)
        if isinstance(self.declared, list):
            declared.extend(self.declared)

        responsable = []
        if isinstance(self.responsable, Location):
            responsable.append(self.responsable)
        if isinstance(self.responsable, list):
            responsable.extend(self.responsable)

        while len(declared) < len(responsable):
            declared.append(None)
        while len(declared) > len(responsable):
            responsable.append(None)

        for (d, r) in zip(declared, responsable):
            (t, i) = err.next_type_and_indicator()
            err = err.with_frame(Frame(t, i, d, r))

        if self.upper_ctx is not None:
            return self.upper_ctx.wrap(err)
        else:
            return err
Exemplo n.º 8
0
    def wrap(self, err: UntypyTypeError) -> UntypyTypeError:
        type_declared = self.name() + "["
        indicator = " " * len(type_declared)

        for i, checker in enumerate(self.checkers):
            if i == self.idx:
                next_type, next_indicator = err.next_type_and_indicator()
                type_declared += next_type
                indicator += next_indicator
            else:
                type_declared += checker.describe()
                indicator += " " * len(checker.describe())

            if i != len(self.checkers) - 1:  # not last element
                type_declared += ", "
                indicator += "  "

        type_declared += "]"

        err = err.with_frame(
            Frame(
                type_declared,
                indicator,
                declared=self.declared(),
                responsable=self.responsable(),
            ))

        return self.upper.wrap(err)
Exemplo n.º 9
0
 def wrap(self, err: UntypyTypeError) -> UntypyTypeError:
     t, i = err.next_type_and_indicator()
     return err.with_frame(
         Frame(t,
               i,
               responsable=Location.from_stack(self.caller),
               declared=Location.from_code(self.declared)))
Exemplo n.º 10
0
    def wrap(self, err: UntypyTypeError) -> UntypyTypeError:
        (t, i) = err.next_type_and_indicator()

        return err.with_frame(
            Frame(t,
                  i,
                  declared=None,
                  responsable=Location(file="dummy", line_no=0, line_span=1)))
Exemplo n.º 11
0
    def wrap(self, err: UntypyTypeError) -> UntypyTypeError:
        (nt, ni) = err.next_type_and_indicator()

        if nt == err.expected and nt == self.bv.inner.describe():
            err.expected = self.bv.describe()
            err.expected_indicator = "^" * len(self.bv.describe())

        return self.upper.wrap(err)
 def check(self, arg: Any, ctx: ExecutionContext) -> None:
     res = self.callable(arg)
     if not res:
         exp = self.annotated.describe()
         # raise error on falsy value
         err = UntypyTypeError(given=arg, expected=exp)
         if self.annotated.is_anonymous:
             err = err.with_note(f"condition in {exp} does not hold")
         (t, i) = err.next_type_and_indicator()
         for info in self.annotated.info:
             err = err.with_note("    - " + info)
         raise ctx.wrap(err)
Exemplo n.º 13
0
    def wrap(self, err: UntypyTypeError) -> UntypyTypeError:
        offset = self.ch.describe().find("[") + 1

        (t, i) = err.next_type_and_indicator()

        err = err.with_frame(
            Frame(type_declared=self.ch.describe(),
                  indicator_line=(" " * offset) + i,
                  declared=None,
                  responsable=None))

        return self.upper.wrap(err)
Exemplo n.º 14
0
    def wrap(self, err: UntypyTypeError) -> UntypyTypeError:
        (next_ty, indicator) = err.next_type_and_indicator()
        error_id = IndicatorStr(next_ty, indicator)

        original = WrappedFunction.find_original(self.fn)
        try:
            signature = inspect.signature(original)
        except ValueError:
            # fails on some built-ins
            signature = inspect.signature(self.fn)

        wf = None
        if (hasattr(self.fn, '__wf')):
            wf = getattr(self.fn, '__wf')
        elif isinstance(self.fn, WrappedFunction):
            wf = self.fn

        arglist = []
        for name in signature.parameters:
            if name is self.argument_name:
                arglist.append(IndicatorStr(f"{name}: ") + error_id)
            else:
                if wf is not None:
                    arglist.append(
                        IndicatorStr(
                            f"{name}: {wf.checker_for(name).describe()}"))
                else:
                    arglist.append(IndicatorStr(f"{name}"))

        id = IndicatorStr(f"{format_name(original)}(") + IndicatorStr(
            ", ").join(arglist)

        if wf is not None:
            id += IndicatorStr(f") -> {wf.checker_for('return').describe()}")
        else:
            id += IndicatorStr(f")")

        if self.declared is None:
            declared = WrappedFunction.find_location(self.fn)
        else:
            declared = self.declared

        if self.stack is not None:
            responsable = Location.from_stack(self.stack)
        else:
            responsable = None

        frame = Frame(id.ty,
                      id.indicator,
                      declared=declared,
                      responsable=responsable)
        return err.with_frame(frame)
Exemplo n.º 15
0
    def wrap(self, err: UntypyTypeError) -> UntypyTypeError:
        desc = lambda s: s.describe()
        front_str = f"Callable[[{', '.join(map(desc, self.fn.argument_checker))}], "
        responsable = WrappedFunction.find_location(self.fn.inner)

        if self.invert:
            (next_ty, indicator) = err.next_type_and_indicator()
            err = ReturnExecutionContext(self.fn.inner).wrap(err)
            err = err.with_frame(
                Frame(f"{front_str}{next_ty}]",
                      (" " * len(front_str)) + indicator,
                      declared=None,
                      responsable=responsable))
            err = err.with_inverted_responsibility_type()
            return self.upper.wrap(err)

        (next_ty, indicator) = err.next_type_and_indicator()
        err = err.with_frame(
            Frame(f"{front_str}{next_ty}]", (" " * len(front_str)) + indicator,
                  declared=None,
                  responsable=responsable))

        return self.upper.wrap(err)
Exemplo n.º 16
0
    def declared_and_indicator(self, err: UntypyTypeError) -> Tuple[str, str]:
        (next_ty, indicator) = err.next_type_and_indicator()
        front_types = []
        back_types = []
        for n, ch in enumerate(self.fn.argument_checker):
            if n < self.argument_num:
                front_types.append(ch.describe())
            elif n > self.argument_num:
                back_types.append(ch.describe())

        l = len(f"Callable[[{', '.join(front_types)}")
        if len(front_types) > 0:
            l += len(', ')

        return f"Callable[[{', '.join(front_types + [next_ty] + back_types)}], {self.fn.return_checker.describe()}]", \
               (" " * l) + indicator
Exemplo n.º 17
0
    def check(self, arg: Any, ctx: ExecutionContext) -> None:
        res = self.callable(arg)
        if not res:
            # raise error on falsy value
            err = UntypyTypeError(given=arg,
                                  expected=self.annotated.describe())
            err = err.with_note(
                f"\n\nNote: Assertion in Callable failed with {repr(res)}.")
            (t, i) = err.next_type_and_indicator()
            err = err.with_frame(
                Frame(t, i, WrappedFunction.find_location(self.callable),
                      None))

            for info in self.annotated.info:
                err = err.with_note("    - " + info)

            raise ctx.wrap(err)
Exemplo n.º 18
0
    def wrap(self, err: UntypyTypeError) -> UntypyTypeError:
        (next_ty, indicator) = err.next_type_and_indicator()
        return_id = IndicatorStr(next_ty, indicator)

        original = WrappedFunction.find_original(self.fn)

        try:
            signature = inspect.signature(original)  # TODO:!!! FIX BUILTINS
            front_sig = []
            for name in signature.parameters:
                front_sig.append(
                    f"{name}: {self.fn.checker_for(name).describe()}")
            front_sig = f"{format_name(original)}(" + (
                ", ".join(front_sig)) + ") -> "
            return_id = IndicatorStr(front_sig) + return_id
        except:
            return_id = IndicatorStr("???")

        declared = WrappedFunction.find_location(self.fn)
        responsable = declared

        if responsable is not None:
            if err.expected is not None and err.given is None:
                # Missing Return-Value?
                err = err.with_note("Did you miss a return statement?")
                last_line = responsable.line_no + responsable.line_span - 1
                responsable = responsable.narrow_in_span(
                    (responsable.file, last_line))
            else:
                responsable = responsable.narrow_in_span(self.reti_loc)

        return err.with_frame(
            Frame(
                return_id.ty,
                return_id.indicator,
                declared=declared,
                responsable=responsable,
            ))
Exemplo n.º 19
0
 def wrap(self, err: UntypyTypeError) -> UntypyTypeError:
     next_type, indicator = err.next_type_and_indicator()
     return err.with_frame(
         Frame(f"list[{next_type}]", (" " * len("list[") + indicator),
               declared=self.declared,
               responsable=Location.from_stack(self.stack)))