Shopify/tapioca

RBI: getter methods on belongs_to associations have returns(T.nilable(...)) as sig but belongs_to reflection is not optional

Opened this issue · 1 comments

I noticed the returns signature of any belongs_to association on a model is always nilable in the RBI file, even if the belongs_to is not optional. Rails will raise an error if a nil value is present for such an association. I don't find it optimal to add T.must everywhere to change this in application code.

I changed Tapioca::Dsl::Compilers::ActiveRecordAssociations#populate_single_assoc_getter_setter and it seems to work:

association_type = if !reflection.options[:optional] && reflection.is_a?(ActiveRecord::Reflection::BelongsToReflection)
            association_class
          else
            as_nilable_type(association_class)
          end

What do you think ?

I have a PR open (and approved) to address this issue: #1993