ruby / ruby/rbs

Type error with OptionParser#on method

Open
#727 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Ruby
Stars
2.2k
Forks
256
Avg merge
6d 17h
Merged PRs (30d)
37

Description

Hi, thank you for providing the optparse types!

But, when I try the types with Steep, I get the following error: 😢

a.rb:6:2: [error] Cannot find compatible overloading of method `on` of type `::OptionParser`
│ Method types:
│   def on: (*::String) ?{ (*untyped) -> untyped } -> ::OptionParser
│         | (::String, (::Class | ::Array[::String] | ::Hash[::Symbol, untyped] | ::Regexp), ?::String) ?{ (*untyped) -> untyped } -> ::OptionParser
│         | (*::String) -> ::OptionParser
│         | (::String, (::Class | ::Array[::String] | ::Hash[::Symbol, untyped] | ::Regexp), ?::String) -> ::OptionParser
│
│ Diagnostic ID: Ruby::UnresolvedOverloading
│
└   opts.on("--name=NAME", "This is a name.", "Other explanations.", ["foo", "bar", "baz"]) do |name|
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Detected 1 problem from 1 file

This error occurs on the use of the OptionParser#on method. The next section describes more detailed reproduction.

Reproduction

Gemfile:

source "https://rubygems.org"
gem "rbs", "1.3.3"
gem "steep", "0.44.1"

Steepfile:

target :src do
  check "*.rb"
  library "optparse"
end

Ruby code:

require "optparse"

names = []

OptionParser.new do |opts|
  opts.on("--name=NAME", "This is a name.", "Other explanations.", ["foo", "bar", "baz"]) do |name|
    names << name
  end
end.parse!

p names

Run Steep:

$ bundle exec steep check
# Type checking files:

.........................................................F

a.rb:6:2: [error] Cannot find compatible overloading of method `on` of type `::OptionParser`
│ Method types:
│   def on: (*::String) ?{ (*untyped) -> untyped } -> ::OptionParser
│         | (::String, (::Class | ::Array[::String] | ::Hash[::Symbol, untyped] | ::Regexp), ?::String) ?{ (*untyped) -> untyped } -> ::OptionParser
│         | (*::String) -> ::OptionParser
│         | (::String, (::Class | ::Array[::String] | ::Hash[::Symbol, untyped] | ::Regexp), ?::String) -> ::OptionParser
│
│ Diagnostic ID: Ruby::UnresolvedOverloading
│
└   opts.on("--name=NAME", "This is a name.", "Other explanations.", ["foo", "bar", "baz"]) do |name|
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Detected 1 problem from 1 file

Run the Ruby code:

$ bundle exec ruby a.rb -h
Usage: a [options]
        --name=NAME                  This is a name.
                                     Other explanations.

$ bundle exec ruby a.rb --name=foo --name=bar --name=unknown
a.rb:9:in `<main>': invalid argument: --name=unknown (OptionParser::InvalidArgument)

$ bundle exec ruby a.rb --name=foo --name=bar
["foo", "bar"]
Environment
  • ruby: 3.0.2p107 (2021-07-07 revision 0db68f0233) [x86_64-darwin20]
  • rbs: 1.3.3
  • steep: 0.44.1

I’m glad to provide you with more information if needed. Thank you. 😊

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure with the shown Gemfile, Steepfile, and Ruby example, then inspect the optparse type definitions for the OptionParser#on overloads. Compare the accepted arguments with this array-valued option and block; done means the example passes steep check without the unresolved-overloading error.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.