Repository navigation
Description
Summary
Since the next release is going to be 2.0.0 (#238), I'd like to propose one more backward-incompatible change for it: only what is passed as Ruby keyword arguments becomes Python keyword arguments. A Hash in the positional arguments is passed as a dict, even when it is the last one.
This is what #116 proposed: since Ruby 2.7 a Hash argument and keyword arguments can be told apart, so the trailing empty Hash in Pandas::Series.new({c: 100, b: 200, a: 300}, {}) is no longer needed. A major version seems the right time to do it.
Problem
Today a Hash in the last position is always taken as keyword arguments (pycall_call_python_callable, argc - RB_TYPE_P(argv[argc - 1], T_HASH)). So a dict cannot be passed as the last positional argument, which is a very common pattern in Python APIs:
json = PyCall.import_module('json')
json.dumps({"a" => 1})
# => PyCall::PyError: <class 'TypeError'>: dumps() missing 1 required positional argument: 'obj'The same happens with pd.DataFrame({...}), pd.Series.new({...}) (#116), xr.Dataset({...}), dict.update({...}), and so on. The workaround is a trailing empty Hash, f(h, {}).
Current behavior, with f = PyCall.eval('lambda *a, **k: (a, k)') (pycall master, 267b450):
| Ruby | Python today |
|---|---|
f.(a: 1) |
f(a=1) |
f.(**h) |
f(a=1) |
f.({"a" => 1}) |
f(a=1) |
f.(h) |
f(a=1) |
f.(h, k: 1) |
f({'a': 1}, k=1) |
f.(h, {}) |
f({'a': 1}) |
Proposal
Use Ruby's own keyword flag, rb_keyword_given_p(), instead of the type of the last argument:
- If the call passed keyword arguments, the last Hash is the keyword arguments.
- Otherwise every argument is positional, and a Hash is converted to a dict.
| Ruby | Python in 2.0 |
|---|---|
f.(a: 1) |
f(a=1) (unchanged) |
f.(**h) |
f(a=1) (unchanged) |
f.(**{}) |
f() |
f.({"a" => 1}) |
f({'a': 1}) |
f.(h) |
f({'a': 1}) |
f.(h, k: 1) |
f({'a': 1}, k=1) (unchanged) |
f.("a" => 1) |
f(a=1) (unchanged; without braces Ruby passes keywords, also with String keys) |
f.(1 => 2) |
TypeError (as f(**{1: 2}) in Python) |
The rule is the same for method calls (obj.meth(...)), obj.(...), Klass.new(...) and names defined by pyimport/pyfrom. Return values are not affected.
This follows the keyword argument separation of Ruby 3: a braced Hash literal or a Hash variable is a positional argument, and ** makes keywords.
Incompatibilities and migration
f({a: 1})written to mean keyword arguments: writef(a: 1).f(opts)with an options Hash meant as keyword arguments: writef(**opts). This is the same migration Ruby 3 asked for.- Code that forwards arguments to PyCall with a bare
*args(other gems, user wrappers) would silently turn keyword arguments into a dict, as*argsdrops the keyword flag. Such code needsruby2_keywords,(*args, **kw)or(...). This is the subtle one, since it gives a different result rather than an error, so it would be worth noting in the change log.
Most existing code uses the f(a: 1) form and is not affected.
What the implementation needs
- Ruby >= 2.7.
rb_keyword_given_p()exists since 2.7. Keeping the old heuristic for older Rubies would make the behavior depend on the Ruby version, so this would mean requiring Ruby 2.7 or later in 2.0. - The Ruby-side forwarders keep the keyword flag.
PyObjectWrapper#method_missing(name, *args),PyObjectWrapper#call(*args),PyTypeObjectWrapper#new(*args)and the methods defined byPyCall::Importtake a bare*argsand pass it to the C side. They work today because the C side treats the last Hash as keyword arguments. They needruby2_keywords(or*args, **kw).
Questions
- Is it acceptable for 2.0.0 to require Ruby 2.7 or later?
- Should this go into 2.0.0 together with Return non-routine callable attributes when called without arguments #238?
Activity
himotoyoshi commentedon Nov 17, 2026
I'm moving this to #116, where it was first proposed.