Skip to content

Pass a positional Hash as a dict; take keyword arguments only from Ruby keywords (2.0.0) #239

Closed

Description

@himotoyoshi

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

  1. f({a: 1}) written to mean keyword arguments: write f(a: 1).
  2. f(opts) with an options Hash meant as keyword arguments: write f(**opts). This is the same migration Ruby 3 asked for.
  3. Code that forwards arguments to PyCall with a bare *args (other gems, user wrappers) would silently turn keyword arguments into a dict, as *args drops the keyword flag. Such code needs ruby2_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

  1. 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.
  2. The Ruby-side forwarders keep the keyword flag. PyObjectWrapper#method_missing(name, *args), PyObjectWrapper#call(*args), PyTypeObjectWrapper#new(*args) and the methods defined by PyCall::Import take a bare *args and pass it to the C side. They work today because the C side treats the last Hash as keyword arguments. They need ruby2_keywords (or *args, **kw).

Questions

Activity

  1. himotoyoshi commented on Nov 17, 2026

    @himotoyoshi
    Author

    I'm moving this to #116, where it was first proposed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions