@@ -29,25 +29,49 @@ def err_func2():
29
29
@types (bool , int )
30
30
def f ():
31
31
pass
32
+
32
33
return 1
33
34
34
35
@staticmethod
35
36
@types (bool )
36
37
def predicate ():
37
38
return 1
38
39
40
+ @types (x = Option ((int , float )))
41
+ def optional_func1 (self , x ):
42
+ return x
43
+
44
+ @types (int , xs = Option (ListOf (int )))
45
+ def optional_func2 (self , xs = None ):
46
+ return len (xs or [])
47
+
48
+ class Foo (object ):
49
+ pass
50
+
39
51
def test_types (self ):
40
52
str_type = '(basestring|str)' if six .PY2 else '(str|bytes)'
41
53
42
54
self .assertEqual (self .bin_func (10 , 20 ), 30 )
43
- self .assertRaisesMessage (AssertionError , 'x must be int, not dict.' , self .bin_func , {}, 20 )
44
- self .assertRaisesMessage (AssertionError , 'y must be int, not list.' , self .bin_func , 10 , [])
55
+ self .assertRaisesMessage (TypeError , 'x must be int, not dict.' , self .bin_func , {}, 20 )
56
+ self .assertRaisesMessage (TypeError , 'y must be int, not list.' , self .bin_func , 10 , [])
45
57
46
58
self .assertEqual (self .complex_func (123 , [1 , 2 ], 10 , 'abc' , 'def' , [{'x' : set ([3 , 4 , 5 ])}]), 1 )
47
- self .assertRaisesMessage (AssertionError , 'kw must be dict(%s->float), not dict.' % str_type ,
59
+ self .assertRaisesMessage (TypeError , 'kw must be dict(%s->float), not dict.' % str_type ,
48
60
self .complex_func , 123 , [1 , 2 ], 10 , 'abc' , 'def' , [{'x' : set ([3 , 4 , 5 ])}], x = '12.3' )
49
61
50
- self .assertRaisesMessage (AssertionError , 'must return bool, not int.' , self .predicate )
62
+ self .assertRaisesMessage (TypeError , 'must return bool, not int.' , self .predicate )
63
+
64
+ self .assertEqual (self .optional_func1 (None ), None )
65
+ self .assertEqual (self .optional_func1 (123 ), 123 )
66
+ self .assertEqual (self .optional_func1 (1.23 ), 1.23 )
67
+ self .assertRaisesMessage (TypeError , 'x must be (int|float|NoneType), not Foo.' ,
68
+ self .optional_func1 , self .Foo ())
69
+
70
+ self .assertEqual (self .optional_func2 (), 0 )
71
+ self .assertEqual (self .optional_func2 (None ), 0 )
72
+ self .assertEqual (self .optional_func2 ([1 , 2 , 3 ]), 3 )
73
+ self .assertRaisesMessage (TypeError , 'xs must be (list(int)|NoneType), not list.' ,
74
+ self .optional_func2 , [1 , 2 , 3.4 ])
51
75
52
76
def test_types_error (self ):
53
77
self .assertRaisesMessage (AssertionError , 'Not found argument: a' , self .err_func1 , 123 )
0 commit comments