Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
thierry-tranchant opened this issue Sep 6, 2024 · 1 comment

Comments

@thierry-tranchant
Copy link

thierry-tranchant commented Sep 6, 2024

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 ?

@thierry-tranchant thierry-tranchant changed the title RBI: getter methods on belongs_to associations have returns(T.nil(...)) as sig but belongs_to reflection is not optional RBI: getter methods on belongs_to associations have returns(T.nilable(...)) as sig but belongs_to reflection is not optional Sep 6, 2024
@stathis-alexander
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants