def test_union_protocols(self):
        class U1:
            @untypy.patch
            def meth(self) -> str:
                return "s"

        class U2:
            @untypy.patch
            def meth(self) -> int:
                return 42

            @untypy.patch
            def meth2(self) -> int:
                return 42

        # when wrapping order matters
        UnionFactory() \
            .create_from(Union[U1, U2], DummyDefaultCreationContext()) \
            .check_and_wrap(U1(), DummyExecutionContext()) \
            .meth()
        UnionFactory() \
            .create_from(Union[U1, U2], DummyDefaultCreationContext()) \
            .check_and_wrap(U2(), DummyExecutionContext()) \
            .meth()
        UnionFactory() \
            .create_from(Union[U2, U1], DummyDefaultCreationContext()) \
            .check_and_wrap(U1(), DummyExecutionContext()) \
            .meth()
        UnionFactory() \
            .create_from(Union[U2, U1], DummyDefaultCreationContext()) \
            .check_and_wrap(U2(), DummyExecutionContext()) \
            .meth()
    def test_wrap(self):
        checker = UnionFactory().create_from(Union[int, str], DummyDefaultCreationContext())
        res = checker.check_and_wrap(1, DummyExecutionContext())
        self.assertEqual(1, res)

        res = checker.check_and_wrap("2", DummyExecutionContext())
        self.assertEqual("2", res)
    def test_not_allowing_multiple_callables(self):
        with self.assertRaises(UntypyAttributeError):
            checker = UnionFactory().create_from(Union[int, str, Callable[[int], str], Callable[[str], str]],
                                                 DummyDefaultCreationContext())

        with self.assertRaises(UntypyAttributeError):
            checker = UnionFactory().create_from(Union[int, Callable[[int], str],
                                                       Union[Callable[[str], str], list[int]]],
                                                 DummyDefaultCreationContext())
    def test_wrap_negative(self):
        checker = UnionFactory().create_from(Union[int, str], DummyDefaultCreationContext())
        with self.assertRaises(UntypyTypeError) as cm:
            res = checker.check_and_wrap(23.5, DummyExecutionContext())

        (t, i) = cm.exception.next_type_and_indicator()
        i = i.rstrip()

        self.assertEqual(t, "Union[int, str]")
        self.assertEqual(i, "^^^^^^^^^^^^^^^")

        # This DummyExecutionContext is responsable
        self.assertEqual(cm.exception.last_responsable().file, "dummy")
示例#5
0
    def test_unions_simple_types_negative(self):
        class U3:
            def meth(self) -> None:
                pass

        class U4:
            def meth(self) -> None:
                pass

            def somethingelse(self) -> None:
                pass

        # this should also be fine. A and B don't have any signatures,
        # so protocol like wrapping does not apply
        UnionFactory().create_from(Union[A, B], DummyDefaultCreationContext())

        # this must be fine: Names of Signatures differ.
        UnionFactory().create_from(Union[U3, U4], DummyDefaultCreationContext())
示例#6
0
    def test_unions_simple_types_negative(self):
        class U1:
            def meth(self) -> None:
                pass

        class U2:
            def meth(self) -> None:
                pass

        with self.assertRaises(UntypyAttributeError):
            # Should fail because both have the same methods
            # Wrapping cannot distinguish
            UnionFactory().create_from(Union[U1, U2], DummyDefaultCreationContext())