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

Add annotation for ActiveRecord::Relation#each #290

Merged
merged 1 commit into from
Oct 15, 2024

Conversation

marknuzz
Copy link
Contributor

@marknuzz marknuzz commented Oct 15, 2024

Closes Shopify/tapioca#2034

The generic type_member was necessary to prevent the following error:

type_member type Enumerable::Elem used outside of the class definition https://srb.help/5072

Using anything other than { { fixed: T.untyped } } would cause breakages that would need to be addressed separately.

For example: Using { { upper: ActiveRecord::Base } } doesn't work as the rails :has_and_belongs_to_many leads to Tapioca rbi output that don't inherit from anything, even though the classes should inherit from ActiveRecord::Base. Additionally, there may be user code that references ActiveRecord::Relation, which would break unless it was changed to something like ActiveRecord::Relation[ActiveRecord::Base] (and the class would need to be patched to extend T::Generic, which seems unacceptable).

Type of Change

  • Add RBI for a new gem
  • Modify RBI for an existing gem
  • Other:

Changes

  • Gem name: activerecord
  • Gem version: 7.1.3.4
  • Tapioca version: 0.16.2
  • Sorbet version: 0.5.11602

The generic type_member was necessary for type tests to pass.

Using anything other than { { fixed: T.untyped } } was a breaking change
@marknuzz marknuzz requested a review from a team as a code owner October 15, 2024 00:06
@marknuzz
Copy link
Contributor Author

I have signed the CLA!

Copy link
Contributor

@amomchilov amomchilov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me just check something internally, and I'll merge shortly.

@@ -196,6 +196,12 @@ class ActiveRecord::Base
end

class ActiveRecord::Relation
Elem = type_member(:out) { { fixed: T.untyped } }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, it's odd that Sorbet is requiring this, as ActiveRecord::Relation already does an include Enumerable:

https://github.com/rails/rails/blob/f481602bc5f0e2b79f95b73d7cc343303dd69672/activerecord/lib/active_record/relation.rb#L67

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NVM, Sorbet always makes you re-declare generic type variables.

@amomchilov amomchilov merged commit f99caa1 into Shopify:main Oct 15, 2024
4 checks passed
@amomchilov amomchilov changed the title Add annotation for ActiveRecord::Relation#each Add annotation for ActiveRecord::Relation#each Oct 21, 2024
@marknuzz marknuzz deleted the marknuzz/ar-relation-each branch October 22, 2024 06:19
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

Successfully merging this pull request may close these issues.

ActiveRecord relations' each method block argument is T.untyped
2 participants