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