blob: d526b5f1924c17c4ebeea27e34b38d2f1cd697f2 [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'
Christian Heimesb186d002008-03-18 15:15:01 +000060 >>> 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 Peterson025e9eb2015-05-05 20:16:41 -040064 >>> 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 Heimesb186d002008-03-18 15:15:01 +000068
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 Peterson025e9eb2015-05-05 20:16:41 -040075 >>> 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 Heimesb186d002008-03-18 15:15:01 +000077
78Examples with invalid arguments (TypeErrors). We're also testing the function
79names in the exception messages.
80
81Verify 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 Petersone109c702011-06-24 09:37:26 -050091 TypeError: g() missing 1 required positional argument: 'x'
Christian Heimesb186d002008-03-18 15:15:01 +000092
93 >>> g(*())
94 Traceback (most recent call last):
95 ...
Benjamin Petersone109c702011-06-24 09:37:26 -050096 TypeError: g() missing 1 required positional argument: 'x'
Christian Heimesb186d002008-03-18 15:15:01 +000097
98 >>> g(*(), **{})
99 Traceback (most recent call last):
100 ...
Benjamin Petersone109c702011-06-24 09:37:26 -0500101 TypeError: g() missing 1 required positional argument: 'x'
Christian Heimesb186d002008-03-18 15:15:01 +0000102
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 Panterb5944222016-01-31 06:30:56 +0000117 TypeError: g() argument after * must be an iterable, not Nothing
Christian Heimesb186d002008-03-18 15:15:01 +0000118
119 >>> class Nothing:
120 ... def __len__(self): return 5
121 ...
122
123 >>> g(*Nothing())
124 Traceback (most recent call last):
125 ...
Martin Panterb5944222016-01-31 06:30:56 +0000126 TypeError: g() argument after * must be an iterable, not Nothing
Christian Heimesb186d002008-03-18 15:15:01 +0000127
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 Norwitzd79bacc2008-03-18 20:58:39 +0000141 ... def __next__(self):
Christian Heimesb186d002008-03-18 15:15:01 +0000142 ... 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 Panterb5944222016-01-31 06:30:56 +0000152Check for issue #4806: Does a TypeError in a generator get propagated with the
153right 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 Heimesb186d002008-03-18 15:15:01 +0000191Make 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
200What 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 Hyltonaed0d8d2000-03-28 23:51:17 +0000210
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000211
Christian Heimesb186d002008-03-18 15:15:01 +0000212 >>> g(1, 2, 3, **{'x': 4, 'y': 5})
213 Traceback (most recent call last):
214 ...
Benjamin Petersonb204a422011-06-05 22:04:07 -0500215 TypeError: g() got multiple values for argument 'x'
Christian Heimesb186d002008-03-18 15:15:01 +0000216
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 Panterb5944222016-01-31 06:30:56 +0000230 TypeError: h() argument after * must be an iterable, not function
Christian Heimesb186d002008-03-18 15:15:01 +0000231
232 >>> dir(*h)
233 Traceback (most recent call last):
234 ...
Martin Panterb5944222016-01-31 06:30:56 +0000235 TypeError: dir() argument after * must be an iterable, not function
Christian Heimesb186d002008-03-18 15:15:01 +0000236
237 >>> None(*h)
238 Traceback (most recent call last):
239 ...
Martin Panterb5944222016-01-31 06:30:56 +0000240 TypeError: NoneType object argument after * must be an iterable, \
Christian Heimesb186d002008-03-18 15:15:01 +0000241not 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, \
257not 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
264Another helper function
265
266 >>> def f2(*a, **b):
267 ... return a, b
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000268
269
Christian Heimesb186d002008-03-18 15:15:01 +0000270 >>> d = {}
Neal Norwitzd79bacc2008-03-18 20:58:39 +0000271 >>> for i in range(512):
Christian Heimesb186d002008-03-18 15:15:01 +0000272 ... 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 Hettinger40174c32003-05-31 07:04:16 +0000277
Christian Heimesb186d002008-03-18 15:15:01 +0000278 >>> class Foo:
279 ... def method(self, arg1, arg2):
280 ... return arg1+arg2
Fred Drake004d5e62000-10-23 17:22:08 +0000281
Christian Heimesb186d002008-03-18 15:15:01 +0000282 >>> 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 Norwitzd79bacc2008-03-18 20:58:39 +0000288 5
Christian Heimesb186d002008-03-18 15:15:01 +0000289 >>> Foo.method(1, *[2, 3])
Neal Norwitzd79bacc2008-03-18 20:58:39 +0000290 5
Fred Drake004d5e62000-10-23 17:22:08 +0000291
Ezio Melotti13925002011-03-16 11:05:33 +0200292A PyCFunction that takes only positional parameters should allow an
Christian Heimesb186d002008-03-18 15:15:01 +0000293empty keyword dictionary to pass without a complaint, but raise a
294TypeError if te dictionary is not empty
Jeremy Hylton074c3e62000-03-30 23:55:31 +0000295
Christian Heimesb186d002008-03-18 15:15:01 +0000296 >>> try:
297 ... silence = id(1, *{})
298 ... True
299 ... except:
300 ... False
301 True
Fred Drake004d5e62000-10-23 17:22:08 +0000302
Christian Heimesb186d002008-03-18 15:15:01 +0000303 >>> id(1, **{'foo': 1})
304 Traceback (most recent call last):
305 ...
306 TypeError: id() takes no keyword arguments
Jeremy Hylton074c3e62000-03-30 23:55:31 +0000307
Benjamin Peterson0289b152009-06-28 17:22:03 +0000308A corner case of keyword dictionary items being deleted during
309the 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 Petersona567a772010-03-21 21:00:50 +0000326
Benjamin Petersone109c702011-06-24 09:37:26 -0500327Too many arguments:
Benjamin Petersona567a772010-03-21 21:00:50 +0000328
Benjamin Petersone109c702011-06-24 09:37:26 -0500329 >>> def f(): pass
Benjamin Petersonb204a422011-06-05 22:04:07 -0500330 >>> f(1)
331 Traceback (most recent call last):
332 ...
Benjamin Petersone109c702011-06-24 09:37:26 -0500333 TypeError: f() takes 0 positional arguments but 1 was given
334 >>> def f(a): pass
335 >>> f(1, 2)
Benjamin Petersonb204a422011-06-05 22:04:07 -0500336 Traceback (most recent call last):
337 ...
Benjamin Petersone109c702011-06-24 09:37:26 -0500338 TypeError: f() takes 1 positional argument but 2 were given
339 >>> def f(a, b=1): pass
340 >>> f(1, 2, 3)
Benjamin Petersonb204a422011-06-05 22:04:07 -0500341 Traceback (most recent call last):
342 ...
Benjamin Petersone109c702011-06-24 09:37:26 -0500343 TypeError: f() takes from 1 to 2 positional arguments but 3 were given
344 >>> def f(*, kw): pass
345 >>> f(1, kw=3)
Benjamin Petersonb204a422011-06-05 22:04:07 -0500346 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 Petersone109c702011-06-24 09:37:26 -0500349 >>> 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 Petersonb204a422011-06-05 22:04:07 -0500359
Benjamin Petersone109c702011-06-24 09:37:26 -0500360Too few and missing arguments:
361
362 >>> def f(a): pass
Benjamin Petersonb204a422011-06-05 22:04:07 -0500363 >>> f()
364 Traceback (most recent call last):
Benjamin Petersone109c702011-06-24 09:37:26 -0500365 ...
366 TypeError: f() missing 1 required positional argument: 'a'
367 >>> def f(a, b): pass
368 >>> f()
Benjamin Petersonb204a422011-06-05 22:04:07 -0500369 Traceback (most recent call last):
Benjamin Petersone109c702011-06-24 09:37:26 -0500370 ...
371 TypeError: f() missing 2 required positional arguments: 'a' and 'b'
372 >>> def f(a, b, c): pass
373 >>> f()
Benjamin Petersonb204a422011-06-05 22:04:07 -0500374 Traceback (most recent call last):
Benjamin Petersone109c702011-06-24 09:37:26 -0500375 ...
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
388Same 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 Heimesb186d002008-03-18 15:15:01 +0000401"""
Samuele Pedroni8036c832004-02-21 21:03:30 +0000402
Benjamin Petersona567a772010-03-21 21:00:50 +0000403import sys
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000404from test import support
Samuele Pedroni8036c832004-02-21 21:03:30 +0000405
Christian Heimesb186d002008-03-18 15:15:01 +0000406def test_main():
Benjamin Petersona567a772010-03-21 21:00:50 +0000407 support.run_doctest(sys.modules[__name__], True)
Jeremy Hylton074c3e62000-03-30 23:55:31 +0000408
Christian Heimesb186d002008-03-18 15:15:01 +0000409if __name__ == '__main__':
410 test_main()