mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-29 19:24:57 +08:00
* Reject unsupported native keywords before argument binding Add explicit keyword dispatch metadata independently of inferred Rust calling flags. Reject nonempty keywords before binding and conversion, while preserving descriptor self checks and keyword-capable calls. Annotate 31 builtins and list.append/list.clear, derive C-API policies from ml_flags, and group dynamic registration metadata in MethodDefSpec. Add regression coverage and remove the id doctest expected-failure marker. Validation: workspace and C-API tests/clippy, VM without doc, keyword regressions, and test_call/test_extcall/test_builtin/test_list passed. The full snippet suite had 475 passes and 7 failures: six CPython-side expectation mismatches and one missing optional SQLite module. Assisted-by: Codex:GPT-6 * Infer native keyword dispatch from argument metadata Replace no_keywords annotations with registration-time inference from FromArgs::PARAMS, including nested parameter metadata. Forward arguments with keyword-capable parameters to the binder, preserving min/max body validation and descriptor self checks. Add regressions for raw arguments, keyword argument structs, and list.sort. Tested: C-API tests, workspace and C-API Clippy, VM without doc, both keyword regression snippets, and test_call/test_extcall/test_builtin/test_list. Workspace tests reported eight _opcode snapshot failures. The full snippet suite reported 477 passes and seven failures: six CPython-side failures and one RustPython failure due to the missing optional SQLite module. Assisted-by: Codex:GPT-6 * Simplify native function registration metadata Reuse SimpleItemMeta for pyfunction attributes and remove FunctionItemMeta. Remove MethodDefSpec and restore the original new_method_def arguments with an additional keyword_dispatch parameter, updating its callers. Consolidate keyword dispatch regressions in syntax_function_args.py, preserving their assertions and placing them before builtin shadowing. Validation: workspace and C-API tests/clippy, VM without doc, the combined snippet, and test_extcall/test_builtin/test_list/test_call passed. The full snippet suite reported 477 passes and seven existing failures: six CPython-side failures and one missing optional SQLite module. Assisted-by: Codex:GPT-6 * Reject native keywords by calling convention flags Remove `KeywordDispatch`, the `keyword_dispatch` field of `PyMethodDef`, `PyMethodDef::with_keyword_dispatch`, `function::keyword_dispatch` and the derive macros' `keyword_dispatch_tokens`. Native functions and method descriptors reject nonempty keywords when `flags` lacks `KEYWORDS`, and report the unqualified name when `flags` contains `VARARGS`. `Context::new_method_def` takes `(name, f, flags, doc)` again. Add a snippet for `int.__new__` called with a keyword argument. Assisted-by: Claude Code:claude-opus-5-5 --------- Co-authored-by: Jeong YunWon <jeong@youknowone.org>
216 lines
5.1 KiB
Python
216 lines
5.1 KiB
Python
from testutils import assert_raises
|
|
|
|
|
|
def error(call, expected):
|
|
try:
|
|
call()
|
|
except TypeError as exc:
|
|
assert str(exc) == expected, (str(exc), expected)
|
|
else:
|
|
raise AssertionError("expected TypeError")
|
|
|
|
|
|
error(lambda: id(obj=1), "id() takes no keyword arguments")
|
|
error(lambda: id(**{"obj": 1}), "id() takes no keyword arguments")
|
|
error(lambda: id(1, 2, obj=1), "id() takes no keyword arguments")
|
|
error(lambda: list.append(x=1), "unbound method list.append() needs an argument")
|
|
error(lambda: list.append(**{"x": 1}), "unbound method list.append() needs an argument")
|
|
error(
|
|
lambda: list.append(1, x=1),
|
|
"descriptor 'append' for 'list' objects doesn't apply to a 'int' object",
|
|
)
|
|
error(lambda: list.append([], x=1), "list.append() takes no keyword arguments")
|
|
error(lambda: list.append([], **{"x": 1}), "list.append() takes no keyword arguments")
|
|
error(lambda: [].append(x=1), "list.append() takes no keyword arguments")
|
|
error(lambda: [].clear(1, x=1), "list.clear() takes no keyword arguments")
|
|
|
|
|
|
class Index:
|
|
def __index__(self):
|
|
raise AssertionError("conversion ran before keyword check")
|
|
|
|
|
|
error(lambda: bin(Index(), x=1), "bin() takes no keyword arguments")
|
|
assert id(1, **{}) == id(1)
|
|
assert round(1.25, ndigits=1) == 1.2
|
|
assert pow(2, exp=3) == 8
|
|
assert sorted([2, 1], reverse=True) == [2, 1]
|
|
assert sum([1, 2], start=4) == 7
|
|
values = []
|
|
list.append(values, 1)
|
|
values.append(2)
|
|
assert values == [1, 2]
|
|
list.clear(values)
|
|
assert values == []
|
|
|
|
# Keyword metadata must forward raw arguments without changing body validation.
|
|
for choose in (min, max):
|
|
assert choose([], default=42) == 42
|
|
assert choose([1, 2], key=lambda value: -value) == (2 if choose is min else 1)
|
|
try:
|
|
choose(bogus=1)
|
|
except TypeError as exc:
|
|
assert str(exc) == f"{choose.__name__} expected at least 1 argument, got 0"
|
|
else:
|
|
raise AssertionError("missing positional argument was accepted")
|
|
try:
|
|
choose(1, 2, default=0)
|
|
except TypeError as exc:
|
|
assert str(exc) == (
|
|
f"Cannot specify a default for {choose.__name__}() "
|
|
"with multiple positional arguments"
|
|
)
|
|
else:
|
|
raise AssertionError("default with multiple arguments was accepted")
|
|
|
|
# Named fields in argument structs must override positional-only arguments.
|
|
assert eval("value", globals={"value": 42}) == 42
|
|
namespace = {}
|
|
exec("value = 42", globals=namespace)
|
|
assert namespace["value"] == 42
|
|
assert compile(source="42", filename="<inference>", mode="eval")
|
|
assert __import__(name="sys").__name__ == "sys"
|
|
|
|
from io import StringIO
|
|
|
|
output = StringIO()
|
|
print(1, 2, sep=":", end="!", file=output)
|
|
assert output.getvalue() == "1:2!"
|
|
|
|
# Methods using a keyword-only argument struct must keep accepting keywords.
|
|
values = [1, 2]
|
|
values.sort(reverse=True)
|
|
assert values == [2, 1]
|
|
|
|
# Definitions built without the macros infer their calling convention too.
|
|
assert int.__new__(int, "11", base=2) == 3
|
|
|
|
def sum(x, y):
|
|
return x+y
|
|
|
|
def total(a, b, c, d):
|
|
return sum(sum(a,b), sum(c,d))
|
|
|
|
|
|
assert total(1,1,1,1) == 4
|
|
assert total(1,2,3,4) == 10
|
|
|
|
assert sum(1, 1) == 2
|
|
assert sum(1, 3) == 4
|
|
|
|
|
|
def sum2y(x, y):
|
|
return x+y*2
|
|
|
|
|
|
assert sum2y(1, 1) == 3
|
|
assert sum2y(1, 3) == 7
|
|
|
|
|
|
def va(a, b=2, *c, d, **e):
|
|
assert a == 1
|
|
assert b == 22
|
|
assert c == (3, 4)
|
|
assert d == 1337
|
|
assert e['f'] == 42
|
|
|
|
|
|
va(1, 22, 3, 4, d=1337, f=42)
|
|
|
|
assert va.__defaults__ == (2,)
|
|
assert va.__kwdefaults__ is None
|
|
|
|
|
|
def va2(*args, **kwargs):
|
|
assert args == (5, 4)
|
|
assert len(kwargs) == 0
|
|
|
|
va2(5, 4)
|
|
x = (5, 4)
|
|
va2(*x)
|
|
|
|
va2(5, *x[1:])
|
|
|
|
|
|
def va3(x, *, a, b=2, c=9):
|
|
return x + b + c
|
|
|
|
|
|
assert va3(1, a=1, b=10) == 20
|
|
|
|
with assert_raises(TypeError):
|
|
va3(1, 2, 3, a=1, b=10)
|
|
|
|
with assert_raises(TypeError):
|
|
va3(1, b=10)
|
|
|
|
|
|
assert va3.__defaults__ is None
|
|
kw_defaults = va3.__kwdefaults__
|
|
# assert va3.__kwdefaults__ == {'b': 2, 'c': 9}
|
|
assert set(kw_defaults) == {'b', 'c'}
|
|
assert kw_defaults['b'] == 2
|
|
assert kw_defaults['c'] == 9
|
|
|
|
x = {'f': 42, 'e': 1337}
|
|
y = {'d': 1337}
|
|
va(1, 22, 3, 4, **x, **y)
|
|
|
|
# star arg after keyword args:
|
|
def fubar(x, y, obj=None):
|
|
assert x == 4
|
|
assert y == 5
|
|
assert obj == 6
|
|
|
|
rest = [4, 5]
|
|
fubar(obj=6, *rest)
|
|
|
|
|
|
# https://www.python.org/dev/peps/pep-0468/
|
|
def func(**kwargs):
|
|
return list(kwargs.items())
|
|
|
|
empty_kwargs = func()
|
|
assert empty_kwargs == []
|
|
|
|
kwargs = func(a=1, b=2)
|
|
assert kwargs == [('a', 1), ('b', 2)]
|
|
|
|
kwargs = func(a=1, b=2, c=3)
|
|
assert kwargs == [('a', 1), ('b', 2), ('c', 3)]
|
|
|
|
|
|
def inc(n):
|
|
return n + 1
|
|
|
|
with assert_raises(SyntaxError):
|
|
exec("inc(n=1, n=2)")
|
|
|
|
with assert_raises(SyntaxError):
|
|
exec("def f(a=1, b): pass")
|
|
|
|
|
|
def f(a):
|
|
pass
|
|
|
|
x = {'a': 1}
|
|
y = {'a': 2}
|
|
with assert_raises(TypeError):
|
|
f(**x, **y)
|
|
|
|
|
|
def f(a, b, /, c, d, *, e, f):
|
|
return a + b + c + d + e + f
|
|
|
|
assert f(1,2,3,4,e=5,f=6) == 21
|
|
assert f(1,2,3,d=4,e=5,f=6) == 21
|
|
assert f(1,2,c=3,d=4,e=5,f=6) == 21
|
|
with assert_raises(TypeError):
|
|
f(1,b=2,c=3,d=4,e=5,f=6)
|
|
with assert_raises(TypeError):
|
|
f(a=1,b=2,c=3,d=4,e=5,f=6)
|
|
with assert_raises(TypeError):
|
|
f(1,2,3,4,5,f=6)
|
|
with assert_raises(TypeError):
|
|
f(1,2,3,4,5,6)
|