tapioca icon indicating copy to clipboard operation
tapioca copied to clipboard

Generated relation collections don't have types for each and find_each

Open saty9 opened this issue 3 years ago • 1 comments

Tapioca version: 0.7.3

has_many :session_participations

def example
      T.reveal_type(session_participations)
      session_participations.each do |sp|
        T.reveal_type(sp)
      end
end

When typechecking this code where session_participations are a relation of the model this code is in. sp comes through as untyped.

Revealed type: Sessions::SessionParticipation::PrivateCollectionProxy https://srb.help/7014
     150 |      T.reveal_type(session_participations)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  Got Sessions::SessionParticipation::PrivateCollectionProxy originating from:
     150 |      T.reveal_type(session_participations)
                              ^^^^^^^^^^^^^^^^^^^^^^

Revealed type:  T.untyped https://srb.help/7014
     152 |        T.reveal_type(sp)
                  ^^^^^^^^^^^^^^^^^
  Got  T.untyped originating from:
     151 |      session_participations.each do |sp|

However if I use a map I do get the correct type for sp.

It would be good if the typing of blocks passed to map, each, and find_each was consistent.

saty9 avatar Nov 24 '22 14:11 saty9

Copy-paste from the Sorbet Slack: tapioca gem generates these methods like these for the delegators defined here:

# source://activerecord//lib/active_record/relation/delegation.rb#7
module ActiveRecord::Delegation
  extend ::ActiveSupport::Concern

  mixes_in_class_methods ::ActiveRecord::Delegation::ClassMethods

[omitted]

  # source://activerecord//lib/active_record/relation/delegation.rb#88
  def each(*_arg0, **_arg1, &_arg2); end

Removing them makes types work for each, but that change would be blown away on the next tapioca gem invocation.

bdewater avatar Feb 10 '23 22:02 bdewater