From e49a69679db39d48ef3ddb1686fa394db81b0db1 Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Fri, 7 Mar 2025 15:44:13 -0800 Subject: [PATCH] Enable strict typing in TopologicalHash --- Library/Homebrew/cask/installer.rb | 2 +- Library/Homebrew/utils/topological_hash.rb | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Library/Homebrew/cask/installer.rb b/Library/Homebrew/cask/installer.rb index e95f8d5677..f2db98280c 100644 --- a/Library/Homebrew/cask/installer.rb +++ b/Library/Homebrew/cask/installer.rb @@ -374,7 +374,7 @@ on_request: true) graph = ::Utils::TopologicalHash.graph_package_dependencies(@cask) - raise CaskSelfReferencingDependencyError, @cask.token if graph[@cask].include?(@cask) + raise CaskSelfReferencingDependencyError, @cask.token if graph.fetch(@cask).include?(@cask) ::Utils::TopologicalHash.graph_package_dependencies(primary_container.dependencies, graph) diff --git a/Library/Homebrew/utils/topological_hash.rb b/Library/Homebrew/utils/topological_hash.rb index cf7382f42b..9b6cec4f20 100644 --- a/Library/Homebrew/utils/topological_hash.rb +++ b/Library/Homebrew/utils/topological_hash.rb @@ -1,4 +1,4 @@ -# typed: true # rubocop:todo Sorbet/StrictSigil +# typed: strict # frozen_string_literal: true require "tsort" @@ -6,11 +6,18 @@ require "tsort" module Utils # Topologically sortable hash map. class TopologicalHash < Hash + extend T::Generic include TSort + CaskOrFormula = T.type_alias { T.any(Cask::Cask, Formula) } + + K = type_member { { fixed: CaskOrFormula } } + V = type_member { { fixed: T::Array[CaskOrFormula] } } + Elem = type_member(:out) { { fixed: [CaskOrFormula, T::Array[CaskOrFormula]] } } + sig { params( - packages: T.any(Cask::Cask, Formula, T::Array[T.any(Cask::Cask, Formula)]), + packages: T.any(CaskOrFormula, T::Array[CaskOrFormula]), accumulator: TopologicalHash, ).returns(TopologicalHash) } @@ -48,10 +55,12 @@ module Utils private + sig { params(block: T.proc.params(arg0: K).void).void } def tsort_each_node(&block) each_key(&block) end + sig { params(node: K, block: T.proc.params(arg0: CaskOrFormula).void).returns(V) } def tsort_each_child(node, &block) fetch(node).each(&block) end