예제 #1
0
 def test_either_as_optional(self):
     optint = Union((self.int, None))
     self.assertEqual(str(optint), 'Optional[int]')
     optfloat = Union((None, self.float))
     self.assertEqual(str(optfloat), 'Optional[float]')
     eithernone = Union((self.int, self.float, None))
     self.assertEqual(str(eithernone), 'Union[None, float, int]')
예제 #2
0
    def test_either(self):
        i = self.int
        f = self.float
        s = Instance(str)

        e2 = Union((i, f))
        self.assertEqual(len(e2.types), 2)
        self.assertEqual(str(e2), 'Union[float, int]')

        self.assertEqual(e2, Union((i, f)))
        self.assertEqual(e2, Union((f, i)))
        self.assertNotEqual(e2, Union((i, s)))
        self.assertNotEqual(e2, Union((i, f, s)))
        self.assertNotEqual(Union((i, f, s)), e2)
        self.assertNotEqual(e2, i)
예제 #3
0
    def test_combine_types(self):
        i = self.int
        f = self.float
        s = Instance(str)
        c = Instance(complex)

        class Foo:
            pass

        o = Instance(Foo)

        # Simple types
        self.assert_combine(i, i, i)
        self.assert_combine(s, s, s)
        self.assert_combine(i, s, Union((i, s)))
        self.assert_combine(i, None, Union((i, None)))
        # Unknowns
        self.assert_combine(i, Unknown(), i)
        self.assert_combine(Unknown(), Unknown(), Unknown())
        # Union types
        self.assert_combine(o, Union((f, s)), Union((o, f, s)))
        self.assert_combine(i, Union((i, s)), Union((i, s)))
        self.assert_combine(Union((o, f)), Union((o, s)), Union((o, f, s)))
        # Tuple types
        self.assert_combine(Tuple([i, i]), Tuple([i, i]), Tuple([i, i]))
        self.assert_combine(Tuple([i, i]), Tuple([o, s]),
                            Tuple([Union([o, i]), Union([s, i])]))
        # Numeric types
        self.assert_combine(i, f, f)
        self.assert_combine(i, c, c)
        self.assert_combine(c, f, c)
        # Unions with numerics
        self.assert_combine(i, Union((o, f)), Union((o, f)))
        self.assert_combine(Union((o, f)), i, Union((o, f)))
        self.assert_combine(Union((o, i)), f, Union((o, f)))
        # Tuples with numerics
        self.assert_combine(Tuple([i, i]), Tuple([f, i]), Tuple([f, i]))
        self.assert_combine(Tuple([i, i]), Tuple([f, o]),
                            Tuple([f, Union((i, o))]))
        self.assert_combine(Tuple([f, i]), Tuple([i, o]),
                            Tuple([f, Union((i, o))]))