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