-
Notifications
You must be signed in to change notification settings - Fork 136
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
Use type_for_attribute
to determine primary_key for find
#1851
Conversation
sig { params(args: T.any(String, Symbol, ::ActiveSupport::Multibyte::Chars, T::Boolean, BigDecimal, Numeric, ::ActiveRecord::Type::Binary::Data, ::ActiveRecord::Type::Time::Value, Date, Time, ::ActiveSupport::Duration, T::Class[T.anything])).returns(::Post) } | ||
sig { params(args: T::Array[T.any(String, Symbol, ::ActiveSupport::Multibyte::Chars, T::Boolean, BigDecimal, Numeric, ::ActiveRecord::Type::Binary::Data, ::ActiveRecord::Type::Time::Value, Date, Time, ::ActiveSupport::Duration, T::Class[T.anything])]).returns(T::Enumerable[::Post]) } | ||
sig { params(args: T.any(String, Symbol, ::ActiveSupport::Multibyte::Chars, T::Boolean, BigDecimal, Numeric, ::ActiveRecord::Type::Binary::Data, ::ActiveRecord::Type::Time::Value, Date, Time, ::ActiveSupport::Duration, T::Class[T.anything], T.nilable(::CustomId))).returns(::Post) } | ||
sig { params(args: T::Array[T.any(String, Symbol, ::ActiveSupport::Multibyte::Chars, T::Boolean, BigDecimal, Numeric, ::ActiveRecord::Type::Binary::Data, ::ActiveRecord::Type::Time::Value, Date, Time, ::ActiveSupport::Duration, T::Class[T.anything], T.nilable(::CustomId))]).returns(T::Enumerable[::Post]) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code should not generate a nilable type, since Model.find(nil)
will raise:
[...]/activerecord-7.1.3.2/lib/active_record/relation/finder_methods.rb:497:in 'find_with_ids': Couldn't find Model without an ID (ActiveRecord::RecordNotFound)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, this is because the deserialize
sig says that's the return value so it's extracting from that. I could do a Regex replace of T.nilable(
and )
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I guess that's the quickest way. Keep in mind you could also see ::T.nilable
since it uses this:
tapioca/lib/tapioca/helpers/rbi_helper.rb
Line 92 in 0c24553
if type.start_with?("T.nilable(", "::T.nilable(") || type == "T.untyped" || type == "::T.untyped" |
Thanks @alex-tan, this needs a rebase on main and a CLA agreement using https://github.com/Shopify/tapioca/actions/runs/8599879286/job/23563693229?pr=1851#step:2:12 |
Thanks @KaanOzkan. I'm working on getting the CLA signed by my company. I'll get this rebased in the meantime. |
da45252
to
c2d409d
Compare
cf26ef7
to
9c563a2
Compare
I have signed the CLA! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strange failure, maybe a rebase will fix it.
9c563a2
to
8912c37
Compare
Ah weird. I just rebased again 👍 |
Motivation
Issue #1845
We use custom ID types for our models. The current sigs for
find
don't reflect the use ofActiveRecord::Attributes::ClassMethods.attribute
Implementation
I utilized
Tapioca::Dsl::Helpers::ActiveModelTypeHelper.type_for
which reflects on the signatures of theActiveModel::Type::Value
subclasses to determine what other potential types should be in the signatures.Tests
I have added tests.