Closed olivier-thatch closed 1 month ago
Ah, I guess the reason why we really need this is because Tapioca will now generate versions of Array#present?
and friends without signatures, which will prevent Sorbet from using the signature from the Object#present?
version.
The motivation for adding these wasn't very clear to me until I understood this, so wanted to share for other onlookers.
I think we should also add:
class NilClass
sig { returns(TrueClass) }
def blank?; end
sig { returns(FalseClass) }
def present?; end
end
class FalseClass
sig { returns(TrueClass) }
def blank?; end
sig { returns(FalseClass) }
def present?; end
end
class TrueClass
sig { returns(FalseClass) }
def blank?; end
sig { returns(TrueClass) }
def present?; end
end
class Numeric
sig { returns(FalseClass) }
def blank?; end
sig { returns(TrueClass) }
def present?; end
end
class Time
sig { returns(FalseClass) }
def blank?; end
sig { returns(TrueClass) }
def present?; end
end
@paracycle Those all already exist :) Though some are marked as @shim
, which will no longer be true for Rails 7.2 as they will become actual methods, not shims.
Type of Change
Changes
Rails 7.2 defines
#present?
methods directly on a few classes likeArray
,Hash
,Numeric
etc.This requires new signatures, otherwise Sorbet defaults to
T.untyped
: https://sorbet.run/#%23%20typed%3A%20true%0A%0Aclass%20Object%0A%20%20extend%20T%3A%3ASig%0A%0A%20%20sig%20%7B%20returns%28T%3A%3ABoolean%29%20%7D%0A%20%20def%20blank%3F%0A%20%20%20%20respond_to%3F%28%3Aempty%3F%29%20%3F%20!!%28T.unsafe%28self%29.empty%3F%29%20%3A%20false%0A%20%20end%0A%0A%20%20sig%20%7B%20returns%28T%3A%3ABoolean%29%20%7D%0A%20%20def%20present%3F%0A%20%20%20%20!blank%3F%0A%20%20end%0Aend%0A%0Aclass%20Array%0A%20%20alias_method%20%3Ablank%3F%2C%20%3Aempty%3F%0Aend%0A%0Aclass%20ArrayRails72%20%3C%20Array%0A%20%20%23%20sig%20%7B%20returns%28T%3A%3ABoolean%29%20%7D%0A%20%20def%20present%3F%0A%20%20%20%20!empty%3F%0A%20%20end%0Aend%0A%0Aarr%20%3D%20%5B%5D%0AT.reveal_type%28arr.present%3F%29%0A%0Aarr_72%20%3D%20ArrayRails72.new%0AT.reveal_type%28arr_72.present%3F%29%0AThis should be safe to merge and use with previous Rails versions though, since the
#present?
method does exist onArray
andHash
even with Rails 7.1, it's just inherited fromObject
rather than defined directly on the classes.