blob: 4205ca82222f274c5238217fa30aab2204c28e88 [file] [log] [blame]
Benjamin Petersona567a772010-03-21 21:00:50 +00001
Christian Heimesb186d002008-03-18 15:15:01 +00002"""Doctest for method/function calls.
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +00003
Christian Heimesb186d002008-03-18 15:15:01 +00004We're going the use these types for extra testing
Raymond Hettinger40174c32003-05-31 07:04:16 +00005
Neal Norwitzd79bacc2008-03-18 20:58:39 +00006 >>> from collections import UserList
7 >>> from collections import UserDict
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +00008
Christian Heimesb186d002008-03-18 15:15:01 +00009We're defining four helper functions
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +000010
Christian Heimesb186d002008-03-18 15:15:01 +000011 >>> def e(a,b):
Neal Norwitzd79bacc2008-03-18 20:58:39 +000012 ... print(a, b)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +000013
Christian Heimesb186d002008-03-18 15:15:01 +000014 >>> def f(*a, **k):
Benjamin Petersonee8712c2008-05-20 21:35:26 +000015 ... print(a, support.sortdict(k))
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +000016
Christian Heimesb186d002008-03-18 15:15:01 +000017 >>> def g(x, *y, **z):
Benjamin Petersonee8712c2008-05-20 21:35:26 +000018 ... print(x, y, support.sortdict(z))
Christian Heimesb186d002008-03-18 15:15:01 +000019
20 >>> def h(j=1, a=2, h=3):
Neal Norwitzd79bacc2008-03-18 20:58:39 +000021 ... print(j, a, h)
Christian Heimesb186d002008-03-18 15:15:01 +000022
23Argument 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 Peterson025e9eb2015-05-05 20:16:41 -040037 >>> f(*[1, 2, 3], 4, 5)
38 (1, 2, 3, 4, 5) {}
Christian Heimesb186d002008-03-18 15:15:01 +000039 >>> f(1, 2, 3, *UserList([4, 5]))
40 (1, 2, 3, 4, 5) {}
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040041 >>> 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 Heimesb186d002008-03-18 15:15:01 +000047
48Here we add keyword arguments
49
50 >>> f(1, 2, 3, **{'a':4, 'b':5})
51 (1, 2, 3) {'a': 4, 'b': 5}
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040052 >>> f(1, 2, **{'a': -1, 'b': 5}, **{'a': 4, 'c': 6})
53 Traceback (most recent call last):
54 ...
Jeroen Demeyerbf17d412019-11-05 16:48:04 +010055 TypeError: test.test_extcall.f() got multiple values for keyword argument 'a'
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040056 >>> f(1, 2, **{'a': -1, 'b': 5}, a=4, c=6)
57 Traceback (most recent call last):
58 ...
Jeroen Demeyerbf17d412019-11-05 16:48:04 +010059 TypeError: test.test_extcall.f() got multiple values for keyword argument 'a'
Serhiy Storchaka3c317e72016-06-12 09:22:01 +030060 >>> f(1, 2, a=3, **{'a': 4}, **{'a': 5})
61 Traceback (most recent call last):
62 ...
Jeroen Demeyerbf17d412019-11-05 16:48:04 +010063 TypeError: test.test_extcall.f() got multiple values for keyword argument 'a'
Christian Heimesb186d002008-03-18 15:15:01 +000064 >>> 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 Peterson025e9eb2015-05-05 20:16:41 -040068 >>> 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 Heimesb186d002008-03-18 15:15:01 +000072
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 Peterson025e9eb2015-05-05 20:16:41 -040079 >>> 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 Heimesb186d002008-03-18 15:15:01 +000081
Mark Shannondb64f122020-06-01 10:42:42 +010082Mix 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 Heimesb186d002008-03-18 15:15:01 +0000100Examples with invalid arguments (TypeErrors). We're also testing the function
101names in the exception messages.
102
103Verify 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 Petersone109c702011-06-24 09:37:26 -0500113 TypeError: g() missing 1 required positional argument: 'x'
Christian Heimesb186d002008-03-18 15:15:01 +0000114
115 >>> g(*())
116 Traceback (most recent call last):
117 ...
Benjamin Petersone109c702011-06-24 09:37:26 -0500118 TypeError: g() missing 1 required positional argument: 'x'
Christian Heimesb186d002008-03-18 15:15:01 +0000119
120 >>> g(*(), **{})
121 Traceback (most recent call last):
122 ...
Benjamin Petersone109c702011-06-24 09:37:26 -0500123 TypeError: g() missing 1 required positional argument: 'x'
Christian Heimesb186d002008-03-18 15:15:01 +0000124
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 Demeyerbf17d412019-11-05 16:48:04 +0100139 TypeError: test.test_extcall.g() argument after * must be an iterable, not Nothing
Christian Heimesb186d002008-03-18 15:15:01 +0000140
141 >>> class Nothing:
142 ... def __len__(self): return 5
143 ...
144
145 >>> g(*Nothing())
146 Traceback (most recent call last):
147 ...
Jeroen Demeyerbf17d412019-11-05 16:48:04 +0100148 TypeError: test.test_extcall.g() argument after * must be an iterable, not Nothing
Christian Heimesb186d002008-03-18 15:15:01 +0000149
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 Norwitzd79bacc2008-03-18 20:58:39 +0000163 ... def __next__(self):
Christian Heimesb186d002008-03-18 15:15:01 +0000164 ... 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 Panterb5944222016-01-31 06:30:56 +0000174Check for issue #4806: Does a TypeError in a generator get propagated with the
175right 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 Storchaka25e4f772017-08-03 11:37:15 +0300184 >>> g(*range(1), *(broken() for i in range(1)))
185 Traceback (most recent call last):
186 ...
187 TypeError: myerror
Martin Panterb5944222016-01-31 06:30:56 +0000188
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 Storchaka25e4f772017-08-03 11:37:15 +0300197 >>> g(*range(1), *BrokenIterable1())
198 Traceback (most recent call last):
199 ...
200 TypeError: myerror
Martin Panterb5944222016-01-31 06:30:56 +0000201
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 Storchaka25e4f772017-08-03 11:37:15 +0300211 >>> g(*range(1), *BrokenIterable2())
212 Traceback (most recent call last):
213 ...
214 TypeError: myerror
Martin Panterb5944222016-01-31 06:30:56 +0000215
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 Storchaka25e4f772017-08-03 11:37:15 +0300224 >>> g(*range(1), *BrokenSequence())
225 Traceback (most recent call last):
226 ...
227 TypeError: myerror
Martin Panterb5944222016-01-31 06:30:56 +0000228
Christian Heimesb186d002008-03-18 15:15:01 +0000229Make 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
238What 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 Hyltonaed0d8d2000-03-28 23:51:17 +0000248
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000249
Christian Heimesb186d002008-03-18 15:15:01 +0000250 >>> g(1, 2, 3, **{'x': 4, 'y': 5})
251 Traceback (most recent call last):
252 ...
Benjamin Petersonb204a422011-06-05 22:04:07 -0500253 TypeError: g() got multiple values for argument 'x'
Christian Heimesb186d002008-03-18 15:15:01 +0000254
255 >>> f(**{1:2})
256 Traceback (most recent call last):
257 ...
Jeroen Demeyer05677862019-08-16 12:41:27 +0200258 TypeError: keywords must be strings
Christian Heimesb186d002008-03-18 15:15:01 +0000259
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 Demeyerbf17d412019-11-05 16:48:04 +0100268 TypeError: test.test_extcall.h() argument after * must be an iterable, not function
Christian Heimesb186d002008-03-18 15:15:01 +0000269
Serhiy Storchaka73442852016-10-02 10:33:46 +0300270 >>> h(1, *h)
271 Traceback (most recent call last):
272 ...
Mark Shannon13bc1392020-01-23 09:25:17 +0000273 TypeError: Value after * must be an iterable, not function
Serhiy Storchaka73442852016-10-02 10:33:46 +0300274
275 >>> h(*[1], *h)
276 Traceback (most recent call last):
277 ...
Mark Shannon13bc1392020-01-23 09:25:17 +0000278 TypeError: Value after * must be an iterable, not function
Serhiy Storchaka73442852016-10-02 10:33:46 +0300279
Christian Heimesb186d002008-03-18 15:15:01 +0000280 >>> dir(*h)
281 Traceback (most recent call last):
282 ...
Serhiy Storchakab7281052016-09-12 00:52:40 +0300283 TypeError: dir() argument after * must be an iterable, not function
Christian Heimesb186d002008-03-18 15:15:01 +0000284
Serhiy Storchaka8e79e6e2019-02-19 13:49:09 +0200285 >>> nothing = None
286 >>> nothing(*h)
Christian Heimesb186d002008-03-18 15:15:01 +0000287 Traceback (most recent call last):
288 ...
Jeroen Demeyerbf17d412019-11-05 16:48:04 +0100289 TypeError: None argument after * must be an iterable, \
Serhiy Storchakab7281052016-09-12 00:52:40 +0300290not function
Christian Heimesb186d002008-03-18 15:15:01 +0000291
292 >>> h(**h)
293 Traceback (most recent call last):
294 ...
Jeroen Demeyerbf17d412019-11-05 16:48:04 +0100295 TypeError: test.test_extcall.h() argument after ** must be a mapping, not function
Christian Heimesb186d002008-03-18 15:15:01 +0000296
Serhiy Storchakae036ef82016-10-02 11:06:43 +0300297 >>> h(**[])
298 Traceback (most recent call last):
299 ...
Jeroen Demeyerbf17d412019-11-05 16:48:04 +0100300 TypeError: test.test_extcall.h() argument after ** must be a mapping, not list
Serhiy Storchakae036ef82016-10-02 11:06:43 +0300301
302 >>> h(a=1, **h)
303 Traceback (most recent call last):
304 ...
Jeroen Demeyerbf17d412019-11-05 16:48:04 +0100305 TypeError: test.test_extcall.h() argument after ** must be a mapping, not function
Serhiy Storchakae036ef82016-10-02 11:06:43 +0300306
307 >>> h(a=1, **[])
308 Traceback (most recent call last):
309 ...
Jeroen Demeyerbf17d412019-11-05 16:48:04 +0100310 TypeError: test.test_extcall.h() argument after ** must be a mapping, not list
Serhiy Storchakae036ef82016-10-02 11:06:43 +0300311
312 >>> h(**{'a': 1}, **h)
313 Traceback (most recent call last):
314 ...
Jeroen Demeyerbf17d412019-11-05 16:48:04 +0100315 TypeError: test.test_extcall.h() argument after ** must be a mapping, not function
Serhiy Storchakae036ef82016-10-02 11:06:43 +0300316
317 >>> h(**{'a': 1}, **[])
318 Traceback (most recent call last):
319 ...
Jeroen Demeyerbf17d412019-11-05 16:48:04 +0100320 TypeError: test.test_extcall.h() argument after ** must be a mapping, not list
Serhiy Storchakae036ef82016-10-02 11:06:43 +0300321
Christian Heimesb186d002008-03-18 15:15:01 +0000322 >>> dir(**h)
323 Traceback (most recent call last):
324 ...
Serhiy Storchakab7281052016-09-12 00:52:40 +0300325 TypeError: dir() argument after ** must be a mapping, not function
Christian Heimesb186d002008-03-18 15:15:01 +0000326
Serhiy Storchaka8e79e6e2019-02-19 13:49:09 +0200327 >>> nothing(**h)
Christian Heimesb186d002008-03-18 15:15:01 +0000328 Traceback (most recent call last):
329 ...
Jeroen Demeyerbf17d412019-11-05 16:48:04 +0100330 TypeError: None argument after ** must be a mapping, \
Serhiy Storchakab7281052016-09-12 00:52:40 +0300331not function
Christian Heimesb186d002008-03-18 15:15:01 +0000332
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 Storchakaf1ec3ce2019-01-12 10:12:24 +0200338Test 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 Demeyerbf17d412019-11-05 16:48:04 +0100372 TypeError: test.test_extcall.g() got multiple values for keyword argument 'x'
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +0200373
374 >>> g(a=3, **MultiDict([('x', 1), ('x', 2)]))
375 Traceback (most recent call last):
376 ...
Jeroen Demeyerbf17d412019-11-05 16:48:04 +0100377 TypeError: test.test_extcall.g() got multiple values for keyword argument 'x'
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +0200378
379 >>> g(**MultiDict([('a', 3)]), **MultiDict([('x', 1), ('x', 2)]))
380 Traceback (most recent call last):
381 ...
Jeroen Demeyerbf17d412019-11-05 16:48:04 +0100382 TypeError: test.test_extcall.g() got multiple values for keyword argument 'x'
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +0200383
Christian Heimesb186d002008-03-18 15:15:01 +0000384Another helper function
385
386 >>> def f2(*a, **b):
387 ... return a, b
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000388
389
Christian Heimesb186d002008-03-18 15:15:01 +0000390 >>> d = {}
Neal Norwitzd79bacc2008-03-18 20:58:39 +0000391 >>> for i in range(512):
Christian Heimesb186d002008-03-18 15:15:01 +0000392 ... 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 Hettinger40174c32003-05-31 07:04:16 +0000397
Christian Heimesb186d002008-03-18 15:15:01 +0000398 >>> class Foo:
399 ... def method(self, arg1, arg2):
400 ... return arg1+arg2
Fred Drake004d5e62000-10-23 17:22:08 +0000401
Christian Heimesb186d002008-03-18 15:15:01 +0000402 >>> 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 Norwitzd79bacc2008-03-18 20:58:39 +0000408 5
Christian Heimesb186d002008-03-18 15:15:01 +0000409 >>> Foo.method(1, *[2, 3])
Neal Norwitzd79bacc2008-03-18 20:58:39 +0000410 5
Fred Drake004d5e62000-10-23 17:22:08 +0000411
Ezio Melotti13925002011-03-16 11:05:33 +0200412A PyCFunction that takes only positional parameters should allow an
Christian Heimesb186d002008-03-18 15:15:01 +0000413empty keyword dictionary to pass without a complaint, but raise a
414TypeError if te dictionary is not empty
Jeremy Hylton074c3e62000-03-30 23:55:31 +0000415
Christian Heimesb186d002008-03-18 15:15:01 +0000416 >>> try:
417 ... silence = id(1, *{})
418 ... True
419 ... except:
420 ... False
421 True
Fred Drake004d5e62000-10-23 17:22:08 +0000422
Christian Heimesb186d002008-03-18 15:15:01 +0000423 >>> id(1, **{'foo': 1})
424 Traceback (most recent call last):
425 ...
426 TypeError: id() takes no keyword arguments
Jeremy Hylton074c3e62000-03-30 23:55:31 +0000427
Benjamin Peterson0289b152009-06-28 17:22:03 +0000428A corner case of keyword dictionary items being deleted during
429the 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 Petersona567a772010-03-21 21:00:50 +0000446
Benjamin Petersone109c702011-06-24 09:37:26 -0500447Too many arguments:
Benjamin Petersona567a772010-03-21 21:00:50 +0000448
Benjamin Petersone109c702011-06-24 09:37:26 -0500449 >>> def f(): pass
Benjamin Petersonb204a422011-06-05 22:04:07 -0500450 >>> f(1)
451 Traceback (most recent call last):
452 ...
Benjamin Petersone109c702011-06-24 09:37:26 -0500453 TypeError: f() takes 0 positional arguments but 1 was given
454 >>> def f(a): pass
455 >>> f(1, 2)
Benjamin Petersonb204a422011-06-05 22:04:07 -0500456 Traceback (most recent call last):
457 ...
Benjamin Petersone109c702011-06-24 09:37:26 -0500458 TypeError: f() takes 1 positional argument but 2 were given
459 >>> def f(a, b=1): pass
460 >>> f(1, 2, 3)
Benjamin Petersonb204a422011-06-05 22:04:07 -0500461 Traceback (most recent call last):
462 ...
Benjamin Petersone109c702011-06-24 09:37:26 -0500463 TypeError: f() takes from 1 to 2 positional arguments but 3 were given
464 >>> def f(*, kw): pass
465 >>> f(1, kw=3)
Benjamin Petersonb204a422011-06-05 22:04:07 -0500466 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 Petersone109c702011-06-24 09:37:26 -0500469 >>> 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 Petersonb204a422011-06-05 22:04:07 -0500479
Benjamin Petersone109c702011-06-24 09:37:26 -0500480Too few and missing arguments:
481
482 >>> def f(a): pass
Benjamin Petersonb204a422011-06-05 22:04:07 -0500483 >>> f()
484 Traceback (most recent call last):
Benjamin Petersone109c702011-06-24 09:37:26 -0500485 ...
486 TypeError: f() missing 1 required positional argument: 'a'
487 >>> def f(a, b): pass
488 >>> f()
Benjamin Petersonb204a422011-06-05 22:04:07 -0500489 Traceback (most recent call last):
Benjamin Petersone109c702011-06-24 09:37:26 -0500490 ...
491 TypeError: f() missing 2 required positional arguments: 'a' and 'b'
492 >>> def f(a, b, c): pass
493 >>> f()
Benjamin Petersonb204a422011-06-05 22:04:07 -0500494 Traceback (most recent call last):
Benjamin Petersone109c702011-06-24 09:37:26 -0500495 ...
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
508Same 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 Heimesb186d002008-03-18 15:15:01 +0000521"""
Samuele Pedroni8036c832004-02-21 21:03:30 +0000522
Benjamin Petersona567a772010-03-21 21:00:50 +0000523import sys
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000524from test import support
Samuele Pedroni8036c832004-02-21 21:03:30 +0000525
Christian Heimesb186d002008-03-18 15:15:01 +0000526def test_main():
Benjamin Petersona567a772010-03-21 21:00:50 +0000527 support.run_doctest(sys.modules[__name__], True)
Jeremy Hylton074c3e62000-03-30 23:55:31 +0000528
Christian Heimesb186d002008-03-18 15:15:01 +0000529if __name__ == '__main__':
530 test_main()