Livecheck::Options: Enable typed: strong

This enables `typed: strong` in `Options` and resolves the related
errors.

I annotated the return value of `serialize` in `#to_hash` to resolve
a type error and then updated other methods to use `to_hash` instead.
This approach resolves similar type errors without duplicating the
same `serialize` annotation.

For `#==`, I switched `instance_of?(other.class)` to
`other.is_a?(Options)`, as Sorbet understands that `is_a?` ensures
`other` is an `Options` object but doesn't seem to understand
that `instance_of?` was doing the same thing. The tests continue to
pass with these changes, so hopefully this is fine.
This commit is contained in:
Sam Ford 2025-02-14 13:48:21 -05:00
parent 8afa354c35
commit c1401ae360
No known key found for this signature in database
GPG Key ID: 7AF5CBEE1DD6F76D

View File

@ -1,4 +1,4 @@
# typed: strict # typed: strong
# frozen_string_literal: true # frozen_string_literal: true
module Homebrew module Homebrew
@ -30,11 +30,13 @@ module Homebrew
# Returns a `Hash` of all instance variables, using `String` keys. # Returns a `Hash` of all instance variables, using `String` keys.
sig { returns(T::Hash[String, T.untyped]) } sig { returns(T::Hash[String, T.untyped]) }
def to_hash = serialize def to_hash
T.let(serialize, T::Hash[String, T.untyped])
end
# Returns a `Hash` of all instance variables, using `Symbol` keys. # Returns a `Hash` of all instance variables, using `Symbol` keys.
sig { returns(T::Hash[Symbol, T.untyped]) } sig { returns(T::Hash[Symbol, T.untyped]) }
def to_h = serialize.transform_keys(&:to_sym) def to_h = to_hash.transform_keys(&:to_sym)
# Returns a new object formed by merging `other` values with a copy of # Returns a new object formed by merging `other` values with a copy of
# `self`. # `self`.
@ -54,10 +56,11 @@ module Homebrew
Options.new(**new_options) Options.new(**new_options)
end end
sig { params(other: T.untyped).returns(T::Boolean) } sig { params(other: Object).returns(T::Boolean) }
def ==(other) def ==(other)
instance_of?(other.class) && return false unless other.is_a?(Options)
@homebrew_curl == other.homebrew_curl &&
@homebrew_curl == other.homebrew_curl &&
@post_form == other.post_form && @post_form == other.post_form &&
@post_json == other.post_json @post_json == other.post_json
end end
@ -65,7 +68,7 @@ module Homebrew
# Whether the object has only default values. # Whether the object has only default values.
sig { returns(T::Boolean) } sig { returns(T::Boolean) }
def empty? = serialize.empty? def empty? = to_hash.empty?
# Whether the object has any non-default values. # Whether the object has any non-default values.
sig { returns(T::Boolean) } sig { returns(T::Boolean) }