Skip to content

Commit cec4776

Browse files
committed
Allow passing var kwarg with the same name as posonly arg
1 parent 8c751f6 commit cec4776

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/grammar/PositionalOnlyArgTests.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@
4040
*/
4141
package com.oracle.graal.python.test.grammar;
4242

43-
import com.oracle.graal.python.test.PythonTests;
4443
import org.junit.Test;
4544

45+
import com.oracle.graal.python.test.PythonTests;
46+
4647
public class PositionalOnlyArgTests {
4748

4849
@Test
@@ -245,4 +246,12 @@ public void lambda04() {
245246
"print(x(1, 2))";
246247
PythonTests.assertPrints("3\n", source);
247248
}
249+
250+
@Test
251+
public void postionalOnlyOverlapsWithKwargs() {
252+
String source = "def f(a, b, /, **kwargs):" +
253+
" print(a, b, kwargs.get('a'))\n" +
254+
"f(1, 2, a=3)";
255+
PythonTests.assertPrints("1 2 3\n", source);
256+
}
248257
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/argument/CreateArgumentsNode.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -491,12 +491,17 @@ Object[] applyCached(Object callee, @SuppressWarnings("unused") Signature callee
491491

492492
if (kwIdx != -1) {
493493
if (positionalOnlyArgIndex > -1 && kwIdx < positionalOnlyArgIndex) {
494-
throw raise.raise(PythonBuiltinClassType.TypeError, "%s() got some positional-only arguments passed as keyword arguments: '%s'", CreateArgumentsNode.getName(callee), name);
494+
if (unusedKeywords != null) {
495+
unusedKeywords[k++] = kwArg;
496+
} else {
497+
throw raise.raise(PythonBuiltinClassType.TypeError, "%s() got some positional-only arguments passed as keyword arguments: '%s'", CreateArgumentsNode.getName(callee), name);
498+
}
499+
} else {
500+
if (PArguments.getArgument(arguments, kwIdx) != null) {
501+
throw raise.raise(PythonBuiltinClassType.TypeError, "%s() got multiple values for argument '%s'", CreateArgumentsNode.getName(callee), name);
502+
}
503+
PArguments.setArgument(arguments, kwIdx, kwArg.getValue());
495504
}
496-
if (PArguments.getArgument(arguments, kwIdx) != null) {
497-
throw raise.raise(PythonBuiltinClassType.TypeError, "%s() got multiple values for argument '%s'", CreateArgumentsNode.getName(callee), name);
498-
}
499-
PArguments.setArgument(arguments, kwIdx, kwArg.getValue());
500505
} else if (unusedKeywords != null) {
501506
unusedKeywords[k++] = kwArg;
502507
} else {

0 commit comments

Comments
 (0)