5
5
6
6
def test_deprecate_positional_args_warns_for_function ():
7
7
@_deprecate_positional_args ("v0.1" )
8
- def f1 (a , b , * , c = 1 , d = 1 ):
9
- pass
8
+ def f1 (a , b , * , c = "c" , d = "d" ):
9
+ return a , b , c , d
10
+
11
+ result = f1 (1 , 2 )
12
+ assert result == (1 , 2 , "c" , "d" )
13
+
14
+ result = f1 (1 , 2 , c = 3 , d = 4 )
15
+ assert result == (1 , 2 , 3 , 4 )
10
16
11
17
with pytest .warns (FutureWarning , match = r".*v0.1" ):
12
- f1 (1 , 2 , 3 )
18
+ result = f1 (1 , 2 , 3 )
19
+ assert result == (1 , 2 , 3 , "d" )
13
20
14
21
with pytest .warns (FutureWarning , match = r"Passing 'c' as positional" ):
15
- f1 (1 , 2 , 3 )
22
+ result = f1 (1 , 2 , 3 )
23
+ assert result == (1 , 2 , 3 , "d" )
16
24
17
25
with pytest .warns (FutureWarning , match = r"Passing 'c, d' as positional" ):
18
- f1 (1 , 2 , 3 , 4 )
26
+ result = f1 (1 , 2 , 3 , 4 )
27
+ assert result == (1 , 2 , 3 , 4 )
19
28
20
29
@_deprecate_positional_args ("v0.1" )
21
- def f2 (a = 1 , * , b = 1 , c = 1 , d = 1 ):
22
- pass
30
+ def f2 (a = "a" , * , b = "b" , c = "c" , d = "d" ):
31
+ return a , b , c , d
23
32
24
33
with pytest .warns (FutureWarning , match = r"Passing 'b' as positional" ):
25
- f2 (1 , 2 )
34
+ result = f2 (1 , 2 )
35
+ assert result == (1 , 2 , "c" , "d" )
26
36
27
37
@_deprecate_positional_args ("v0.1" )
28
- def f3 (a , * , b = 1 , ** kwargs ):
29
- pass
38
+ def f3 (a , * , b = "b" , ** kwargs ):
39
+ return a , b , kwargs
30
40
31
41
with pytest .warns (FutureWarning , match = r"Passing 'b' as positional" ):
32
- f3 (1 , 2 )
42
+ result = f3 (1 , 2 )
43
+ assert result == (1 , 2 , {})
33
44
34
- with pytest .raises (TypeError , match = r"Cannot handle positional-only params" ):
45
+ with pytest .warns (FutureWarning , match = r"Passing 'b' as positional" ):
46
+ result = f3 (1 , 2 , f = "f" )
47
+ assert result == (1 , 2 , {"f" : "f" })
35
48
36
- @_deprecate_positional_args ("v0.1" )
37
- def f4 (a , / , * , b = 2 , ** kwargs ):
38
- pass
49
+ @_deprecate_positional_args ("v0.1" )
50
+ def f4 (a , / , * , b = "b" , ** kwargs ):
51
+ return a , b , kwargs
52
+
53
+ result = f4 (1 )
54
+ assert result == (1 , "b" , {})
55
+
56
+ result = f4 (1 , b = 2 , f = "f" )
57
+ assert result == (1 , 2 , {"f" : "f" })
58
+
59
+ with pytest .warns (FutureWarning , match = r"Passing 'b' as positional" ):
60
+ result = f4 (1 , 2 , f = "f" )
61
+ assert result == (1 , 2 , {"f" : "f" })
39
62
40
63
with pytest .raises (TypeError , match = r"Keyword-only param without default" ):
41
64
@@ -47,47 +70,71 @@ def f5(a, *, b, c=3, **kwargs):
47
70
def test_deprecate_positional_args_warns_for_class ():
48
71
class A1 :
49
72
@_deprecate_positional_args ("v0.1" )
50
- def __init__ (self , a , b , * , c = 1 , d = 1 ):
51
- pass
73
+ def method (self , a , b , * , c = "c" , d = "d" ):
74
+ return a , b , c , d
75
+
76
+ result = A1 ().method (1 , 2 )
77
+ assert result == (1 , 2 , "c" , "d" )
78
+
79
+ result = A1 ().method (1 , 2 , c = 3 , d = 4 )
80
+ assert result == (1 , 2 , 3 , 4 )
52
81
53
82
with pytest .warns (FutureWarning , match = r".*v0.1" ):
54
- A1 (1 , 2 , 3 )
83
+ result = A1 ().method (1 , 2 , 3 )
84
+ assert result == (1 , 2 , 3 , "d" )
55
85
56
86
with pytest .warns (FutureWarning , match = r"Passing 'c' as positional" ):
57
- A1 (1 , 2 , 3 )
87
+ result = A1 ().method (1 , 2 , 3 )
88
+ assert result == (1 , 2 , 3 , "d" )
58
89
59
90
with pytest .warns (FutureWarning , match = r"Passing 'c, d' as positional" ):
60
- A1 (1 , 2 , 3 , 4 )
91
+ result = A1 ().method (1 , 2 , 3 , 4 )
92
+ assert result == (1 , 2 , 3 , 4 )
61
93
62
94
class A2 :
63
95
@_deprecate_positional_args ("v0.1" )
64
- def __init__ (self , a = 1 , b = 1 , * , c = 1 , d = 1 ):
65
- pass
96
+ def method (self , a = 1 , b = 1 , * , c = "c" , d = "d" ):
97
+ return a , b , c , d
66
98
67
99
with pytest .warns (FutureWarning , match = r"Passing 'c' as positional" ):
68
- A2 (1 , 2 , 3 )
100
+ result = A2 ().method (1 , 2 , 3 )
101
+ assert result == (1 , 2 , 3 , "d" )
69
102
70
103
with pytest .warns (FutureWarning , match = r"Passing 'c, d' as positional" ):
71
- A2 (1 , 2 , 3 , 4 )
104
+ result = A2 ().method (1 , 2 , 3 , 4 )
105
+ assert result == (1 , 2 , 3 , 4 )
72
106
73
107
class A3 :
74
108
@_deprecate_positional_args ("v0.1" )
75
- def __init__ (self , a , * , b = 1 , ** kwargs ):
76
- pass
109
+ def method (self , a , * , b = "b" , ** kwargs ):
110
+ return a , b , kwargs
77
111
78
112
with pytest .warns (FutureWarning , match = r"Passing 'b' as positional" ):
79
- A3 (1 , 2 )
113
+ result = A3 ().method (1 , 2 )
114
+ assert result == (1 , 2 , {})
80
115
81
- with pytest .raises (TypeError , match = r"Cannot handle positional-only params" ):
116
+ with pytest .warns (FutureWarning , match = r"Passing 'b' as positional" ):
117
+ result = A3 ().method (1 , 2 , f = "f" )
118
+ assert result == (1 , 2 , {"f" : "f" })
82
119
83
- class A3 :
84
- @_deprecate_positional_args ("v0.1" )
85
- def __init__ (self , a , / , * , b = 1 , ** kwargs ):
86
- pass
120
+ class A4 :
121
+ @_deprecate_positional_args ("v0.1" )
122
+ def method (self , a , / , * , b = "b" , ** kwargs ):
123
+ return a , b , kwargs
124
+
125
+ result = A4 ().method (1 )
126
+ assert result == (1 , "b" , {})
127
+
128
+ result = A4 ().method (1 , b = 2 , f = "f" )
129
+ assert result == (1 , 2 , {"f" : "f" })
130
+
131
+ with pytest .warns (FutureWarning , match = r"Passing 'b' as positional" ):
132
+ result = A4 ().method (1 , 2 , f = "f" )
133
+ assert result == (1 , 2 , {"f" : "f" })
87
134
88
135
with pytest .raises (TypeError , match = r"Keyword-only param without default" ):
89
136
90
- class A4 :
137
+ class A5 :
91
138
@_deprecate_positional_args ("v0.1" )
92
139
def __init__ (self , a , * , b , c = 3 , ** kwargs ):
93
140
pass
0 commit comments