1 | n/a | """Unit tests for the keyword only argument specified in PEP 3102.""" |
---|
2 | n/a | |
---|
3 | n/a | __author__ = "Jiwon Seo" |
---|
4 | n/a | __email__ = "seojiwon at gmail dot com" |
---|
5 | n/a | |
---|
6 | n/a | import unittest |
---|
7 | n/a | |
---|
8 | n/a | def posonly_sum(pos_arg1, *arg, **kwarg): |
---|
9 | n/a | return pos_arg1 + sum(arg) + sum(kwarg.values()) |
---|
10 | n/a | def keywordonly_sum(*, k1=0, k2): |
---|
11 | n/a | return k1 + k2 |
---|
12 | n/a | def keywordonly_nodefaults_sum(*, k1, k2): |
---|
13 | n/a | return k1 + k2 |
---|
14 | n/a | def keywordonly_and_kwarg_sum(*, k1, k2, **kwarg): |
---|
15 | n/a | return k1 + k2 + sum(kwarg.values()) |
---|
16 | n/a | def mixedargs_sum(a, b=0, *arg, k1, k2=0): |
---|
17 | n/a | return a + b + k1 + k2 + sum(arg) |
---|
18 | n/a | def mixedargs_sum2(a, b=0, *arg, k1, k2=0, **kwargs): |
---|
19 | n/a | return a + b + k1 + k2 + sum(arg) + sum(kwargs.values()) |
---|
20 | n/a | |
---|
21 | n/a | def sortnum(*nums, reverse=False): |
---|
22 | n/a | return sorted(list(nums), reverse=reverse) |
---|
23 | n/a | |
---|
24 | n/a | def sortwords(*words, reverse=False, **kwargs): |
---|
25 | n/a | return sorted(list(words), reverse=reverse) |
---|
26 | n/a | |
---|
27 | n/a | class Foo: |
---|
28 | n/a | def __init__(self, *, k1, k2=0): |
---|
29 | n/a | self.k1 = k1 |
---|
30 | n/a | self.k2 = k2 |
---|
31 | n/a | def set(self, p1, *, k1, k2): |
---|
32 | n/a | self.k1 = k1 |
---|
33 | n/a | self.k2 = k2 |
---|
34 | n/a | def sum(self): |
---|
35 | n/a | return self.k1 + self.k2 |
---|
36 | n/a | |
---|
37 | n/a | class KeywordOnlyArgTestCase(unittest.TestCase): |
---|
38 | n/a | def assertRaisesSyntaxError(self, codestr): |
---|
39 | n/a | def shouldRaiseSyntaxError(s): |
---|
40 | n/a | compile(s, "<test>", "single") |
---|
41 | n/a | self.assertRaises(SyntaxError, shouldRaiseSyntaxError, codestr) |
---|
42 | n/a | |
---|
43 | n/a | def testSyntaxErrorForFunctionDefinition(self): |
---|
44 | n/a | self.assertRaisesSyntaxError("def f(p, *):\n pass\n") |
---|
45 | n/a | self.assertRaisesSyntaxError("def f(p1, *, p1=100):\n pass\n") |
---|
46 | n/a | self.assertRaisesSyntaxError("def f(p1, *k1, k1=100):\n pass\n") |
---|
47 | n/a | self.assertRaisesSyntaxError("def f(p1, *, k1, k1=100):\n pass\n") |
---|
48 | n/a | self.assertRaisesSyntaxError("def f(p1, *, **k1):\n pass\n") |
---|
49 | n/a | self.assertRaisesSyntaxError("def f(p1, *, k1, **k1):\n pass\n") |
---|
50 | n/a | self.assertRaisesSyntaxError("def f(p1, *, None, **k1):\n pass\n") |
---|
51 | n/a | self.assertRaisesSyntaxError("def f(p, *, (k1, k2), **kw):\n pass\n") |
---|
52 | n/a | |
---|
53 | n/a | def testSyntaxForManyArguments(self): |
---|
54 | n/a | # more than 255 positional arguments, should compile ok |
---|
55 | n/a | fundef = "def f(%s):\n pass\n" % ', '.join('i%d' % i for i in range(300)) |
---|
56 | n/a | compile(fundef, "<test>", "single") |
---|
57 | n/a | # more than 255 keyword-only arguments, should compile ok |
---|
58 | n/a | fundef = "def f(*, %s):\n pass\n" % ', '.join('i%d' % i for i in range(300)) |
---|
59 | n/a | compile(fundef, "<test>", "single") |
---|
60 | n/a | |
---|
61 | n/a | def testTooManyPositionalErrorMessage(self): |
---|
62 | n/a | def f(a, b=None, *, c=None): |
---|
63 | n/a | pass |
---|
64 | n/a | with self.assertRaises(TypeError) as exc: |
---|
65 | n/a | f(1, 2, 3) |
---|
66 | n/a | expected = "f() takes from 1 to 2 positional arguments but 3 were given" |
---|
67 | n/a | self.assertEqual(str(exc.exception), expected) |
---|
68 | n/a | |
---|
69 | n/a | def testSyntaxErrorForFunctionCall(self): |
---|
70 | n/a | self.assertRaisesSyntaxError("f(p, k=1, p2)") |
---|
71 | n/a | self.assertRaisesSyntaxError("f(p, k1=50, *(1,2), k1=100)") |
---|
72 | n/a | |
---|
73 | n/a | def testRaiseErrorFuncallWithUnexpectedKeywordArgument(self): |
---|
74 | n/a | self.assertRaises(TypeError, keywordonly_sum, ()) |
---|
75 | n/a | self.assertRaises(TypeError, keywordonly_nodefaults_sum, ()) |
---|
76 | n/a | self.assertRaises(TypeError, Foo, ()) |
---|
77 | n/a | try: |
---|
78 | n/a | keywordonly_sum(k2=100, non_existing_arg=200) |
---|
79 | n/a | self.fail("should raise TypeError") |
---|
80 | n/a | except TypeError: |
---|
81 | n/a | pass |
---|
82 | n/a | try: |
---|
83 | n/a | keywordonly_nodefaults_sum(k2=2) |
---|
84 | n/a | self.fail("should raise TypeError") |
---|
85 | n/a | except TypeError: |
---|
86 | n/a | pass |
---|
87 | n/a | |
---|
88 | n/a | def testFunctionCall(self): |
---|
89 | n/a | self.assertEqual(1, posonly_sum(1)) |
---|
90 | n/a | self.assertEqual(1+2, posonly_sum(1,**{"2":2})) |
---|
91 | n/a | self.assertEqual(1+2+3, posonly_sum(1,*(2,3))) |
---|
92 | n/a | self.assertEqual(1+2+3+4, posonly_sum(1,*(2,3),**{"4":4})) |
---|
93 | n/a | |
---|
94 | n/a | self.assertEqual(1, keywordonly_sum(k2=1)) |
---|
95 | n/a | self.assertEqual(1+2, keywordonly_sum(k1=1, k2=2)) |
---|
96 | n/a | |
---|
97 | n/a | self.assertEqual(1+2, keywordonly_and_kwarg_sum(k1=1, k2=2)) |
---|
98 | n/a | self.assertEqual(1+2+3, keywordonly_and_kwarg_sum(k1=1, k2=2, k3=3)) |
---|
99 | n/a | self.assertEqual(1+2+3+4, |
---|
100 | n/a | keywordonly_and_kwarg_sum(k1=1, k2=2, |
---|
101 | n/a | **{"a":3,"b":4})) |
---|
102 | n/a | |
---|
103 | n/a | self.assertEqual(1+2, mixedargs_sum(1, k1=2)) |
---|
104 | n/a | self.assertEqual(1+2+3, mixedargs_sum(1, 2, k1=3)) |
---|
105 | n/a | self.assertEqual(1+2+3+4, mixedargs_sum(1, 2, k1=3, k2=4)) |
---|
106 | n/a | self.assertEqual(1+2+3+4+5, mixedargs_sum(1, 2, 3, k1=4, k2=5)) |
---|
107 | n/a | |
---|
108 | n/a | self.assertEqual(1+2, mixedargs_sum2(1, k1=2)) |
---|
109 | n/a | self.assertEqual(1+2+3, mixedargs_sum2(1, 2, k1=3)) |
---|
110 | n/a | self.assertEqual(1+2+3+4, mixedargs_sum2(1, 2, k1=3, k2=4)) |
---|
111 | n/a | self.assertEqual(1+2+3+4+5, mixedargs_sum2(1, 2, 3, k1=4, k2=5)) |
---|
112 | n/a | self.assertEqual(1+2+3+4+5+6, |
---|
113 | n/a | mixedargs_sum2(1, 2, 3, k1=4, k2=5, k3=6)) |
---|
114 | n/a | self.assertEqual(1+2+3+4+5+6, |
---|
115 | n/a | mixedargs_sum2(1, 2, 3, k1=4, **{'k2':5, 'k3':6})) |
---|
116 | n/a | |
---|
117 | n/a | self.assertEqual(1, Foo(k1=1).sum()) |
---|
118 | n/a | self.assertEqual(1+2, Foo(k1=1,k2=2).sum()) |
---|
119 | n/a | |
---|
120 | n/a | self.assertEqual([1,2,3], sortnum(3,2,1)) |
---|
121 | n/a | self.assertEqual([3,2,1], sortnum(1,2,3, reverse=True)) |
---|
122 | n/a | |
---|
123 | n/a | self.assertEqual(['a','b','c'], sortwords('a','c','b')) |
---|
124 | n/a | self.assertEqual(['c','b','a'], sortwords('a','c','b', reverse=True)) |
---|
125 | n/a | self.assertEqual(['c','b','a'], |
---|
126 | n/a | sortwords('a','c','b', reverse=True, ignore='ignore')) |
---|
127 | n/a | |
---|
128 | n/a | def testKwDefaults(self): |
---|
129 | n/a | def foo(p1,p2=0, *, k1, k2=0): |
---|
130 | n/a | return p1 + p2 + k1 + k2 |
---|
131 | n/a | |
---|
132 | n/a | self.assertEqual(2, foo.__code__.co_kwonlyargcount) |
---|
133 | n/a | self.assertEqual({"k2":0}, foo.__kwdefaults__) |
---|
134 | n/a | foo.__kwdefaults__ = {"k1":0} |
---|
135 | n/a | try: |
---|
136 | n/a | foo(1,k1=10) |
---|
137 | n/a | self.fail("__kwdefaults__ is not properly changed") |
---|
138 | n/a | except TypeError: |
---|
139 | n/a | pass |
---|
140 | n/a | |
---|
141 | n/a | def test_kwonly_methods(self): |
---|
142 | n/a | class Example: |
---|
143 | n/a | def f(self, *, k1=1, k2=2): |
---|
144 | n/a | return k1, k2 |
---|
145 | n/a | |
---|
146 | n/a | self.assertEqual(Example().f(k1=1, k2=2), (1, 2)) |
---|
147 | n/a | self.assertEqual(Example.f(Example(), k1=1, k2=2), (1, 2)) |
---|
148 | n/a | self.assertRaises(TypeError, Example.f, k1=1, k2=2) |
---|
149 | n/a | |
---|
150 | n/a | def test_issue13343(self): |
---|
151 | n/a | # The Python compiler must scan all symbols of a function to |
---|
152 | n/a | # determine their scope: global, local, cell... |
---|
153 | n/a | # This was not done for the default values of keyword |
---|
154 | n/a | # arguments in a lambda definition, and the following line |
---|
155 | n/a | # used to fail with a SystemError. |
---|
156 | n/a | lambda *, k1=unittest: None |
---|
157 | n/a | |
---|
158 | n/a | def test_mangling(self): |
---|
159 | n/a | class X: |
---|
160 | n/a | def f(self, *, __a=42): |
---|
161 | n/a | return __a |
---|
162 | n/a | self.assertEqual(X().f(), 42) |
---|
163 | n/a | |
---|
164 | n/a | def test_default_evaluation_order(self): |
---|
165 | n/a | # See issue 16967 |
---|
166 | n/a | a = 42 |
---|
167 | n/a | with self.assertRaises(NameError) as err: |
---|
168 | n/a | def f(v=a, x=b, *, y=c, z=d): |
---|
169 | n/a | pass |
---|
170 | n/a | self.assertEqual(str(err.exception), "name 'b' is not defined") |
---|
171 | n/a | with self.assertRaises(NameError) as err: |
---|
172 | n/a | f = lambda v=a, x=b, *, y=c, z=d: None |
---|
173 | n/a | self.assertEqual(str(err.exception), "name 'b' is not defined") |
---|
174 | n/a | |
---|
175 | n/a | |
---|
176 | n/a | if __name__ == "__main__": |
---|
177 | n/a | unittest.main() |
---|