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

Don't assume nilable when #__tapioca_type is provided #2126

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions lib/tapioca/dsl/compilers/active_model_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,26 +101,26 @@ def handle_method_pattern?(pattern)

sig { params(attribute_type_value: T.untyped).returns(::String) }
def type_for(attribute_type_value)
type = case attribute_type_value
case attribute_type_value
when ActiveModel::Type::Boolean
"T::Boolean"
as_nilable_type("T::Boolean")
when ActiveModel::Type::Date
"::Date"
as_nilable_type("::Date")
when ActiveModel::Type::DateTime, ActiveModel::Type::Time
"::Time"
as_nilable_type("::Time")
when ActiveModel::Type::Decimal
"::BigDecimal"
as_nilable_type("::BigDecimal")
when ActiveModel::Type::Float
"::Float"
as_nilable_type("::Float")
when ActiveModel::Type::Integer
"::Integer"
as_nilable_type("::Integer")
when ActiveModel::Type::String
"::String"
as_nilable_type("::String")
else
Helpers::ActiveModelTypeHelper.type_for(attribute_type_value)
type = Helpers::ActiveModelTypeHelper.type_for(attribute_type_value)
type = as_nilable_type(type) if Helpers::ActiveModelTypeHelper.assume_nilable?(attribute_type_value)
type
end

as_nilable_type(type)
end

sig { params(klass: RBI::Scope, method: String, type: String).void }
Expand Down
5 changes: 5 additions & 0 deletions lib/tapioca/dsl/helpers/active_model_type_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ def type_for(type_value)
type.to_s
end

sig { params(type_value: T.untyped).returns(T::Boolean) }
def assume_nilable?(type_value)
!type_value.respond_to?(:__tapioca_type)
end

private

MEANINGLESS_TYPES = T.let(
Expand Down
25 changes: 17 additions & 8 deletions spec/tapioca/dsl/compilers/active_model_attributes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,24 +193,27 @@ def custom_with_cast_sig_attr=(value); end
it "generates method sigs for attribute type with `#__tapioca_type`" do
add_ruby_file("shop.rb", <<~RUBY)
class GenericType < ActiveModel::Type::Value
extend T::Sig

def initialize(tapioca_type)
@tapioca_type = tapioca_type
end

def __tapioca_type
@tapioca_type
end
def __tapioca_type = @tapioca_type
end

class Post
end
class User; end
class Post; end

class Shop
include ActiveModel::Model
include ActiveModel::Attributes

attribute :post, GenericType.new(Post)
attribute :user, GenericType.new(User)
attribute :post, GenericType.new(T.nilable(Post))

def initialize(attributes = {})
raise 'User is required' unless attributes.key?(:user)
super
end
end
RUBY

Expand All @@ -223,6 +226,12 @@ def post; end

sig { params(value: T.nilable(::Post)).returns(T.nilable(::Post)) }
def post=(value); end

sig { returns(::User) }
def user; end

sig { params(value: ::User).returns(::User) }
def user=(value); end
end
RBI

Expand Down
25 changes: 25 additions & 0 deletions spec/tapioca/dsl/helpers/active_model_type_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,31 @@ def cast_value(value)
)
end
end

describe "assume_nilable?" do
it "assumes the type is nilable when `#__tapioca_type` is not defined" do
klass = Class.new(ActiveModel::Type::Value)

assert_equal(
true,
Tapioca::Dsl::Helpers::ActiveModelTypeHelper.assume_nilable?(klass.new),
)
end

it "does not assume the type is nilable when `#__tapioca_type` is defined" do
klass = Class.new(ActiveModel::Type::Value) do
extend T::Sig

sig { returns(Module) }
def __tapioca_type = String
end

assert_equal(
false,
Tapioca::Dsl::Helpers::ActiveModelTypeHelper.assume_nilable?(klass.new),
)
end
end
end
end
end
Expand Down
Loading