From 1cfee5a1d3d919499cb705c566c04627c80239e4 Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Sun, 14 Sep 2025 11:31:41 -0700 Subject: [PATCH] Enable in tests only via ENV --- Library/Homebrew/dev-cmd/tests.rb | 1 + Library/Homebrew/env_config.rb | 5 ++ Library/Homebrew/standalone/sorbet.rb | 122 +++++++++++--------------- 3 files changed, 55 insertions(+), 73 deletions(-) diff --git a/Library/Homebrew/dev-cmd/tests.rb b/Library/Homebrew/dev-cmd/tests.rb index 3b1c1f097e..0780be102d 100644 --- a/Library/Homebrew/dev-cmd/tests.rb +++ b/Library/Homebrew/dev-cmd/tests.rb @@ -249,6 +249,7 @@ module Homebrew ENV["HOMEBREW_TEST_GENERIC_OS"] = "1" if args.generic? ENV["HOMEBREW_TEST_ONLINE"] = "1" if args.online? ENV["HOMEBREW_SORBET_RUNTIME"] = "1" + ENV["HOMEBREW_SORBET_RECURSIVE"] = "1" ENV["USER"] ||= system_command!("id", args: ["-nu"]).stdout.chomp diff --git a/Library/Homebrew/env_config.rb b/Library/Homebrew/env_config.rb index 328bd734f8..40bdbadbbd 100644 --- a/Library/Homebrew/env_config.rb +++ b/Library/Homebrew/env_config.rb @@ -471,6 +471,11 @@ module Homebrew "of macOS. This is useful in development on new macOS versions.", boolean: true, }, + HOMEBREW_SORBET_RECURSIVE: { + description: "If set along with `$HOMEBREW_SORBET_RUNTIME`, enable recursive typechecking using Sorbet. " \ + "Auomatically enabled when running tests.", + boolean: true, + }, HOMEBREW_SORBET_RUNTIME: { description: "If set, enable runtime typechecking using Sorbet. " \ "Set by default for `$HOMEBREW_DEVELOPER` or when running some developer commands.", diff --git a/Library/Homebrew/standalone/sorbet.rb b/Library/Homebrew/standalone/sorbet.rb index e9202f06e0..332356f5c5 100644 --- a/Library/Homebrew/standalone/sorbet.rb +++ b/Library/Homebrew/standalone/sorbet.rb @@ -10,6 +10,55 @@ require "extend/module" # There are mechanisms to achieve a middle ground (`default_checked_level`). if ENV["HOMEBREW_SORBET_RUNTIME"] T::Configuration.enable_final_checks_on_hooks + if ENV["HOMEBREW_SORBET_RECURSIVE"] == "1" + module T + module Types + class FixedArray < Base + def valid?(obj) = recursively_valid?(obj) + end + + class FixedHash < Base + def valid?(obj) = recursively_valid?(obj) + end + + class Intersection < Base + def valid?(obj) = recursively_valid?(obj) + end + + class TypedArray < TypedEnumerable + def valid?(obj) = recursively_valid?(obj) + end + + class TypedEnumerable < Base + def valid?(obj) = recursively_valid?(obj) + end + + class TypedEnumeratorChain < TypedEnumerable + def valid?(obj) = recursively_valid?(obj) + end + + class TypedEnumeratorLazy < TypedEnumerable + def valid?(obj) = recursively_valid?(obj) + end + + class TypedHash < TypedEnumerable + def valid?(obj) = recursively_valid?(obj) + end + + class TypedRange < TypedEnumerable + def valid?(obj) = recursively_valid?(obj) + end + + class TypedSet < TypedEnumerable + def valid?(obj) = recursively_valid?(obj) + end + + class Union < Base + def valid?(obj) = recursively_valid?(obj) + end + end + end + end else # Redefine `T.let`, etc. to make the `checked` parameter default to `false` rather than `true`. # @private @@ -47,76 +96,3 @@ else T::Configuration.call_validation_error_handler = ->(signature, opts) {} T::Configuration.inline_type_error_handler = ->(error, opts) {} end - -# TODO: only do this in specs -module T - module Types - class FixedArray < Base - def valid?(obj) - recursively_valid?(obj) - end - end - - class FixedHash < Base - def valid?(obj) - recursively_valid?(obj) - end - end - - class Intersection < Base - def valid?(obj) - recursively_valid?(obj) - end - end - - class TypedArray < TypedEnumerable - # overrides Base - def valid?(obj) - recursively_valid?(obj) - end - end - - class TypedEnumerable < Base - def valid?(obj) - recursively_valid?(obj) - end - end - - class TypedEnumeratorChain < TypedEnumerable - def valid?(obj) - recursively_valid?(obj) - end - end - - class TypedEnumeratorLazy < TypedEnumerable - def valid?(obj) - recursively_valid?(obj) - end - end - - class TypedHash < TypedEnumerable - # overrides Base - def valid?(obj) - recursively_valid?(obj) - end - end - - class TypedRange < TypedEnumerable - def valid?(obj) - recursively_valid?(obj) - end - end - - class TypedSet < TypedEnumerable - def valid?(obj) - recursively_valid?(obj) - end - end - - class Union < Base - def valid?(obj) - recursively_valid?(obj) - end - end - end -end