Shopify / Shopify/tapioca

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

Open
#1,281 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
873
Forks
164
Avg merge
4d 27m
Merged PRs (30d)
9

Description

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.

Contributor guide

No contributing guide indexed for this repository

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

Start by tracing Tapioca's generated relation collection for has_many and compare block typing for map, each, and find_each. Reproduce the Sorbet example with T.reveal_type and verify that each and find_each infer SessionParticipation rather than T.untyped, while map remains consistent.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.