예제 #1
0
    def testNewType(self):
        from typing import NewType
        UID = NewType("UID", int)
        uid = UID(42)

        self.assertTrue(isconsistent(uid, UID))
        self.assertTrue(isconsistent(1, UID))
예제 #2
0
 def testSet(self):
     from typing import Set
     self.assertTrue(isconsistent(set(), Set[str]))
     self.assertFalse(isconsistent({}, Set[str]))
     self.assertTrue(isconsistent({"foo"}, Set[str]))
     self.assertTrue(isconsistent({1, 2, 3}, Set[int]))
     self.assertFalse(isconsistent({1, 2, "foo"}, Set[int]))
예제 #3
0
    def testType(self):
        from typing import Type

        class X:
            pass

        class Y(X):
            pass

        self.assertTrue(isconsistent(X, Type[X]))
        self.assertTrue(isconsistent(Y, Type[X]))
        self.assertFalse(isconsistent(X(), Type[X]))
예제 #4
0
    def testTuple(self):
        from typing import Tuple
        self.assertTrue(isconsistent((1, "foo"), Tuple[int, str]))
        self.assertFalse(isconsistent((1, ), Tuple[int, str]))
        self.assertFalse(isconsistent(("foo", 1), Tuple[int, str]))

        self.assertTrue(isconsistent((), Tuple[int, ...]))
        self.assertTrue(isconsistent((1, ), Tuple[int, ...]))
        self.assertTrue(isconsistent((1, 2), Tuple[int, ...]))
        self.assertTrue(isconsistent((1, 2, 3), Tuple[int, ...]))
        self.assertFalse(isconsistent((1, 2, 3, "foo"), Tuple[int, ...]))

        self.assertTrue(isconsistent((), Tuple[()]))
        self.assertFalse(isconsistent((1, ), Tuple[()]))
예제 #5
0
    def testDict(self):
        from typing import Dict
        self.assertTrue(isconsistent({}, Dict[str, int]))
        self.assertFalse(isconsistent([], Dict[str, int]))
        self.assertTrue(isconsistent({"foo": 1}, Dict[str, int]))
        self.assertFalse(isconsistent({"foo": "bar"}, Dict[str, int]))

        from typing import Mapping
        self.assertTrue(isconsistent({}, Mapping[str, int]))
        self.assertFalse(isconsistent([], Mapping[str, int]))
        self.assertTrue(isconsistent({"foo": 1}, Mapping[str, int]))
        self.assertFalse(isconsistent({"foo": "bar"}, Mapping[str, int]))
예제 #6
0
    def testNamedDict(self):
        d = {
            'x': 1,
            'y': 1,
            'z': 2,
        }
        self.assertTrue(isconsistent({}, EmptyDict))
        self.assertFalse(isconsistent({}, T1))
        self.assertTrue(isconsistent(d, T1))
        self.assertTrue(isconsistent(d, T2))
        self.assertFalse(isconsistent(d, T3))

        d['foo'] = 4
        self.assertFalse(isconsistent(d, T3))
        d['foo'] = 'bar'
        self.assertTrue(isconsistent(d, T3))

        # TODO
        # del d['z']
        # self.assertTrue(isconsistent(d, T3))
예제 #7
0
    def testUnion(self):
        t = Union[int, str]
        self.assertFalse(isconsistent(None, t))
        self.assertTrue(isconsistent(1, t))
        self.assertTrue(isconsistent("foo", t))
        self.assertFalse(isconsistent(1.0, t))

        t = Optional[int]
        self.assertTrue(isconsistent(None, t))
        self.assertTrue(isconsistent(1, t))
        self.assertFalse(isconsistent("foo", t))

        class X:
            pass

        t = Optional[Union[float, X]]
        self.assertTrue(isconsistent(None, t))
        self.assertTrue(isconsistent(1, t))
        self.assertFalse(isconsistent("foo", t))

        t = Literal["x", "y"]
        self.assertTrue(isconsistent("y", t))
        self.assertFalse(isconsistent("z", t))
예제 #8
0
 def testAny(self):
     from typing import Any
     self.assertTrue(isconsistent(None, Any))
     self.assertTrue(isconsistent({}, Any))
     self.assertTrue(isconsistent(int, Any))
예제 #9
0
    def testList(self):
        from typing import List
        self.assertFalse(isconsistent(None, List[int]))
        self.assertTrue(isconsistent([], List[int]))
        self.assertFalse(isconsistent({}, List[int]))
        self.assertTrue(isconsistent([1], List[int]))
        self.assertFalse(isconsistent([1, "foo"], List[int]))

        from typing import Iterable
        self.assertTrue(isconsistent([1], Iterable[int]))

        from typing import Sequence
        self.assertFalse(isconsistent(None, Sequence[int]))
        self.assertTrue(isconsistent([], Sequence[int]))
        self.assertFalse(isconsistent({}, Sequence[int]))
        self.assertTrue(isconsistent([1], Sequence[int]))
        self.assertFalse(isconsistent([1, "foo"], Sequence[int]))
예제 #10
0
    def testBasic(self):
        self.assertTrue(isconsistent(None, None))
        self.assertTrue(isconsistent(None, object))
        self.assertTrue(isconsistent(1, object))

        self.assertTrue(isconsistent(1, int))
        self.assertTrue(isconsistent(True, int))
        self.assertTrue(isconsistent(1, float))
        self.assertTrue(isconsistent(1.0, float))
        self.assertTrue(isconsistent(1, complex))
        self.assertTrue(isconsistent(1.0, complex))
        self.assertTrue(isconsistent("foo", str))
        self.assertTrue(isconsistent({}, dict))
        self.assertTrue(isconsistent([], list))

        class X:
            pass

        class Y(X):
            pass

        self.assertTrue(isconsistent(Y(), object))
        self.assertTrue(isconsistent(Y(), X))
        self.assertTrue(isconsistent(Y(), Y))

        # and some inconsistent ones
        self.assertFalse(isconsistent(1, None))
        self.assertFalse(isconsistent(1, str))
        self.assertFalse(isconsistent(1, bool))
        self.assertFalse(isconsistent({}, list))
        self.assertFalse(isconsistent(X(), Y))

        from typing import NoReturn
        self.assertFalse(isconsistent(None, NoReturn))
        self.assertFalse(isconsistent(NoReturn, NoReturn))