blob: 9cb0d38669be5874080585df9b6bb118b8421387 [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 ...
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 Storchaka3c317e72016-06-12 09:22:01 +030060 >>> 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 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
82Examples with invalid arguments (TypeErrors). We're also testing the function
83names in the exception messages.
84
85Verify 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 Petersone109c702011-06-24 09:37:26 -050095 TypeError: g() missing 1 required positional argument: 'x'
Christian Heimesb186d002008-03-18 15:15:01 +000096
97 >>> g(*())
98 Traceback (most recent call last):
99 ...
Benjamin Petersone109c702011-06-24 09:37:26 -0500100 TypeError: g() missing 1 required positional argument: 'x'
Christian Heimesb186d002008-03-18 15:15:01 +0000101
102 >>> g(*(), **{})
103 Traceback (most recent call last):
104 ...
Benjamin Petersone109c702011-06-24 09:37:26 -0500105 TypeError: g() missing 1 required positional argument: 'x'
Christian Heimesb186d002008-03-18 15:15:01 +0000106
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 ...
Martin Panterb5944222016-01-31 06:30:56 +0000121 TypeError: g() argument after * must be an iterable, not Nothing
Christian Heimesb186d002008-03-18 15:15:01 +0000122
123 >>> class Nothing:
124 ... def __len__(self): return 5
125 ...
126
127 >>> g(*Nothing())
128 Traceback (most recent call last):
129 ...
Martin Panterb5944222016-01-31 06:30:56 +0000130 TypeError: g() argument after * must be an iterable, not Nothing
Christian Heimesb186d002008-03-18 15:15:01 +0000131
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 Norwitzd79bacc2008-03-18 20:58:39 +0000145 ... def __next__(self):
Christian Heimesb186d002008-03-18 15:15:01 +0000146 ... 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 Panterb5944222016-01-31 06:30:56 +0000156Check for issue #4806: Does a TypeError in a generator get propagated with the
157right 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
166
167 >>> class BrokenIterable1:
168 ... def __iter__(self):
169 ... raise TypeError('myerror')
170 ...
171 >>> g(*BrokenIterable1())
172 Traceback (most recent call last):
173 ...
174 TypeError: myerror
175
176 >>> class BrokenIterable2:
177 ... def __iter__(self):
178 ... yield 0
179 ... raise TypeError('myerror')
180 ...
181 >>> g(*BrokenIterable2())
182 Traceback (most recent call last):
183 ...
184 TypeError: myerror
185
186 >>> class BrokenSequence:
187 ... def __getitem__(self, idx):
188 ... raise TypeError('myerror')
189 ...
190 >>> g(*BrokenSequence())
191 Traceback (most recent call last):
192 ...
193 TypeError: myerror
194
Christian Heimesb186d002008-03-18 15:15:01 +0000195Make sure that the function doesn't stomp the dictionary
196
197 >>> d = {'a': 1, 'b': 2, 'c': 3}
198 >>> d2 = d.copy()
199 >>> g(1, d=4, **d)
200 1 () {'a': 1, 'b': 2, 'c': 3, 'd': 4}
201 >>> d == d2
202 True
203
204What about willful misconduct?
205
206 >>> def saboteur(**kw):
207 ... kw['x'] = 'm'
208 ... return kw
209
210 >>> d = {}
211 >>> kw = saboteur(a=1, **d)
212 >>> d
213 {}
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000214
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000215
Christian Heimesb186d002008-03-18 15:15:01 +0000216 >>> g(1, 2, 3, **{'x': 4, 'y': 5})
217 Traceback (most recent call last):
218 ...
Benjamin Petersonb204a422011-06-05 22:04:07 -0500219 TypeError: g() got multiple values for argument 'x'
Christian Heimesb186d002008-03-18 15:15:01 +0000220
221 >>> f(**{1:2})
222 Traceback (most recent call last):
223 ...
224 TypeError: f() keywords must be strings
225
226 >>> h(**{'e': 2})
227 Traceback (most recent call last):
228 ...
229 TypeError: h() got an unexpected keyword argument 'e'
230
231 >>> h(*h)
232 Traceback (most recent call last):
233 ...
Martin Panterb5944222016-01-31 06:30:56 +0000234 TypeError: h() argument after * must be an iterable, not function
Christian Heimesb186d002008-03-18 15:15:01 +0000235
Serhiy Storchaka6498e522016-10-02 10:36:33 +0300236 >>> h(1, *h)
237 Traceback (most recent call last):
238 ...
239 TypeError: h() argument after * must be an iterable, not function
240
Christian Heimesb186d002008-03-18 15:15:01 +0000241 >>> dir(*h)
242 Traceback (most recent call last):
243 ...
Martin Panterb5944222016-01-31 06:30:56 +0000244 TypeError: dir() argument after * must be an iterable, not function
Christian Heimesb186d002008-03-18 15:15:01 +0000245
246 >>> None(*h)
247 Traceback (most recent call last):
248 ...
Martin Panterb5944222016-01-31 06:30:56 +0000249 TypeError: NoneType object argument after * must be an iterable, \
Christian Heimesb186d002008-03-18 15:15:01 +0000250not function
251
252 >>> h(**h)
253 Traceback (most recent call last):
254 ...
255 TypeError: h() argument after ** must be a mapping, not function
256
Serhiy Storchakad556a352016-10-02 11:10:18 +0300257 >>> h(**[])
258 Traceback (most recent call last):
259 ...
260 TypeError: h() argument after ** must be a mapping, not list
261
262 >>> h(a=1, **h)
263 Traceback (most recent call last):
264 ...
265 TypeError: h() argument after ** must be a mapping, not function
266
267 >>> h(a=1, **[])
268 Traceback (most recent call last):
269 ...
270 TypeError: h() argument after ** must be a mapping, not list
271
Serhiy Storchaka56653012016-10-07 23:32:41 +0300272 >>> h(**{'a': 1}, **h)
273 Traceback (most recent call last):
274 ...
275 TypeError: h() argument after ** must be a mapping, not function
276
277 >>> h(**{'a': 1}, **[])
278 Traceback (most recent call last):
279 ...
280 TypeError: h() argument after ** must be a mapping, not list
281
Christian Heimesb186d002008-03-18 15:15:01 +0000282 >>> dir(**h)
283 Traceback (most recent call last):
284 ...
285 TypeError: dir() argument after ** must be a mapping, not function
286
287 >>> None(**h)
288 Traceback (most recent call last):
289 ...
290 TypeError: NoneType object argument after ** must be a mapping, \
291not function
292
293 >>> dir(b=1, **{'b': 1})
294 Traceback (most recent call last):
295 ...
296 TypeError: dir() got multiple values for keyword argument 'b'
297
298Another helper function
299
300 >>> def f2(*a, **b):
301 ... return a, b
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000302
303
Christian Heimesb186d002008-03-18 15:15:01 +0000304 >>> d = {}
Neal Norwitzd79bacc2008-03-18 20:58:39 +0000305 >>> for i in range(512):
Christian Heimesb186d002008-03-18 15:15:01 +0000306 ... key = 'k%d' % i
307 ... d[key] = i
308 >>> a, b = f2(1, *(2,3), **d)
309 >>> len(a), len(b), b == d
310 (3, 512, True)
Raymond Hettinger40174c32003-05-31 07:04:16 +0000311
Christian Heimesb186d002008-03-18 15:15:01 +0000312 >>> class Foo:
313 ... def method(self, arg1, arg2):
314 ... return arg1+arg2
Fred Drake004d5e62000-10-23 17:22:08 +0000315
Christian Heimesb186d002008-03-18 15:15:01 +0000316 >>> x = Foo()
317 >>> Foo.method(*(x, 1, 2))
318 3
319 >>> Foo.method(x, *(1, 2))
320 3
321 >>> Foo.method(*(1, 2, 3))
Neal Norwitzd79bacc2008-03-18 20:58:39 +0000322 5
Christian Heimesb186d002008-03-18 15:15:01 +0000323 >>> Foo.method(1, *[2, 3])
Neal Norwitzd79bacc2008-03-18 20:58:39 +0000324 5
Fred Drake004d5e62000-10-23 17:22:08 +0000325
Ezio Melotti13925002011-03-16 11:05:33 +0200326A PyCFunction that takes only positional parameters should allow an
Christian Heimesb186d002008-03-18 15:15:01 +0000327empty keyword dictionary to pass without a complaint, but raise a
328TypeError if te dictionary is not empty
Jeremy Hylton074c3e62000-03-30 23:55:31 +0000329
Christian Heimesb186d002008-03-18 15:15:01 +0000330 >>> try:
331 ... silence = id(1, *{})
332 ... True
333 ... except:
334 ... False
335 True
Fred Drake004d5e62000-10-23 17:22:08 +0000336
Christian Heimesb186d002008-03-18 15:15:01 +0000337 >>> id(1, **{'foo': 1})
338 Traceback (most recent call last):
339 ...
340 TypeError: id() takes no keyword arguments
Jeremy Hylton074c3e62000-03-30 23:55:31 +0000341
Benjamin Peterson0289b152009-06-28 17:22:03 +0000342A corner case of keyword dictionary items being deleted during
343the function call setup. See <http://bugs.python.org/issue2016>.
344
345 >>> class Name(str):
346 ... def __eq__(self, other):
347 ... try:
348 ... del x[self]
349 ... except KeyError:
350 ... pass
351 ... return str.__eq__(self, other)
352 ... def __hash__(self):
353 ... return str.__hash__(self)
354
355 >>> x = {Name("a"):1, Name("b"):2}
356 >>> def f(a, b):
357 ... print(a,b)
358 >>> f(**x)
359 1 2
Benjamin Petersona567a772010-03-21 21:00:50 +0000360
Benjamin Petersone109c702011-06-24 09:37:26 -0500361Too many arguments:
Benjamin Petersona567a772010-03-21 21:00:50 +0000362
Benjamin Petersone109c702011-06-24 09:37:26 -0500363 >>> def f(): pass
Benjamin Petersonb204a422011-06-05 22:04:07 -0500364 >>> f(1)
365 Traceback (most recent call last):
366 ...
Benjamin Petersone109c702011-06-24 09:37:26 -0500367 TypeError: f() takes 0 positional arguments but 1 was given
368 >>> def f(a): pass
369 >>> f(1, 2)
Benjamin Petersonb204a422011-06-05 22:04:07 -0500370 Traceback (most recent call last):
371 ...
Benjamin Petersone109c702011-06-24 09:37:26 -0500372 TypeError: f() takes 1 positional argument but 2 were given
373 >>> def f(a, b=1): pass
374 >>> f(1, 2, 3)
Benjamin Petersonb204a422011-06-05 22:04:07 -0500375 Traceback (most recent call last):
376 ...
Benjamin Petersone109c702011-06-24 09:37:26 -0500377 TypeError: f() takes from 1 to 2 positional arguments but 3 were given
378 >>> def f(*, kw): pass
379 >>> f(1, kw=3)
Benjamin Petersonb204a422011-06-05 22:04:07 -0500380 Traceback (most recent call last):
381 ...
382 TypeError: f() takes 0 positional arguments but 1 positional argument (and 1 keyword-only argument) were given
Benjamin Petersone109c702011-06-24 09:37:26 -0500383 >>> def f(*, kw, b): pass
384 >>> f(1, 2, 3, b=3, kw=3)
385 Traceback (most recent call last):
386 ...
387 TypeError: f() takes 0 positional arguments but 3 positional arguments (and 2 keyword-only arguments) were given
388 >>> def f(a, b=2, *, kw): pass
389 >>> f(2, 3, 4, kw=4)
390 Traceback (most recent call last):
391 ...
392 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 -0500393
Benjamin Petersone109c702011-06-24 09:37:26 -0500394Too few and missing arguments:
395
396 >>> def f(a): pass
Benjamin Petersonb204a422011-06-05 22:04:07 -0500397 >>> f()
398 Traceback (most recent call last):
Benjamin Petersone109c702011-06-24 09:37:26 -0500399 ...
400 TypeError: f() missing 1 required positional argument: 'a'
401 >>> def f(a, b): pass
402 >>> f()
Benjamin Petersonb204a422011-06-05 22:04:07 -0500403 Traceback (most recent call last):
Benjamin Petersone109c702011-06-24 09:37:26 -0500404 ...
405 TypeError: f() missing 2 required positional arguments: 'a' and 'b'
406 >>> def f(a, b, c): pass
407 >>> f()
Benjamin Petersonb204a422011-06-05 22:04:07 -0500408 Traceback (most recent call last):
Benjamin Petersone109c702011-06-24 09:37:26 -0500409 ...
410 TypeError: f() missing 3 required positional arguments: 'a', 'b', and 'c'
411 >>> def f(a, b, c, d, e): pass
412 >>> f()
413 Traceback (most recent call last):
414 ...
415 TypeError: f() missing 5 required positional arguments: 'a', 'b', 'c', 'd', and 'e'
416 >>> def f(a, b=4, c=5, d=5): pass
417 >>> f(c=12, b=9)
418 Traceback (most recent call last):
419 ...
420 TypeError: f() missing 1 required positional argument: 'a'
421
422Same with keyword only args:
423
424 >>> def f(*, w): pass
425 >>> f()
426 Traceback (most recent call last):
427 ...
428 TypeError: f() missing 1 required keyword-only argument: 'w'
429 >>> def f(*, a, b, c, d, e): pass
430 >>> f()
431 Traceback (most recent call last):
432 ...
433 TypeError: f() missing 5 required keyword-only arguments: 'a', 'b', 'c', 'd', and 'e'
434
Christian Heimesb186d002008-03-18 15:15:01 +0000435"""
Samuele Pedroni8036c832004-02-21 21:03:30 +0000436
Benjamin Petersona567a772010-03-21 21:00:50 +0000437import sys
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000438from test import support
Samuele Pedroni8036c832004-02-21 21:03:30 +0000439
Christian Heimesb186d002008-03-18 15:15:01 +0000440def test_main():
Benjamin Petersona567a772010-03-21 21:00:50 +0000441 support.run_doctest(sys.modules[__name__], True)
Jeremy Hylton074c3e62000-03-30 23:55:31 +0000442
Christian Heimesb186d002008-03-18 15:15:01 +0000443if __name__ == '__main__':
444 test_main()