From 2d603b0c263474eb985e07b206a97e1d7601dcfa Mon Sep 17 00:00:00 2001 From: Sam Ford <1584702+samford@users.noreply.github.com> Date: Sat, 5 Apr 2025 16:03:18 -0400 Subject: [PATCH] OnSystem: enable strict typing --- Library/Homebrew/extend/on_system.rb | 55 ++++++++++++++++------------ Library/Homebrew/readall.rb | 2 +- 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/Library/Homebrew/extend/on_system.rb b/Library/Homebrew/extend/on_system.rb index 4e1854ad19..a4426cd5e0 100644 --- a/Library/Homebrew/extend/on_system.rb +++ b/Library/Homebrew/extend/on_system.rb @@ -1,20 +1,26 @@ -# typed: true # rubocop:todo Sorbet/StrictSigil +# typed: strict # frozen_string_literal: true require "simulate_system" module OnSystem - ARCH_OPTIONS = [:intel, :arm].freeze - BASE_OS_OPTIONS = [:macos, :linux].freeze - ALL_OS_OPTIONS = [*MacOSVersion::SYMBOLS.keys, :linux].freeze - ALL_OS_ARCH_COMBINATIONS = ALL_OS_OPTIONS.product(ARCH_OPTIONS).freeze + ARCH_OPTIONS = T.let([:intel, :arm].freeze, T::Array[Symbol]) + BASE_OS_OPTIONS = T.let([:macos, :linux].freeze, T::Array[Symbol]) + ALL_OS_OPTIONS = T.let([*MacOSVersion::SYMBOLS.keys, :linux].freeze, T::Array[Symbol]) + ALL_OS_ARCH_COMBINATIONS = T.let( + ALL_OS_OPTIONS.product(ARCH_OPTIONS).freeze, + T::Array[[Symbol, Symbol]], + ) - VALID_OS_ARCH_TAGS = ALL_OS_ARCH_COMBINATIONS.filter_map do |os, arch| - tag = Utils::Bottles::Tag.new(system: os, arch:) - next unless tag.valid_combination? + VALID_OS_ARCH_TAGS = T.let( + ALL_OS_ARCH_COMBINATIONS.filter_map do |os, arch| + tag = Utils::Bottles::Tag.new(system: os, arch:) + next unless tag.valid_combination? - tag - end.freeze + tag + end.freeze, + T::Array[Utils::Bottles::Tag], + ) sig { params(arch: Symbol).returns(T::Boolean) } def self.arch_condition_met?(arch) @@ -55,15 +61,15 @@ module OnSystem method_name.to_s.sub(/^on_/, "").to_sym end - sig { params(base: Class).void } + sig { params(base: T::Class[T.anything]).void } def self.setup_arch_methods(base) ARCH_OPTIONS.each do |arch| base.define_method(:"on_#{arch}") do |&block| - @on_system_blocks_exist = true + @on_system_blocks_exist = T.let(true, T.nilable(T::Boolean)) return unless OnSystem.arch_condition_met? OnSystem.condition_from_method_name(T.must(__method__)) - @called_in_on_system_block = true + @called_in_on_system_block = T.let(true, T.nilable(T::Boolean)) result = block.call @called_in_on_system_block = false @@ -82,7 +88,7 @@ module OnSystem end end - sig { params(base: Class).void } + sig { params(base: T::Class[T.anything]).void } def self.setup_base_os_methods(base) BASE_OS_OPTIONS.each do |base_os| base.define_method(:"on_#{base_os}") do |&block| @@ -128,7 +134,7 @@ module OnSystem end end - sig { params(base: Class).void } + sig { params(base: T::Class[T.anything]).void } def self.setup_macos_methods(base) MacOSVersion::SYMBOLS.each_key do |os_name| base.define_method(:"on_#{os_name}") do |or_condition = nil, &block| @@ -137,11 +143,14 @@ module OnSystem os_condition = OnSystem.condition_from_method_name T.must(__method__) return unless OnSystem.os_condition_met? os_condition, or_condition - @on_system_block_min_os = if or_condition == :or_older - @called_in_on_system_block ? @on_system_block_min_os : MacOSVersion.new(HOMEBREW_MACOS_OLDEST_ALLOWED) - else - MacOSVersion.from_symbol(os_condition) - end + @on_system_block_min_os = T.let( + if or_condition == :or_older + @called_in_on_system_block ? @on_system_block_min_os : MacOSVersion.new(HOMEBREW_MACOS_OLDEST_ALLOWED) + else + MacOSVersion.from_symbol(os_condition) + end, + T.nilable(MacOSVersion), + ) @called_in_on_system_block = true result = block.call @called_in_on_system_block = false @@ -151,13 +160,13 @@ module OnSystem end end - sig { params(_base: Class).void } + sig { params(_base: T::Class[T.anything]).void } def self.included(_base) raise "Do not include `OnSystem` directly. Instead, include `OnSystem::MacOSAndLinux` or `OnSystem::MacOSOnly`" end module MacOSAndLinux - sig { params(base: Class).void } + sig { params(base: T::Class[T.anything]).void } def self.included(base) OnSystem.setup_arch_methods(base) OnSystem.setup_base_os_methods(base) @@ -166,7 +175,7 @@ module OnSystem end module MacOSOnly - sig { params(base: Class).void } + sig { params(base: T::Class[T.anything]).void } def self.included(base) OnSystem.setup_arch_methods(base) OnSystem.setup_macos_methods(base) diff --git a/Library/Homebrew/readall.rb b/Library/Homebrew/readall.rb index 363fad0d29..36ae737fcf 100644 --- a/Library/Homebrew/readall.rb +++ b/Library/Homebrew/readall.rb @@ -90,7 +90,7 @@ module Readall sig { params( - tap: Tap, aliases: T::Boolean, no_simulate: T::Boolean, os_arch_combinations: T::Array[T::Array[String]], + tap: Tap, aliases: T::Boolean, no_simulate: T::Boolean, os_arch_combinations: T::Array[[Symbol, Symbol]], ).returns(T::Boolean) } def self.valid_tap?(tap, aliases: false, no_simulate: false,