def assertBlame(self, cm, blamed):
     ok = cm.exception.last_responsable() in Location.from_code(blamed)
     if not ok:
         print(cm.exception.last_responsable())
         print("not in")
         print(Location.from_code(blamed))
     self.assertTrue(ok)
Exemplo n.º 2
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.º 3
0
    def test_standalone(self):
        ch = untypy.checker(lambda: int, TestStandaloneChecker.test_standalone)

        self.assertEqual(ch(42), 42)

        def myfunc(x):
            ch(x)

        with self.assertRaises(UntypyTypeError) as cm:
            myfunc("hello")

        self.assertEqual(cm.exception.expected, 'int')
        self.assertEqual(
            cm.exception.last_declared(),
            Location.from_code(TestStandaloneChecker.test_standalone))
        self.assertIn('myfunc("hello")',
                      cm.exception.last_responsable().source_lines)
Exemplo n.º 4
0
    def get_checker(self):
        if self._checker:
            return self._checker

        ctx = DefaultCreationContext(
            typevars=dict(),
            declared_location=Location.from_code(self.declared),
            checkedpkgprefixes=self.cfg.checkedprefixes)
        try:
            annotation = self.annotation()
        except NameError as ne:
            raise ctx.wrap(
                UntypyNameError(
                    f"{ne}.\nType annotation could not be resolved."))

        checker = ctx.find_checker(annotation)
        if checker is None:
            raise ctx.wrap(
                UntypyAttributeError(
                    f"\n\tUnsupported type annotation: {self.annotation}\n"))
        self._checker = checker
        return checker
Exemplo n.º 5
0
def wrap_class(a: type, cfg: Config) -> Callable:
    return WrappedType(
        a,
        DefaultCreationContext(typevars=dict(),
                               declared_location=Location.from_code(a),
                               checkedpkgprefixes=cfg.checkedprefixes))