From c1401ae360854e90a8a2f77627e282f141bdaa6f Mon Sep 17 00:00:00 2001 From: Sam Ford <1584702+samford@users.noreply.github.com> Date: Fri, 14 Feb 2025 13:48:21 -0500 Subject: [PATCH] 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. --- Library/Homebrew/livecheck/options.rb | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/Library/Homebrew/livecheck/options.rb b/Library/Homebrew/livecheck/options.rb index e4290b5203..2f851185fd 100644 --- a/Library/Homebrew/livecheck/options.rb +++ b/Library/Homebrew/livecheck/options.rb @@ -1,4 +1,4 @@ -# typed: strict +# typed: strong # frozen_string_literal: true module Homebrew @@ -30,11 +30,13 @@ module Homebrew # Returns a `Hash` of all instance variables, using `String` keys. 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. 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 # `self`. @@ -54,10 +56,11 @@ module Homebrew Options.new(**new_options) end - sig { params(other: T.untyped).returns(T::Boolean) } + sig { params(other: Object).returns(T::Boolean) } def ==(other) - instance_of?(other.class) && - @homebrew_curl == other.homebrew_curl && + return false unless other.is_a?(Options) + + @homebrew_curl == other.homebrew_curl && @post_form == other.post_form && @post_json == other.post_json end @@ -65,7 +68,7 @@ module Homebrew # Whether the object has only default values. sig { returns(T::Boolean) } - def empty? = serialize.empty? + def empty? = to_hash.empty? # Whether the object has any non-default values. sig { returns(T::Boolean) }