2
"""Doctest for method/function calls.
4
We're going the use these types for extra testing
6
>>> from collections import UserList
7
>>> from collections import UserDict
9
We're defining four helper functions
15
... print(a, support.sortdict(k))
17
>>> def g(x, *y, **z):
18
... print(x, y, support.sortdict(z))
20
>>> def h(j=1, a=2, h=3):
23
Argument list examples
33
>>> f(1, 2, 3, *(4, 5))
35
>>> f(1, 2, 3, *[4, 5])
37
>>> f(1, 2, 3, *UserList([4, 5]))
40
Here we add keyword arguments
42
>>> f(1, 2, 3, **{'a':4, 'b':5})
43
(1, 2, 3) {'a': 4, 'b': 5}
44
>>> f(1, 2, 3, *[4, 5], **{'a':6, 'b':7})
45
(1, 2, 3, 4, 5) {'a': 6, 'b': 7}
46
>>> f(1, 2, 3, x=4, y=5, *(6, 7), **{'a':8, 'b': 9})
47
(1, 2, 3, 6, 7) {'a': 8, 'b': 9, 'x': 4, 'y': 5}
49
>>> f(1, 2, 3, **UserDict(a=4, b=5))
50
(1, 2, 3) {'a': 4, 'b': 5}
51
>>> f(1, 2, 3, *(4, 5), **UserDict(a=6, b=7))
52
(1, 2, 3, 4, 5) {'a': 6, 'b': 7}
53
>>> f(1, 2, 3, x=4, y=5, *(6, 7), **UserDict(a=8, b=9))
54
(1, 2, 3, 6, 7) {'a': 8, 'b': 9, 'x': 4, 'y': 5}
56
Examples with invalid arguments (TypeErrors). We're also testing the function
57
names in the exception messages.
59
Verify clearing of SF bug #733667
62
Traceback (most recent call last):
64
TypeError: e() got an unexpected keyword argument 'c'
67
Traceback (most recent call last):
69
TypeError: g() missing 1 required positional argument: 'x'
72
Traceback (most recent call last):
74
TypeError: g() missing 1 required positional argument: 'x'
77
Traceback (most recent call last):
79
TypeError: g() missing 1 required positional argument: 'x'
87
>>> g(1, 2, 3, *(4, 5))
90
>>> class Nothing: pass
93
Traceback (most recent call last):
95
TypeError: g() argument after * must be a sequence, not Nothing
98
... def __len__(self): return 5
102
Traceback (most recent call last):
104
TypeError: g() argument after * must be a sequence, not Nothing
107
... def __len__(self): return 5
108
... def __getitem__(self, i):
110
... else: raise IndexError(i)
117
... def __init__(self): self.c = 0
118
... def __iter__(self): return self
119
... def __next__(self):
121
... raise StopIteration
130
Make sure that the function doesn't stomp the dictionary
132
>>> d = {'a': 1, 'b': 2, 'c': 3}
135
1 () {'a': 1, 'b': 2, 'c': 3, 'd': 4}
139
What about willful misconduct?
141
>>> def saboteur(**kw):
146
>>> kw = saboteur(a=1, **d)
151
>>> g(1, 2, 3, **{'x': 4, 'y': 5})
152
Traceback (most recent call last):
154
TypeError: g() got multiple values for argument 'x'
157
Traceback (most recent call last):
159
TypeError: f() keywords must be strings
162
Traceback (most recent call last):
164
TypeError: h() got an unexpected keyword argument 'e'
167
Traceback (most recent call last):
169
TypeError: h() argument after * must be a sequence, not function
172
Traceback (most recent call last):
174
TypeError: dir() argument after * must be a sequence, not function
177
Traceback (most recent call last):
179
TypeError: NoneType object argument after * must be a sequence, \
183
Traceback (most recent call last):
185
TypeError: h() argument after ** must be a mapping, not function
188
Traceback (most recent call last):
190
TypeError: dir() argument after ** must be a mapping, not function
193
Traceback (most recent call last):
195
TypeError: NoneType object argument after ** must be a mapping, \
198
>>> dir(b=1, **{'b': 1})
199
Traceback (most recent call last):
201
TypeError: dir() got multiple values for keyword argument 'b'
203
Another helper function
210
>>> for i in range(512):
213
>>> a, b = f2(1, *(2,3), **d)
214
>>> len(a), len(b), b == d
218
... def method(self, arg1, arg2):
222
>>> Foo.method(*(x, 1, 2))
224
>>> Foo.method(x, *(1, 2))
226
>>> Foo.method(*(1, 2, 3))
228
>>> Foo.method(1, *[2, 3])
231
A PyCFunction that takes only positional parameters should allow an
232
empty keyword dictionary to pass without a complaint, but raise a
233
TypeError if te dictionary is not empty
236
... silence = id(1, *{})
242
>>> id(1, **{'foo': 1})
243
Traceback (most recent call last):
245
TypeError: id() takes no keyword arguments
247
A corner case of keyword dictionary items being deleted during
248
the function call setup. See <http://bugs.python.org/issue2016>.
251
... def __eq__(self, other):
256
... return str.__eq__(self, other)
257
... def __hash__(self):
258
... return str.__hash__(self)
260
>>> x = {Name("a"):1, Name("b"):2}
270
Traceback (most recent call last):
272
TypeError: f() takes 0 positional arguments but 1 was given
275
Traceback (most recent call last):
277
TypeError: f() takes 1 positional argument but 2 were given
278
>>> def f(a, b=1): pass
280
Traceback (most recent call last):
282
TypeError: f() takes from 1 to 2 positional arguments but 3 were given
283
>>> def f(*, kw): pass
285
Traceback (most recent call last):
287
TypeError: f() takes 0 positional arguments but 1 positional argument (and 1 keyword-only argument) were given
288
>>> def f(*, kw, b): pass
289
>>> f(1, 2, 3, b=3, kw=3)
290
Traceback (most recent call last):
292
TypeError: f() takes 0 positional arguments but 3 positional arguments (and 2 keyword-only arguments) were given
293
>>> def f(a, b=2, *, kw): pass
295
Traceback (most recent call last):
297
TypeError: f() takes from 1 to 2 positional arguments but 3 positional arguments (and 1 keyword-only argument) were given
299
Too few and missing arguments:
303
Traceback (most recent call last):
305
TypeError: f() missing 1 required positional argument: 'a'
306
>>> def f(a, b): pass
308
Traceback (most recent call last):
310
TypeError: f() missing 2 required positional arguments: 'a' and 'b'
311
>>> def f(a, b, c): pass
313
Traceback (most recent call last):
315
TypeError: f() missing 3 required positional arguments: 'a', 'b', and 'c'
316
>>> def f(a, b, c, d, e): pass
318
Traceback (most recent call last):
320
TypeError: f() missing 5 required positional arguments: 'a', 'b', 'c', 'd', and 'e'
321
>>> def f(a, b=4, c=5, d=5): pass
323
Traceback (most recent call last):
325
TypeError: f() missing 1 required positional argument: 'a'
327
Same with keyword only args:
329
>>> def f(*, w): pass
331
Traceback (most recent call last):
333
TypeError: f() missing 1 required keyword-only argument: 'w'
334
>>> def f(*, a, b, c, d, e): pass
336
Traceback (most recent call last):
338
TypeError: f() missing 5 required keyword-only arguments: 'a', 'b', 'c', 'd', and 'e'
343
from test import support
346
support.run_doctest(sys.modules[__name__], True)
348
if __name__ == '__main__':