Skip to content

Improve interface check by method parameters - #1698

Merged
soutaro merged 4 commits into
ruby:masterfrom
ksss:rbs-test-interface
Jan 9, 2024
Merged

soutaro merged 4 commits into
ruby:masterfrom
ksss:rbs-test-interface

Conversation

@ksss

@ksss ksss commented Dec 25, 2023

Copy link
Copy Markdown
Collaborator

Regarding checking interfaces, in addition to checking with #respond_to?, I believe that comparing Method#parameters with types can provide a more accurate interface check.

As an implementation, I've made it so that a match only occurs if the method has exactly the same arguments as the type notation.
Does this match with the philosophy of RBS?
For example, the following code actually works.

# sample.rbs
class Sample
  def foo: (a: Integer, b: String) -> void
end
# sample.rb
class Sample
  def foo(a = {})
    p a # { :a => 1, :b => 'b' }
  end
end

Sample.new.foo(a: 1, b: 'b')

However, for instance, steep would report Ruby::MethodArityMismatch and Ruby::DifferentMethodParameterKind.
What kind of checks should be attempted with RBS alone?

@soutaro soutaro added this to the RBS 3.5 milestone Dec 26, 2023
@soutaro

soutaro commented Dec 26, 2023

Copy link
Copy Markdown
Member

I think the parameter checking should be permissive. Having an optional argument in implementation while required argument in RBS should be allowed for example.

@soutaro

soutaro commented Dec 26, 2023

Copy link
Copy Markdown
Member

Steep validates the method implementation and reports DifferentMethodParameterKind. But I don't think RBS should do it on the implementation side.

@ksss

ksss commented Dec 26, 2023

Copy link
Copy Markdown
Collaborator Author

Thank you!
I think it seems good to use the criterion "if it can be called, it's OK"

@ksss
ksss force-pushed the rbs-test-interface branch from b7c205e to a9cd4d0 Compare January 5, 2024 13:42
@ksss

ksss commented Jan 5, 2024

Copy link
Copy Markdown
Collaborator Author

I have reimplemented code to allow for many method call patterns.
I also added an ArgumentChecker to generate arguments from RBS and test whether a method can actually be called.

Comment thread test/rbs/test/type_check_test.rb Resolved

@soutaro soutaro left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't review the implementation very carefully, but give it a try! 💪

@soutaro
soutaro added this pull request to the merge queue Jan 9, 2024
Merged via the queue into ruby:master with commit 5142125 Jan 9, 2024
@ksss
ksss deleted the rbs-test-interface branch January 9, 2024 05:33
@soutaro soutaro added the Released PRs already included in the released version label Jun 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Released PRs already included in the released version

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants