def test_issubclass_union(self): self.assertTrue(issubclass(union(int, float), union(int, float))) self.assertTrue(issubclass(union(int, float), union(float, int))) self.assertTrue(issubclass(union(int, float), union(int, float, bool))) self.assertFalse(issubclass(union(int, float), union(int, str))) self.assertFalse(issubclass(union(int, str, float), union(int, float)))
def test_issubclass_single_class(self): class Test1: pass class Test2: pass class Other: pass self.assertTrue(issubclass(Test1, union(Test1, Test2))) self.assertTrue(issubclass(Test2, union(Test1, Test2))) self.assertFalse(issubclass(Other, union(Test1, Test2)))
def test_isinstance_classes(self): class Test1: pass class Test2: pass class Other: pass self.assertIsInstance(Test1(), union(Test1, Test2)) self.assertIsInstance(Test2(), union(Test1, Test2)) self.assertNotIsInstance(Other(), union(Test1, Test2))
def test_issubclass_single_interface(self): class Test1(Interface): def test1(self) -> int: pass class Test2(Interface): def test2(self) -> str: pass class Other(Interface): pass class Other2: pass self.assertTrue(issubclass(Test1, union(Test1, Test2))) self.assertTrue(issubclass(Test2, union(Test1, Test2))) self.assertFalse(issubclass(Other, union(Test1, Test2))) self.assertFalse(issubclass(Other2, union(Test1, Test2)))
def test_isinstance_interfaces(self): class Test1(Interface): def test1() -> int: pass class Test1Implementation: def test1(self) -> int: return 1 class Test2(Interface): def test2() -> str: pass class Test2Implementation: def test2(self) -> str: return 1 class Other: pass self.assertIsInstance(Test1Implementation(), union(Test1, Test2)) self.assertIsInstance(Test2Implementation(), union(Test1, Test2)) self.assertNotIsInstance(Other(), union(Test1, Test2))
def test_isinstance_mixed(self): class Test1: pass class Test2(Interface): def test2() -> str: pass class Test2Implementation: def test2() -> str: return 1 class Other: pass self.assertIsInstance(1, union(int, Test1, Test2)) self.assertIsInstance(Test1(), union(int, Test1, Test2)) self.assertIsInstance(Test2Implementation(), union(int, Test1, Test2)) self.assertNotIsInstance('string', union(int, Test1, Test2)) self.assertNotIsInstance(Other(), union(int, Test1, Test2))
def test_isinstance_bultin_types(self): self.assertIsInstance(1, union(int, float)) self.assertIsInstance(1.2, union(int, float)) self.assertNotIsInstance('string', union(int, float)) self.assertNotIsInstance([], union(int, float)) self.assertNotIsInstance(tuple(), union(int, float)) self.assertNotIsInstance({}, union(int, float))
def test_issubclass_single_mixed(self): class Test1(Interface): def test1(self) -> int: pass class Test2: pass self.assertTrue(issubclass(int, union(int, Test1, Test2))) self.assertTrue(issubclass(Test1, union(int, Test1, Test2))) self.assertTrue(issubclass(Test2, union(int, Test1, Test2))) self.assertTrue(issubclass(int, union(int, Test1, Test2))) self.assertTrue(issubclass(Test1, union(int, Test1, Test2))) self.assertTrue(issubclass(Test2, union(int, Test1, Test2)))
def test_issubclass_single_builtin_type(self): self.assertTrue(issubclass(int, union(int, float))) self.assertTrue(issubclass(float, union(int, float))) self.assertFalse(issubclass(str, union(int, float))) self.assertFalse(issubclass(list, union(int, float)))
def test(self, a: union(int, float)) -> int: return 1
def test(a: union(int, float, Decimal)): return a
def test(a: union(int, float)) -> int: pass
def test_union_is_type(self): self.assertIsInstance(union(int, float), type)
def test(a) -> union(int, float): return a
def test() -> union(int, float): pass
def test() -> union(int, float, bool): pass
def test(self) -> union(float, int): return 1
def test(self) -> union(int, float): return 1
class TestInterface(Interface): x = union(int, str)