ยปCore Development>Code coverage>Lib/test/test_keywordonlyarg.py

Python code coverage for Lib/test/test_keywordonlyarg.py

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