Benjamin Peterson | a567a77 | 2010-03-21 21:00:50 +0000 | [diff] [blame] | 1 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 2 | """Doctest for method/function calls. |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 3 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 4 | We're going the use these types for extra testing |
Raymond Hettinger | 40174c3 | 2003-05-31 07:04:16 +0000 | [diff] [blame] | 5 | |
Neal Norwitz | d79bacc | 2008-03-18 20:58:39 +0000 | [diff] [blame] | 6 | >>> from collections import UserList |
| 7 | >>> from collections import UserDict |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 8 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 9 | We're defining four helper functions |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 10 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 11 | >>> def e(a,b): |
Neal Norwitz | d79bacc | 2008-03-18 20:58:39 +0000 | [diff] [blame] | 12 | ... print(a, b) |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 13 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 14 | >>> def f(*a, **k): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 15 | ... print(a, support.sortdict(k)) |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 16 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 17 | >>> def g(x, *y, **z): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 18 | ... print(x, y, support.sortdict(z)) |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 19 | |
| 20 | >>> def h(j=1, a=2, h=3): |
Neal Norwitz | d79bacc | 2008-03-18 20:58:39 +0000 | [diff] [blame] | 21 | ... print(j, a, h) |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 22 | |
| 23 | Argument list examples |
| 24 | |
| 25 | >>> f() |
| 26 | () {} |
| 27 | >>> f(1) |
| 28 | (1,) {} |
| 29 | >>> f(1, 2) |
| 30 | (1, 2) {} |
| 31 | >>> f(1, 2, 3) |
| 32 | (1, 2, 3) {} |
| 33 | >>> f(1, 2, 3, *(4, 5)) |
| 34 | (1, 2, 3, 4, 5) {} |
| 35 | >>> f(1, 2, 3, *[4, 5]) |
| 36 | (1, 2, 3, 4, 5) {} |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 37 | >>> f(*[1, 2, 3], 4, 5) |
| 38 | (1, 2, 3, 4, 5) {} |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 39 | >>> f(1, 2, 3, *UserList([4, 5])) |
| 40 | (1, 2, 3, 4, 5) {} |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 41 | >>> f(1, 2, 3, *[4, 5], *[6, 7]) |
| 42 | (1, 2, 3, 4, 5, 6, 7) {} |
| 43 | >>> f(1, *[2, 3], 4, *[5, 6], 7) |
| 44 | (1, 2, 3, 4, 5, 6, 7) {} |
| 45 | >>> f(*UserList([1, 2]), *UserList([3, 4]), 5, *UserList([6, 7])) |
| 46 | (1, 2, 3, 4, 5, 6, 7) {} |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 47 | |
| 48 | Here we add keyword arguments |
| 49 | |
| 50 | >>> f(1, 2, 3, **{'a':4, 'b':5}) |
| 51 | (1, 2, 3) {'a': 4, 'b': 5} |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 52 | >>> f(1, 2, **{'a': -1, 'b': 5}, **{'a': 4, 'c': 6}) |
| 53 | Traceback (most recent call last): |
| 54 | ... |
Jeroen Demeyer | bf17d41 | 2019-11-05 16:48:04 +0100 | [diff] [blame] | 55 | TypeError: test.test_extcall.f() got multiple values for keyword argument 'a' |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 56 | >>> f(1, 2, **{'a': -1, 'b': 5}, a=4, c=6) |
| 57 | Traceback (most recent call last): |
| 58 | ... |
Jeroen Demeyer | bf17d41 | 2019-11-05 16:48:04 +0100 | [diff] [blame] | 59 | TypeError: test.test_extcall.f() got multiple values for keyword argument 'a' |
Serhiy Storchaka | 3c317e7 | 2016-06-12 09:22:01 +0300 | [diff] [blame] | 60 | >>> f(1, 2, a=3, **{'a': 4}, **{'a': 5}) |
| 61 | Traceback (most recent call last): |
| 62 | ... |
Jeroen Demeyer | bf17d41 | 2019-11-05 16:48:04 +0100 | [diff] [blame] | 63 | TypeError: test.test_extcall.f() got multiple values for keyword argument 'a' |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 64 | >>> f(1, 2, 3, *[4, 5], **{'a':6, 'b':7}) |
| 65 | (1, 2, 3, 4, 5) {'a': 6, 'b': 7} |
| 66 | >>> f(1, 2, 3, x=4, y=5, *(6, 7), **{'a':8, 'b': 9}) |
| 67 | (1, 2, 3, 6, 7) {'a': 8, 'b': 9, 'x': 4, 'y': 5} |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 68 | >>> f(1, 2, 3, *[4, 5], **{'c': 8}, **{'a':6, 'b':7}) |
| 69 | (1, 2, 3, 4, 5) {'a': 6, 'b': 7, 'c': 8} |
| 70 | >>> f(1, 2, 3, *(4, 5), x=6, y=7, **{'a':8, 'b': 9}) |
| 71 | (1, 2, 3, 4, 5) {'a': 8, 'b': 9, 'x': 6, 'y': 7} |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 72 | |
| 73 | >>> f(1, 2, 3, **UserDict(a=4, b=5)) |
| 74 | (1, 2, 3) {'a': 4, 'b': 5} |
| 75 | >>> f(1, 2, 3, *(4, 5), **UserDict(a=6, b=7)) |
| 76 | (1, 2, 3, 4, 5) {'a': 6, 'b': 7} |
| 77 | >>> f(1, 2, 3, x=4, y=5, *(6, 7), **UserDict(a=8, b=9)) |
| 78 | (1, 2, 3, 6, 7) {'a': 8, 'b': 9, 'x': 4, 'y': 5} |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 79 | >>> f(1, 2, 3, *(4, 5), x=6, y=7, **UserDict(a=8, b=9)) |
| 80 | (1, 2, 3, 4, 5) {'a': 8, 'b': 9, 'x': 6, 'y': 7} |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 81 | |
Mark Shannon | db64f12 | 2020-06-01 10:42:42 +0100 | [diff] [blame] | 82 | Mix keyword arguments and dict unpacking |
| 83 | |
| 84 | >>> d1 = {'a':1} |
| 85 | |
| 86 | >>> d2 = {'c':3} |
| 87 | |
| 88 | >>> f(b=2, **d1, **d2) |
| 89 | () {'a': 1, 'b': 2, 'c': 3} |
| 90 | |
| 91 | >>> f(**d1, b=2, **d2) |
| 92 | () {'a': 1, 'b': 2, 'c': 3} |
| 93 | |
| 94 | >>> f(**d1, **d2, b=2) |
| 95 | () {'a': 1, 'b': 2, 'c': 3} |
| 96 | |
| 97 | >>> f(**d1, b=2, **d2, d=4) |
| 98 | () {'a': 1, 'b': 2, 'c': 3, 'd': 4} |
| 99 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 100 | Examples with invalid arguments (TypeErrors). We're also testing the function |
| 101 | names in the exception messages. |
| 102 | |
| 103 | Verify clearing of SF bug #733667 |
| 104 | |
| 105 | >>> e(c=4) |
| 106 | Traceback (most recent call last): |
| 107 | ... |
| 108 | TypeError: e() got an unexpected keyword argument 'c' |
| 109 | |
| 110 | >>> g() |
| 111 | Traceback (most recent call last): |
| 112 | ... |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 113 | TypeError: g() missing 1 required positional argument: 'x' |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 114 | |
| 115 | >>> g(*()) |
| 116 | Traceback (most recent call last): |
| 117 | ... |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 118 | TypeError: g() missing 1 required positional argument: 'x' |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 119 | |
| 120 | >>> g(*(), **{}) |
| 121 | Traceback (most recent call last): |
| 122 | ... |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 123 | TypeError: g() missing 1 required positional argument: 'x' |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 124 | |
| 125 | >>> g(1) |
| 126 | 1 () {} |
| 127 | >>> g(1, 2) |
| 128 | 1 (2,) {} |
| 129 | >>> g(1, 2, 3) |
| 130 | 1 (2, 3) {} |
| 131 | >>> g(1, 2, 3, *(4, 5)) |
| 132 | 1 (2, 3, 4, 5) {} |
| 133 | |
| 134 | >>> class Nothing: pass |
| 135 | ... |
| 136 | >>> g(*Nothing()) |
| 137 | Traceback (most recent call last): |
| 138 | ... |
Jeroen Demeyer | bf17d41 | 2019-11-05 16:48:04 +0100 | [diff] [blame] | 139 | TypeError: test.test_extcall.g() argument after * must be an iterable, not Nothing |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 140 | |
| 141 | >>> class Nothing: |
| 142 | ... def __len__(self): return 5 |
| 143 | ... |
| 144 | |
| 145 | >>> g(*Nothing()) |
| 146 | Traceback (most recent call last): |
| 147 | ... |
Jeroen Demeyer | bf17d41 | 2019-11-05 16:48:04 +0100 | [diff] [blame] | 148 | TypeError: test.test_extcall.g() argument after * must be an iterable, not Nothing |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 149 | |
| 150 | >>> class Nothing(): |
| 151 | ... def __len__(self): return 5 |
| 152 | ... def __getitem__(self, i): |
| 153 | ... if i<3: return i |
| 154 | ... else: raise IndexError(i) |
| 155 | ... |
| 156 | |
| 157 | >>> g(*Nothing()) |
| 158 | 0 (1, 2) {} |
| 159 | |
| 160 | >>> class Nothing: |
| 161 | ... def __init__(self): self.c = 0 |
| 162 | ... def __iter__(self): return self |
Neal Norwitz | d79bacc | 2008-03-18 20:58:39 +0000 | [diff] [blame] | 163 | ... def __next__(self): |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 164 | ... if self.c == 4: |
| 165 | ... raise StopIteration |
| 166 | ... c = self.c |
| 167 | ... self.c += 1 |
| 168 | ... return c |
| 169 | ... |
| 170 | |
| 171 | >>> g(*Nothing()) |
| 172 | 0 (1, 2, 3) {} |
| 173 | |
Martin Panter | b594422 | 2016-01-31 06:30:56 +0000 | [diff] [blame] | 174 | Check for issue #4806: Does a TypeError in a generator get propagated with the |
| 175 | right error message? (Also check with other iterables.) |
| 176 | |
| 177 | >>> def broken(): raise TypeError("myerror") |
| 178 | ... |
| 179 | |
| 180 | >>> g(*(broken() for i in range(1))) |
| 181 | Traceback (most recent call last): |
| 182 | ... |
| 183 | TypeError: myerror |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 184 | >>> g(*range(1), *(broken() for i in range(1))) |
| 185 | Traceback (most recent call last): |
| 186 | ... |
| 187 | TypeError: myerror |
Martin Panter | b594422 | 2016-01-31 06:30:56 +0000 | [diff] [blame] | 188 | |
| 189 | >>> class BrokenIterable1: |
| 190 | ... def __iter__(self): |
| 191 | ... raise TypeError('myerror') |
| 192 | ... |
| 193 | >>> g(*BrokenIterable1()) |
| 194 | Traceback (most recent call last): |
| 195 | ... |
| 196 | TypeError: myerror |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 197 | >>> g(*range(1), *BrokenIterable1()) |
| 198 | Traceback (most recent call last): |
| 199 | ... |
| 200 | TypeError: myerror |
Martin Panter | b594422 | 2016-01-31 06:30:56 +0000 | [diff] [blame] | 201 | |
| 202 | >>> class BrokenIterable2: |
| 203 | ... def __iter__(self): |
| 204 | ... yield 0 |
| 205 | ... raise TypeError('myerror') |
| 206 | ... |
| 207 | >>> g(*BrokenIterable2()) |
| 208 | Traceback (most recent call last): |
| 209 | ... |
| 210 | TypeError: myerror |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 211 | >>> g(*range(1), *BrokenIterable2()) |
| 212 | Traceback (most recent call last): |
| 213 | ... |
| 214 | TypeError: myerror |
Martin Panter | b594422 | 2016-01-31 06:30:56 +0000 | [diff] [blame] | 215 | |
| 216 | >>> class BrokenSequence: |
| 217 | ... def __getitem__(self, idx): |
| 218 | ... raise TypeError('myerror') |
| 219 | ... |
| 220 | >>> g(*BrokenSequence()) |
| 221 | Traceback (most recent call last): |
| 222 | ... |
| 223 | TypeError: myerror |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 224 | >>> g(*range(1), *BrokenSequence()) |
| 225 | Traceback (most recent call last): |
| 226 | ... |
| 227 | TypeError: myerror |
Martin Panter | b594422 | 2016-01-31 06:30:56 +0000 | [diff] [blame] | 228 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 229 | Make sure that the function doesn't stomp the dictionary |
| 230 | |
| 231 | >>> d = {'a': 1, 'b': 2, 'c': 3} |
| 232 | >>> d2 = d.copy() |
| 233 | >>> g(1, d=4, **d) |
| 234 | 1 () {'a': 1, 'b': 2, 'c': 3, 'd': 4} |
| 235 | >>> d == d2 |
| 236 | True |
| 237 | |
| 238 | What about willful misconduct? |
| 239 | |
| 240 | >>> def saboteur(**kw): |
| 241 | ... kw['x'] = 'm' |
| 242 | ... return kw |
| 243 | |
| 244 | >>> d = {} |
| 245 | >>> kw = saboteur(a=1, **d) |
| 246 | >>> d |
| 247 | {} |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 248 | |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 249 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 250 | >>> g(1, 2, 3, **{'x': 4, 'y': 5}) |
| 251 | Traceback (most recent call last): |
| 252 | ... |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 253 | TypeError: g() got multiple values for argument 'x' |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 254 | |
| 255 | >>> f(**{1:2}) |
| 256 | Traceback (most recent call last): |
| 257 | ... |
Jeroen Demeyer | 0567786 | 2019-08-16 12:41:27 +0200 | [diff] [blame] | 258 | TypeError: keywords must be strings |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 259 | |
| 260 | >>> h(**{'e': 2}) |
| 261 | Traceback (most recent call last): |
| 262 | ... |
| 263 | TypeError: h() got an unexpected keyword argument 'e' |
| 264 | |
| 265 | >>> h(*h) |
| 266 | Traceback (most recent call last): |
| 267 | ... |
Jeroen Demeyer | bf17d41 | 2019-11-05 16:48:04 +0100 | [diff] [blame] | 268 | TypeError: test.test_extcall.h() argument after * must be an iterable, not function |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 269 | |
Serhiy Storchaka | 7344285 | 2016-10-02 10:33:46 +0300 | [diff] [blame] | 270 | >>> h(1, *h) |
| 271 | Traceback (most recent call last): |
| 272 | ... |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 273 | TypeError: Value after * must be an iterable, not function |
Serhiy Storchaka | 7344285 | 2016-10-02 10:33:46 +0300 | [diff] [blame] | 274 | |
| 275 | >>> h(*[1], *h) |
| 276 | Traceback (most recent call last): |
| 277 | ... |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 278 | TypeError: Value after * must be an iterable, not function |
Serhiy Storchaka | 7344285 | 2016-10-02 10:33:46 +0300 | [diff] [blame] | 279 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 280 | >>> dir(*h) |
| 281 | Traceback (most recent call last): |
| 282 | ... |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 283 | TypeError: dir() argument after * must be an iterable, not function |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 284 | |
Serhiy Storchaka | 8e79e6e | 2019-02-19 13:49:09 +0200 | [diff] [blame] | 285 | >>> nothing = None |
| 286 | >>> nothing(*h) |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 287 | Traceback (most recent call last): |
| 288 | ... |
Jeroen Demeyer | bf17d41 | 2019-11-05 16:48:04 +0100 | [diff] [blame] | 289 | TypeError: None argument after * must be an iterable, \ |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 290 | not function |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 291 | |
| 292 | >>> h(**h) |
| 293 | Traceback (most recent call last): |
| 294 | ... |
Jeroen Demeyer | bf17d41 | 2019-11-05 16:48:04 +0100 | [diff] [blame] | 295 | TypeError: test.test_extcall.h() argument after ** must be a mapping, not function |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 296 | |
Serhiy Storchaka | e036ef8 | 2016-10-02 11:06:43 +0300 | [diff] [blame] | 297 | >>> h(**[]) |
| 298 | Traceback (most recent call last): |
| 299 | ... |
Jeroen Demeyer | bf17d41 | 2019-11-05 16:48:04 +0100 | [diff] [blame] | 300 | TypeError: test.test_extcall.h() argument after ** must be a mapping, not list |
Serhiy Storchaka | e036ef8 | 2016-10-02 11:06:43 +0300 | [diff] [blame] | 301 | |
| 302 | >>> h(a=1, **h) |
| 303 | Traceback (most recent call last): |
| 304 | ... |
Jeroen Demeyer | bf17d41 | 2019-11-05 16:48:04 +0100 | [diff] [blame] | 305 | TypeError: test.test_extcall.h() argument after ** must be a mapping, not function |
Serhiy Storchaka | e036ef8 | 2016-10-02 11:06:43 +0300 | [diff] [blame] | 306 | |
| 307 | >>> h(a=1, **[]) |
| 308 | Traceback (most recent call last): |
| 309 | ... |
Jeroen Demeyer | bf17d41 | 2019-11-05 16:48:04 +0100 | [diff] [blame] | 310 | TypeError: test.test_extcall.h() argument after ** must be a mapping, not list |
Serhiy Storchaka | e036ef8 | 2016-10-02 11:06:43 +0300 | [diff] [blame] | 311 | |
| 312 | >>> h(**{'a': 1}, **h) |
| 313 | Traceback (most recent call last): |
| 314 | ... |
Jeroen Demeyer | bf17d41 | 2019-11-05 16:48:04 +0100 | [diff] [blame] | 315 | TypeError: test.test_extcall.h() argument after ** must be a mapping, not function |
Serhiy Storchaka | e036ef8 | 2016-10-02 11:06:43 +0300 | [diff] [blame] | 316 | |
| 317 | >>> h(**{'a': 1}, **[]) |
| 318 | Traceback (most recent call last): |
| 319 | ... |
Jeroen Demeyer | bf17d41 | 2019-11-05 16:48:04 +0100 | [diff] [blame] | 320 | TypeError: test.test_extcall.h() argument after ** must be a mapping, not list |
Serhiy Storchaka | e036ef8 | 2016-10-02 11:06:43 +0300 | [diff] [blame] | 321 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 322 | >>> dir(**h) |
| 323 | Traceback (most recent call last): |
| 324 | ... |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 325 | TypeError: dir() argument after ** must be a mapping, not function |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 326 | |
Serhiy Storchaka | 8e79e6e | 2019-02-19 13:49:09 +0200 | [diff] [blame] | 327 | >>> nothing(**h) |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 328 | Traceback (most recent call last): |
| 329 | ... |
Jeroen Demeyer | bf17d41 | 2019-11-05 16:48:04 +0100 | [diff] [blame] | 330 | TypeError: None argument after ** must be a mapping, \ |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 331 | not function |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 332 | |
| 333 | >>> dir(b=1, **{'b': 1}) |
| 334 | Traceback (most recent call last): |
| 335 | ... |
| 336 | TypeError: dir() got multiple values for keyword argument 'b' |
| 337 | |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 338 | Test a kwargs mapping with duplicated keys. |
| 339 | |
| 340 | >>> from collections.abc import Mapping |
| 341 | >>> class MultiDict(Mapping): |
| 342 | ... def __init__(self, items): |
| 343 | ... self._items = items |
| 344 | ... |
| 345 | ... def __iter__(self): |
| 346 | ... return (k for k, v in self._items) |
| 347 | ... |
| 348 | ... def __getitem__(self, key): |
| 349 | ... for k, v in self._items: |
| 350 | ... if k == key: |
| 351 | ... return v |
| 352 | ... raise KeyError(key) |
| 353 | ... |
| 354 | ... def __len__(self): |
| 355 | ... return len(self._items) |
| 356 | ... |
| 357 | ... def keys(self): |
| 358 | ... return [k for k, v in self._items] |
| 359 | ... |
| 360 | ... def values(self): |
| 361 | ... return [v for k, v in self._items] |
| 362 | ... |
| 363 | ... def items(self): |
| 364 | ... return [(k, v) for k, v in self._items] |
| 365 | ... |
| 366 | >>> g(**MultiDict([('x', 1), ('y', 2)])) |
| 367 | 1 () {'y': 2} |
| 368 | |
| 369 | >>> g(**MultiDict([('x', 1), ('x', 2)])) |
| 370 | Traceback (most recent call last): |
| 371 | ... |
Jeroen Demeyer | bf17d41 | 2019-11-05 16:48:04 +0100 | [diff] [blame] | 372 | TypeError: test.test_extcall.g() got multiple values for keyword argument 'x' |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 373 | |
| 374 | >>> g(a=3, **MultiDict([('x', 1), ('x', 2)])) |
| 375 | Traceback (most recent call last): |
| 376 | ... |
Jeroen Demeyer | bf17d41 | 2019-11-05 16:48:04 +0100 | [diff] [blame] | 377 | TypeError: test.test_extcall.g() got multiple values for keyword argument 'x' |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 378 | |
| 379 | >>> g(**MultiDict([('a', 3)]), **MultiDict([('x', 1), ('x', 2)])) |
| 380 | Traceback (most recent call last): |
| 381 | ... |
Jeroen Demeyer | bf17d41 | 2019-11-05 16:48:04 +0100 | [diff] [blame] | 382 | TypeError: test.test_extcall.g() got multiple values for keyword argument 'x' |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 383 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 384 | Another helper function |
| 385 | |
| 386 | >>> def f2(*a, **b): |
| 387 | ... return a, b |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 388 | |
| 389 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 390 | >>> d = {} |
Neal Norwitz | d79bacc | 2008-03-18 20:58:39 +0000 | [diff] [blame] | 391 | >>> for i in range(512): |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 392 | ... key = 'k%d' % i |
| 393 | ... d[key] = i |
| 394 | >>> a, b = f2(1, *(2,3), **d) |
| 395 | >>> len(a), len(b), b == d |
| 396 | (3, 512, True) |
Raymond Hettinger | 40174c3 | 2003-05-31 07:04:16 +0000 | [diff] [blame] | 397 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 398 | >>> class Foo: |
| 399 | ... def method(self, arg1, arg2): |
| 400 | ... return arg1+arg2 |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 401 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 402 | >>> x = Foo() |
| 403 | >>> Foo.method(*(x, 1, 2)) |
| 404 | 3 |
| 405 | >>> Foo.method(x, *(1, 2)) |
| 406 | 3 |
| 407 | >>> Foo.method(*(1, 2, 3)) |
Neal Norwitz | d79bacc | 2008-03-18 20:58:39 +0000 | [diff] [blame] | 408 | 5 |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 409 | >>> Foo.method(1, *[2, 3]) |
Neal Norwitz | d79bacc | 2008-03-18 20:58:39 +0000 | [diff] [blame] | 410 | 5 |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 411 | |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 412 | A PyCFunction that takes only positional parameters should allow an |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 413 | empty keyword dictionary to pass without a complaint, but raise a |
| 414 | TypeError if te dictionary is not empty |
Jeremy Hylton | 074c3e6 | 2000-03-30 23:55:31 +0000 | [diff] [blame] | 415 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 416 | >>> try: |
| 417 | ... silence = id(1, *{}) |
| 418 | ... True |
| 419 | ... except: |
| 420 | ... False |
| 421 | True |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 422 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 423 | >>> id(1, **{'foo': 1}) |
| 424 | Traceback (most recent call last): |
| 425 | ... |
| 426 | TypeError: id() takes no keyword arguments |
Jeremy Hylton | 074c3e6 | 2000-03-30 23:55:31 +0000 | [diff] [blame] | 427 | |
Benjamin Peterson | 0289b15 | 2009-06-28 17:22:03 +0000 | [diff] [blame] | 428 | A corner case of keyword dictionary items being deleted during |
| 429 | the function call setup. See <http://bugs.python.org/issue2016>. |
| 430 | |
| 431 | >>> class Name(str): |
| 432 | ... def __eq__(self, other): |
| 433 | ... try: |
| 434 | ... del x[self] |
| 435 | ... except KeyError: |
| 436 | ... pass |
| 437 | ... return str.__eq__(self, other) |
| 438 | ... def __hash__(self): |
| 439 | ... return str.__hash__(self) |
| 440 | |
| 441 | >>> x = {Name("a"):1, Name("b"):2} |
| 442 | >>> def f(a, b): |
| 443 | ... print(a,b) |
| 444 | >>> f(**x) |
| 445 | 1 2 |
Benjamin Peterson | a567a77 | 2010-03-21 21:00:50 +0000 | [diff] [blame] | 446 | |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 447 | Too many arguments: |
Benjamin Peterson | a567a77 | 2010-03-21 21:00:50 +0000 | [diff] [blame] | 448 | |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 449 | >>> def f(): pass |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 450 | >>> f(1) |
| 451 | Traceback (most recent call last): |
| 452 | ... |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 453 | TypeError: f() takes 0 positional arguments but 1 was given |
| 454 | >>> def f(a): pass |
| 455 | >>> f(1, 2) |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 456 | Traceback (most recent call last): |
| 457 | ... |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 458 | TypeError: f() takes 1 positional argument but 2 were given |
| 459 | >>> def f(a, b=1): pass |
| 460 | >>> f(1, 2, 3) |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 461 | Traceback (most recent call last): |
| 462 | ... |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 463 | TypeError: f() takes from 1 to 2 positional arguments but 3 were given |
| 464 | >>> def f(*, kw): pass |
| 465 | >>> f(1, kw=3) |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 466 | Traceback (most recent call last): |
| 467 | ... |
| 468 | TypeError: f() takes 0 positional arguments but 1 positional argument (and 1 keyword-only argument) were given |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 469 | >>> def f(*, kw, b): pass |
| 470 | >>> f(1, 2, 3, b=3, kw=3) |
| 471 | Traceback (most recent call last): |
| 472 | ... |
| 473 | TypeError: f() takes 0 positional arguments but 3 positional arguments (and 2 keyword-only arguments) were given |
| 474 | >>> def f(a, b=2, *, kw): pass |
| 475 | >>> f(2, 3, 4, kw=4) |
| 476 | Traceback (most recent call last): |
| 477 | ... |
| 478 | TypeError: f() takes from 1 to 2 positional arguments but 3 positional arguments (and 1 keyword-only argument) were given |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 479 | |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 480 | Too few and missing arguments: |
| 481 | |
| 482 | >>> def f(a): pass |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 483 | >>> f() |
| 484 | Traceback (most recent call last): |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 485 | ... |
| 486 | TypeError: f() missing 1 required positional argument: 'a' |
| 487 | >>> def f(a, b): pass |
| 488 | >>> f() |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 489 | Traceback (most recent call last): |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 490 | ... |
| 491 | TypeError: f() missing 2 required positional arguments: 'a' and 'b' |
| 492 | >>> def f(a, b, c): pass |
| 493 | >>> f() |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 494 | Traceback (most recent call last): |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 495 | ... |
| 496 | TypeError: f() missing 3 required positional arguments: 'a', 'b', and 'c' |
| 497 | >>> def f(a, b, c, d, e): pass |
| 498 | >>> f() |
| 499 | Traceback (most recent call last): |
| 500 | ... |
| 501 | TypeError: f() missing 5 required positional arguments: 'a', 'b', 'c', 'd', and 'e' |
| 502 | >>> def f(a, b=4, c=5, d=5): pass |
| 503 | >>> f(c=12, b=9) |
| 504 | Traceback (most recent call last): |
| 505 | ... |
| 506 | TypeError: f() missing 1 required positional argument: 'a' |
| 507 | |
| 508 | Same with keyword only args: |
| 509 | |
| 510 | >>> def f(*, w): pass |
| 511 | >>> f() |
| 512 | Traceback (most recent call last): |
| 513 | ... |
| 514 | TypeError: f() missing 1 required keyword-only argument: 'w' |
| 515 | >>> def f(*, a, b, c, d, e): pass |
| 516 | >>> f() |
| 517 | Traceback (most recent call last): |
| 518 | ... |
| 519 | TypeError: f() missing 5 required keyword-only arguments: 'a', 'b', 'c', 'd', and 'e' |
| 520 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 521 | """ |
Samuele Pedroni | 8036c83 | 2004-02-21 21:03:30 +0000 | [diff] [blame] | 522 | |
Benjamin Peterson | a567a77 | 2010-03-21 21:00:50 +0000 | [diff] [blame] | 523 | import sys |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 524 | from test import support |
Samuele Pedroni | 8036c83 | 2004-02-21 21:03:30 +0000 | [diff] [blame] | 525 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 526 | def test_main(): |
Benjamin Peterson | a567a77 | 2010-03-21 21:00:50 +0000 | [diff] [blame] | 527 | support.run_doctest(sys.modules[__name__], True) |
Jeremy Hylton | 074c3e6 | 2000-03-30 23:55:31 +0000 | [diff] [blame] | 528 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 529 | if __name__ == '__main__': |
| 530 | test_main() |