Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default positional arguments before required positional arguments throws "not enough arguments" error in callers #7873

Open
rstoutamore opened this issue May 6, 2024 · 1 comment
Labels
bug Something isn't working unconfirmed This issue has not yet been confirmed by the Sorbet team

Comments

@rstoutamore
Copy link

Input

→ View on sorbet.run

# typed: true
def function_with_default_arg_first(a='foo', b)
  puts "a: #{a}, b: #{b}"
end

function_with_default_arg_first('bar')

Observed output

editor.rb:6: Not enough arguments provided for method Object#function_with_default_arg_first. Expected: 1..2, got: 1 https://srb.help/7004
     6 |function_with_default_arg_first('bar')
                                        ^^^^^
    editor.rb:2: Object#function_with_default_arg_first defined here
     2 |def function_with_default_arg_first(a='foo', b)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Errors: 1

Expected behavior

Type checking should pass


The example is weird code (default positional args should be last), but it's technically valid Ruby (Ruby docs even include a similar example). I ran into this error when adding typing to code that calls a method in a third-party library with a default nil optional argument before required arguments.

The error presented here made investigating particularly tricky, as Expected: 1..2, got: 1 made it seem like it shouldn't be an error in the first place, as 1 satisfies 1..2.

@rstoutamore rstoutamore added bug Something isn't working unconfirmed This issue has not yet been confirmed by the Sorbet team labels May 6, 2024
@bitwise-aiden
Copy link
Contributor

Just wanted to plus 1 this with an example. ActiveRecord's #update method has the signature of:

def update(id = :all, attributes); end

(source)

This is often used with #where so the only arguments passed to the #update are the kwargs for attributes. Trying to type this will result in any of those valid calls being raised as errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working unconfirmed This issue has not yet been confirmed by the Sorbet team
Projects
None yet
Development

No branches or pull requests

2 participants