ruby / ruby/rbs

Class.new losing type information on var

Open
#701 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

There's a failure I'm observing under steep, when using Class.new, similar to this bit:

class A
  def self.options; {} ; end

  def pl
    puts "pl"
  end
end

module Rando
  def pl
      klass = is_a?(A) ? self.class : A
      klass = Class.new(klass)
      # steep complains with:
      # Cannot pass a value of type `(singleton(::Rando) | singleton(::A))` as an argument of type `::Class`
      # (singleton(::Rando) | singleton(::A)) <: ::Class
      #   singleton(::Rando) <: ::Class
      #    ::Module <: ::Class
      #    ::Object <: ::Class
      #    ::BasicObject <: ::Class
      klass.instance_variable_set(:@options, klass.options)
      # steep complains again:
      #
      # Type `::Class` does not have method `options`
      klass.plugin(pl, options, &blk).new
      # Type `::Class` does not have method `pl`
  end

Seems that this type of meta-programming makes it lose type information, although it should be clear, i.e. Class.new should inherit superclass type information.

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 Ruby snippet in the issue under Steep, then trace how Class.new is typed and how the resulting class is checked. Done means the superclass type information is retained so the reported Class, options, and pl calls no longer produce type errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.