|
| 1 | +# Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
| 2 | +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 3 | +# |
| 4 | +# The Universal Permissive License (UPL), Version 1.0 |
| 5 | +# |
| 6 | +# Subject to the condition set forth below, permission is hereby granted to any |
| 7 | +# person obtaining a copy of this software, associated documentation and/or |
| 8 | +# data (collectively the "Software"), free of charge and under any and all |
| 9 | +# copyright rights in the Software, and any and all patent rights owned or |
| 10 | +# freely licensable by each licensor hereunder covering either (i) the |
| 11 | +# unmodified Software as contributed to or provided by such licensor, or (ii) |
| 12 | +# the Larger Works (as defined below), to deal in both |
| 13 | +# |
| 14 | +# (a) the Software, and |
| 15 | +# |
| 16 | +# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 17 | +# one is included with the Software each a "Larger Work" to which the Software |
| 18 | +# is contributed by such licensors), |
| 19 | +# |
| 20 | +# without restriction, including without limitation the rights to copy, create |
| 21 | +# derivative works of, display, perform, and distribute the Software and make, |
| 22 | +# use, sell, offer for sale, import, export, have made, and have sold the |
| 23 | +# Software and the Larger Work(s), and to sublicense the foregoing rights on |
| 24 | +# either these or other terms. |
| 25 | +# |
| 26 | +# This license is subject to the following condition: |
| 27 | +# |
| 28 | +# The above copyright notice and either this complete permission notice or at a |
| 29 | +# minimum a reference to the UPL must be included in all copies or substantial |
| 30 | +# portions of the Software. |
| 31 | +# |
| 32 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 33 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 34 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 35 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 36 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 37 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 38 | +# SOFTWARE. |
| 39 | + |
| 40 | + |
| 41 | +def assert_raises(err, fn, *args, **kwargs): |
| 42 | + raised = False |
| 43 | + try: |
| 44 | + fn(*args, **kwargs) |
| 45 | + except err: |
| 46 | + raised = True |
| 47 | + assert raised |
| 48 | + |
| 49 | + |
| 50 | +def test_positional_args(): |
| 51 | + def foo(a, b, c): |
| 52 | + return a, b, c |
| 53 | + |
| 54 | + assert foo(1, 2, 3) == (1, 2, 3) |
| 55 | + assert foo(a=1, b=2, c=3) == (1, 2, 3) |
| 56 | + assert foo(c=3, a=1, b=2) == (1, 2, 3) |
| 57 | + assert foo(1, 2, c=3) == (1, 2, 3) |
| 58 | + assert foo(1, b=2, c=3) == (1, 2, 3) |
| 59 | + assert_raises(TypeError, foo, (1,), c=3) |
| 60 | + assert_raises(SyntaxError, exec, 'foo(a=1, 2, 3)') |
| 61 | + assert_raises(TypeError, foo, (1, 2), b=2) |
| 62 | + |
| 63 | + |
| 64 | +bar = 10 |
| 65 | + |
| 66 | + |
| 67 | +def f1(*args): |
| 68 | + pass |
| 69 | + |
| 70 | + |
| 71 | +def f2(*args, **kw): |
| 72 | + pass |
| 73 | + |
| 74 | + |
| 75 | +def f3(**kw): |
| 76 | + pass |
| 77 | + |
| 78 | + |
| 79 | +def f4(foo=bar): |
| 80 | + pass |
| 81 | + |
| 82 | + |
| 83 | +def f5(foo=bar, *args): |
| 84 | + pass |
| 85 | + |
| 86 | + |
| 87 | +def f6(foo=bar, *args, **kw): |
| 88 | + pass |
| 89 | + |
| 90 | + |
| 91 | +def f7(foo=bar, **kw): |
| 92 | + pass |
| 93 | + |
| 94 | + |
| 95 | +def f8(a, b): |
| 96 | + pass |
| 97 | + |
| 98 | + |
| 99 | +def f9(a, b, *args): |
| 100 | + pass |
| 101 | + |
| 102 | + |
| 103 | +def f10(a, b, *args, **kw): |
| 104 | + pass |
| 105 | + |
| 106 | + |
| 107 | +def f11(a, b, **kw): |
| 108 | + pass |
| 109 | + |
| 110 | + |
| 111 | +def f12(a, b, foo=bar): |
| 112 | + pass |
| 113 | + |
| 114 | + |
| 115 | +def f13(a, b, foo=bar, *args): |
| 116 | + pass |
| 117 | + |
| 118 | + |
| 119 | +def f14(a, b, foo=bar, *args, **kw): |
| 120 | + pass |
| 121 | + |
| 122 | + |
| 123 | +def f15(a, b, foo=bar, **kw): |
| 124 | + pass |
| 125 | + |
| 126 | + |
| 127 | +def f16(*, a): |
| 128 | + pass |
| 129 | + |
| 130 | + |
| 131 | +def f17(*, a=5): |
| 132 | + pass |
| 133 | + |
| 134 | + |
| 135 | +def f18(*, a=5, b): |
| 136 | + pass |
| 137 | + |
| 138 | + |
| 139 | +def f19(*, a, b=5): |
| 140 | + pass |
| 141 | + |
| 142 | + |
| 143 | +def f20(*, a, b=5, **kwds): |
| 144 | + pass |
| 145 | + |
| 146 | + |
| 147 | +def f21(*args, a): |
| 148 | + pass |
| 149 | + |
| 150 | + |
| 151 | +def f22(*args, a=5): |
| 152 | + pass |
| 153 | + |
| 154 | + |
| 155 | +def f23(*args, a=5, b): |
| 156 | + pass |
| 157 | + |
| 158 | + |
| 159 | +def f24(*args, a, b=5): |
| 160 | + pass |
| 161 | + |
| 162 | + |
| 163 | +def f25(*args, a, b=5, **kwds): |
| 164 | + pass |
| 165 | + |
| 166 | + |
| 167 | +def assert_parses(call_expr): |
| 168 | + raised = False |
| 169 | + try: |
| 170 | + eval(call_expr) |
| 171 | + except: |
| 172 | + raised = True |
| 173 | + assert not raised |
| 174 | + |
| 175 | + |
| 176 | +def assert_call_raises(exception, call_expr): |
| 177 | + assert_raises(exception, eval, call_expr) |
| 178 | + |
| 179 | + |
| 180 | +def test_parse_args(): |
| 181 | + assert_parses("f1()") |
| 182 | + assert_parses("f1(1, 2, 3)") |
| 183 | + assert_call_raises(TypeError, "f1(a=1)") # TypeError: f1() got an unexpected keyword argument 'a' |
| 184 | + |
| 185 | + assert_parses("f2()") |
| 186 | + assert_parses("f2(1, 2, 3)") |
| 187 | + assert_parses("f2(1, 2, 3, a=1, b=2)") |
| 188 | + assert_parses("f2(a=1, b=2)") |
| 189 | + |
| 190 | + assert_parses("f3()") |
| 191 | + assert_call_raises(TypeError, "f3(1, 2, 3)") # TypeError: f3() takes 0 positional arguments but 3 were given |
| 192 | + assert_call_raises(TypeError, "f3(1, 2, 3, a=1, b=2)") # TypeError: f3() takes 0 positional arguments but 3 were given |
| 193 | + assert_parses("f3(a=1, b=2)") |
| 194 | + |
| 195 | + assert_parses("f4()") |
| 196 | + assert_parses("f4(10)") |
| 197 | + assert_parses("f4(foo=10)") |
| 198 | + assert_call_raises(TypeError, "f4(a=10)") # TypeError: f4() got an unexpected keyword argument 'a' |
| 199 | + |
| 200 | + assert_call_raises(SyntaxError, "f5(foo=1, 2, 3)") # SyntaxError: positional argument follows keyword argument |
| 201 | + assert_parses("f5(foo=1)") |
| 202 | + assert_call_raises(TypeError, "f5(a=1)") # TypeError: f5() got an unexpected keyword argument 'a' |
| 203 | + assert_parses("f5(1, 2, 3)") |
| 204 | + |
| 205 | + assert_parses("f6(foo=1)") |
| 206 | + assert_parses("f6(foo=1, a=2)") |
| 207 | + assert_call_raises(SyntaxError, "f6(foo=1, 3, 4, a=2)") # SyntaxError: positional argument follows keyword argument |
| 208 | + |
| 209 | + assert_call_raises(TypeError, "f7(1, 2)") # TypeError: f7() takes from 0 to 1 positional arguments but 2 were given |
| 210 | + assert_parses("f7(1)") |
| 211 | + assert_parses("f7(1, a=2)") |
| 212 | + assert_parses("f7(foo=1, a=2)") |
| 213 | + assert_parses("f7()") |
| 214 | + |
| 215 | + assert_call_raises(TypeError, "f8()") # TypeError: f8() missing 2 required positional arguments: 'a' and 'b' |
| 216 | + assert_call_raises(TypeError, "f8(1)") # TypeError: f8() missing 1 required positional argument: 'b' |
| 217 | + assert_parses("f8(1,2)") |
| 218 | + assert_call_raises(TypeError, "f8(1,2,3)") # TypeError: f8() takes 2 positional arguments but 3 were given |
| 219 | + |
| 220 | + assert_call_raises(TypeError, "f9()") # TypeError: f8() missing 2 required positional arguments: 'a' and 'b' |
| 221 | + assert_parses("f9(1,2,3)") |
| 222 | + |
| 223 | + assert_parses("f10(1,2,3,4,c=1)") |
| 224 | + assert_call_raises(TypeError, "f10(1,2,3,4,a=1)") # TypeError: f10() got multiple values for argument 'a' |
| 225 | + |
| 226 | + assert_parses("f11(a=1, b=2)") |
| 227 | + assert_parses("f11(a=1, b=2, c=3)") |
| 228 | + assert_call_raises(SyntaxError, "f11(a=1, b=2, a=3)") # SyntaxError: keyword argument repeated |
| 229 | + assert_call_raises(TypeError, "f11(1, b=2, a=3)") # TypeError: f11() got multiple values for argument 'a' |
| 230 | + |
| 231 | + assert_parses("f12(1,2)") |
| 232 | + assert_call_raises(TypeError, "f12(a=1)") # TypeError: f12() missing 1 required positional argument: 'b' |
| 233 | + assert_parses("f12(1,2,3)") |
| 234 | + assert_parses("f12(1,2,foo=3)") |
| 235 | + assert_parses("f12(a=1,b=2,foo=3)") |
| 236 | + assert_call_raises(SyntaxError, "f12(a=1,2,foo=3)") # SyntaxError: positional argument follows keyword argument |
| 237 | + |
| 238 | + assert_parses("f13(1,2,3,4)") |
| 239 | + assert_call_raises(TypeError, "f13(1, 2, foo=3, c=4)") # TypeError: f13() got an unexpected keyword argument 'c' |
| 240 | + |
| 241 | + assert_parses("f14(1, 2, foo=3, c=4)") |
| 242 | + assert_parses("f14(a=1, b=2, foo=3, c=4)") |
| 243 | + assert_parses("f14(a=1, b=2, foo=3)") |
| 244 | + assert_parses("f14(1, 2, 3, c=4)") |
| 245 | + assert_parses("f14(1, 2, 3, 4, 5, 6, 7, d=1)") |
| 246 | + assert_call_raises(TypeError, "f14(1, 2, 3, a=4)") # TypeError: f14() got multiple values for argument 'a' |
| 247 | + |
| 248 | + assert_parses("f15(1, 2, foo=3, c=4)") |
| 249 | + assert_parses("f15(a=1, b=2, foo=3, c=4)") |
| 250 | + assert_parses("f15(a=1, b=2, foo=3)") |
| 251 | + assert_parses("f15(1, 2, 3, c=4)") |
| 252 | + assert_call_raises(TypeError, "f15(1, 2, 3, 4, 5, 6, 7, d=1)") # TypeError: f15() takes from 2 to 3 positional arguments but 7 were given |
| 253 | + |
| 254 | + # keyword only args |
| 255 | + assert_call_raises(TypeError, "f16()") # TypeError: f16() missing 1 required keyword-only argument: 'a' |
| 256 | + assert_call_raises(TypeError, "f16(1)") # TypeError: f16() takes 0 positional arguments but 1 was given |
| 257 | + assert_parses("f16(a=1)") |
| 258 | + assert_call_raises(TypeError, "f16(a=1, b=1)") # TypeError: f16() got an unexpected keyword argument 'b' |
| 259 | + |
| 260 | + assert_parses("f17()") |
| 261 | + assert_parses("f17(a=1)") |
| 262 | + assert_call_raises(TypeError, "f17(b=1)") # TypeError: f17() got an unexpected keyword argument 'b' |
| 263 | + assert_call_raises(TypeError, "f17(1)") # TypeError: f17() takes 0 positional arguments but 1 was given |
| 264 | + |
| 265 | + assert_call_raises(TypeError, "f18(1,2)") # TypeError: f18() takes 0 positional arguments but 2 were given |
| 266 | + assert_call_raises(SyntaxError, "f18(a=1,2)") # SyntaxError: positional argument follows keyword argument |
| 267 | + assert_parses("f18(a=1,b=2)") |
| 268 | + assert_call_raises(TypeError, "f18(a=1,c=2)") # TypeError: f18() got an unexpected keyword argument 'c' |
| 269 | + assert_call_raises(TypeError, "f18(1,b=2)") # TypeError: f18() takes 0 positional arguments but 1 positional argument (and 1 keyword-only argument) were given |
| 270 | + |
| 271 | + assert_parses("f19(a=1)") |
| 272 | + assert_parses("f19(a=1, b=2)") |
| 273 | + assert_call_raises(TypeError, "f19(1, b=2)") # TypeError: f19() takes 0 positional arguments but 1 positional argument (and 1 keyword-only argument) were given |
| 274 | + assert_call_raises(TypeError, "f19(1)") # TypeError: f19() takes 0 positional arguments but 1 was given |
| 275 | + |
| 276 | + assert_parses("f20(a=1)") |
| 277 | + assert_parses("f20(a=1, b=2)") |
| 278 | + assert_parses("f20(a=1, b=2, c=3)") |
| 279 | + assert_call_raises(SyntaxError, "f20(a=1, b=2, a=3)") # SyntaxError: keyword argument repeated |
| 280 | + assert_call_raises(TypeError, "f20(1, b=2)") # TypeError: f20() takes 0 positional arguments but 1 positional argument (and 1 keyword-only argument) were given |
| 281 | + assert_call_raises(TypeError, "f20(1)") # TypeError: f20() takes 0 positional arguments but 1 was given |
| 282 | + |
| 283 | + assert_call_raises(TypeError, "f21(1,2,3)") # TypeError: f21() missing 1 required keyword-only argument: 'a' |
| 284 | + assert_parses("f21(1,2,a=3)") |
| 285 | + assert_parses("f21(a=3)") |
| 286 | + |
| 287 | + assert_parses("f22()") |
| 288 | + assert_parses("f22(a=3)") |
| 289 | + assert_parses("f22(1,2,a=3)") |
| 290 | + assert_call_raises(TypeError, "f22(a=2, b=3)") # TypeError: f22() got an unexpected keyword argument 'b' |
| 291 | + |
| 292 | + assert_call_raises(TypeError, "f23(1,2,3)") # TypeError: f23() missing 1 required keyword-only argument: 'b' |
| 293 | + assert_parses("f23(1,2,3,b=4)") |
| 294 | + assert_parses("f23(1,2,a=3,b=4)") |
| 295 | + assert_parses("f23(1,2,b=4,a=3)") |
| 296 | + |
| 297 | + assert_call_raises(TypeError, "f24(1,2,3)") # TypeError: f24() missing 1 required keyword-only argument: 'a' |
| 298 | + assert_parses("f24(1,2,a=3)") |
| 299 | + assert_parses("f24(1,2,a=3,b=4)") |
| 300 | + assert_call_raises(TypeError, "f24(1,2, a=3, b=4, c=5)") # TypeError: f24() got an unexpected keyword argument 'c' |
| 301 | + assert_parses("f24(a=1)") |
| 302 | + assert_call_raises(TypeError, "f24(1)") # TypeError: f24() missing 1 required keyword-only argument: 'a' |
| 303 | + assert_parses("f24(a=1, b=2)") |
| 304 | + assert_call_raises(SyntaxError, "f24(a=1, 2)") # SyntaxError: positional argument follows keyword argument |
| 305 | + |
| 306 | + assert_call_raises(TypeError, "f25()") # TypeError: f25() missing 1 required keyword-only argument: 'a' |
| 307 | + assert_call_raises(TypeError, "f25(1,2,3)") # TypeError: f25() missing 1 required keyword-only argument: 'a' |
| 308 | + assert_parses("f25(1,2,3,a=4)") |
| 309 | + assert_parses("f25(1,2,3,a=4,b=5)") |
| 310 | + assert_call_raises(SyntaxError, "f25(1,2,3,a=4,5)") # SyntaxError: positional argument follows keyword argument |
| 311 | + assert_parses("f25(1,2,3,a=4,b=5,c=6)") |
| 312 | + assert_parses("f25(1,2,3,a=4,c=6)") |
| 313 | + assert_call_raises(TypeError, "f25(1,2,3,c=6)") # TypeError: f25() missing 1 required keyword-only argument: 'a' |
| 314 | + assert_parses("f25(a=4,c=6)") |
| 315 | + assert_parses("f25(a=4)") |
0 commit comments