Skip to content

Keyword argument handling on Ruby 2.7 #116

Open

Description

@mrkn

We can remove the last empty Hash in the following case in Ruby 2.7 because we can distinguish arguments of Hash and keyword arguments in Ruby 2.7.

>> s4 = Pandas::Series.new({c: 100, b: 200, a: 300}, {})
=>
c    100
b    200
a    300
dtype: int64

>> Pandas::Series.new({c: 100, b: 200, a: 300})
Traceback (most recent call last):
        6: from /Users/mrkn/.rbenv/versions/2.7/bin/irb:23:in `<main>'
        5: from /Users/mrkn/.rbenv/versions/2.7/bin/irb:23:in `load'
        4: from /Users/mrkn/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/irb-1.2.3/exe/irb:11:in `<top (required)>'
        3: from (irb):28
        2: from /Users/mrkn/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/pycall-1.3.0/lib/pycall/pytypeobject_wrapper.rb:24:in `new'
        1: from /Users/mrkn/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/pycall-1.3.0/lib/pycall/pytypeobject_wrapper.rb:24:in `call_object'
PyCall::PyError (<class 'TypeError'>: __init__() got an unexpected keyword argument 'c')

Activity

  1. self-assigned this
    on Apr 22, 2020
  2. Mark24Code commented on Apr 21, 2023

    @Mark24Code

    Same problem +1

  3. mrkn commented on Aug 25, 2023

    @mrkn
    MemberAuthor

    We can drop Ruby 2.7 support now.

  4. himotoyoshi commented on Nov 17, 2026

    @himotoyoshi

    Since the next release will be 2.0.0 (#238), would this be a good time to do this?

    The idea is to use rb_keyword_given_p() instead of the type of the last argument. Only Ruby keyword arguments would become Python keyword arguments, and a Hash in the positional arguments would be passed as a dict:

    Pandas::Series.new({c: 100, b: 200, a: 300})  # a dict; no trailing {} needed
    f.(a: 1)                                      # keyword arguments, as today
    f.(**opts)                                    # keyword arguments, as today

    It would need Ruby 2.7 or later, and the Ruby-side methods that forward *args to the C side (PyObjectWrapper#method_missing, #call, PyTypeObjectWrapper#new, and the methods PyCall::Import defines) would need ruby2_keywords. Code that passes f({a: 1}) or f(opts) meaning keyword arguments would need f(a: 1) or f(**opts), the same migration as Ruby 3.

  5. kou commented on Nov 17, 2026

    @kou
    Member

    We don't need to use ruby2_keywords because we drop support for Ruby 2.7.

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions