From 02fd0422aaaedd16b6a6f1bfee15a32ac787f07e Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Tue, 7 Mar 2023 17:24:20 -0800 Subject: [PATCH] Enable typing in a few more files --- Library/Homebrew/cask/metadata.rb | 2 +- Library/Homebrew/cask/metdata.rbi | 7 +++++++ Library/Homebrew/cask/url.rbi | 1 - Library/Homebrew/diagnostic.rb | 8 +++++--- Library/Homebrew/formula_assertions.rb | 2 +- Library/Homebrew/formula_assertions.rbi | 5 +++++ Library/Homebrew/keg_relocate.rb | 10 +++++----- Library/Homebrew/language/python.rb | 2 +- Library/Homebrew/language/python.rbi | 11 +++++++++++ Library/Homebrew/test.rb | 8 +++----- Library/Homebrew/unlink.rb | 8 +++----- Library/Homebrew/utils/analytics.rb | 11 +++++------ Library/Homebrew/utils/github/api.rb | 6 +++--- Library/Homebrew/utils/github/api.rbi | 5 +++++ Library/Homebrew/utils/repology.rb | 2 +- Library/Homebrew/utils/repology.rbi | 6 ++++++ 16 files changed, 62 insertions(+), 32 deletions(-) create mode 100644 Library/Homebrew/cask/metdata.rbi create mode 100644 Library/Homebrew/formula_assertions.rbi create mode 100644 Library/Homebrew/language/python.rbi create mode 100644 Library/Homebrew/utils/github/api.rbi create mode 100644 Library/Homebrew/utils/repology.rbi diff --git a/Library/Homebrew/cask/metadata.rb b/Library/Homebrew/cask/metadata.rb index c2db2e2cc0..123dc6e0b5 100644 --- a/Library/Homebrew/cask/metadata.rb +++ b/Library/Homebrew/cask/metadata.rb @@ -1,4 +1,4 @@ -# typed: false +# typed: true # frozen_string_literal: true module Cask diff --git a/Library/Homebrew/cask/metdata.rbi b/Library/Homebrew/cask/metdata.rbi new file mode 100644 index 0000000000..1b92e9ce74 --- /dev/null +++ b/Library/Homebrew/cask/metdata.rbi @@ -0,0 +1,7 @@ +# typed: strict + +module Cask + module Metadata + requires_ancestor { Cask } + end +end diff --git a/Library/Homebrew/cask/url.rbi b/Library/Homebrew/cask/url.rbi index 6f32e1b3b2..9070b2fc01 100644 --- a/Library/Homebrew/cask/url.rbi +++ b/Library/Homebrew/cask/url.rbi @@ -1,5 +1,4 @@ # typed: strict -# typed: false class URL include Kernel diff --git a/Library/Homebrew/diagnostic.rb b/Library/Homebrew/diagnostic.rb index 821564e658..455bfed241 100644 --- a/Library/Homebrew/diagnostic.rb +++ b/Library/Homebrew/diagnostic.rb @@ -1,4 +1,4 @@ -# typed: false +# typed: true # frozen_string_literal: true require "keg" @@ -31,7 +31,7 @@ module Homebrew def self.checks(type, fatal: true) @checks ||= Checks.new - failed = false + failed = T.let(false, T::Boolean) @checks.public_send(type).each do |check| out = @checks.public_send(check) next if out.nil? @@ -64,6 +64,7 @@ module Homebrew end end + sig { params(list: T::Array[String], string: String).returns(String) } def inject_file_list(list, string) list.reduce(string.dup) { |acc, elem| acc << " #{elem}\n" } .freeze @@ -642,10 +643,11 @@ module Homebrew EOS end + sig { returns(T.nilable(String)) } def check_git_status return unless Utils::Git.available? - message = nil + message = T.let(nil, T.nilable(String)) repos = { "Homebrew/brew" => HOMEBREW_REPOSITORY, diff --git a/Library/Homebrew/formula_assertions.rb b/Library/Homebrew/formula_assertions.rb index 382bb085ca..8f84f6479b 100644 --- a/Library/Homebrew/formula_assertions.rb +++ b/Library/Homebrew/formula_assertions.rb @@ -1,4 +1,4 @@ -# typed: false +# typed: true # frozen_string_literal: true module Homebrew diff --git a/Library/Homebrew/formula_assertions.rbi b/Library/Homebrew/formula_assertions.rbi new file mode 100644 index 0000000000..3e75262939 --- /dev/null +++ b/Library/Homebrew/formula_assertions.rbi @@ -0,0 +1,5 @@ +# typed: strict + +module Homebrew::Assertions + include Kernel +end diff --git a/Library/Homebrew/keg_relocate.rb b/Library/Homebrew/keg_relocate.rb index 961d3f27fc..083529719b 100644 --- a/Library/Homebrew/keg_relocate.rb +++ b/Library/Homebrew/keg_relocate.rb @@ -1,4 +1,4 @@ -# typed: false +# typed: true # frozen_string_literal: true class Keg @@ -44,7 +44,7 @@ class Keg key.is_a?(String) ? key.length : 999 end.reverse - any_changed = false + any_changed = T.let(nil, T.nilable(String)) sorted_keys.each do |key| changed = text.gsub!(key, replacements[key]) any_changed ||= changed @@ -144,7 +144,7 @@ class Keg def replace_text_in_files(relocation, files: nil) files ||= text_files | libtool_files - changed_files = [] + changed_files = T.let([], Array) files.map(&path.method(:join)).group_by { |f| f.stat.ino }.each_value do |first, *rest| s = first.open("rb", &:read) @@ -179,11 +179,11 @@ class Keg binary = File.binread file odebug "Replacing build prefix in: #{file}" binary_strings = binary.split(/#{NULL_BYTE}/o, -1) - match_indices = binary_strings.each_index.select { |i| binary_strings[i].include?(old_prefix) } + match_indices = binary_strings.each_index.select { |i| binary_strings.fetch(i).include?(old_prefix) } # Only perform substitution on strings which match prefix regex. match_indices.each do |i| - s = binary_strings[i] + s = binary_strings.fetch(i) binary_strings[i] = s.gsub(old_prefix, new_prefix) .ljust(s.size, NULL_BYTE) end diff --git a/Library/Homebrew/language/python.rb b/Library/Homebrew/language/python.rb index c0df766d43..506865790a 100644 --- a/Library/Homebrew/language/python.rb +++ b/Library/Homebrew/language/python.rb @@ -1,4 +1,4 @@ -# typed: false +# typed: true # frozen_string_literal: true module Language diff --git a/Library/Homebrew/language/python.rbi b/Library/Homebrew/language/python.rbi new file mode 100644 index 0000000000..ab011b8db7 --- /dev/null +++ b/Library/Homebrew/language/python.rbi @@ -0,0 +1,11 @@ +# typed: strict + +module Language::Python + module Shebang + include Kernel + end + + module Virtualenv + requires_ancestor { Formula } + end +end diff --git a/Library/Homebrew/test.rb b/Library/Homebrew/test.rb index b22b8c218a..55a46e63d2 100644 --- a/Library/Homebrew/test.rb +++ b/Library/Homebrew/test.rb @@ -1,4 +1,4 @@ -# typed: false +# typed: true # frozen_string_literal: true raise "#{__FILE__} must not be loaded via `require`." if $PROGRAM_NAME != __FILE__ @@ -38,12 +38,10 @@ begin formula.extend(Debrew::Formula) if args.debug? ENV.extend(Stdenv) - T.cast(ENV, Stdenv).setup_build_environment(formula: formula, testing_formula: true) + ENV.setup_build_environment(formula: formula, testing_formula: true) # tests can also return false to indicate failure - run_test = proc do - raise "test returned false" if formula.run_test(keep_tmp: args.keep_tmp?) == false - end + run_test = proc { |_ = nil| raise "test returned false" if formula.run_test(keep_tmp: args.keep_tmp?) == false } if args.debug? # --debug is interactive run_test.call else diff --git a/Library/Homebrew/unlink.rb b/Library/Homebrew/unlink.rb index ef9ce40722..043cadffc2 100644 --- a/Library/Homebrew/unlink.rb +++ b/Library/Homebrew/unlink.rb @@ -1,12 +1,10 @@ -# typed: false +# typed: true # frozen_string_literal: true module Homebrew # Provides helper methods for unlinking formulae and kegs with consistent output. module Unlink - module_function - - def unlink_versioned_formulae(formula, verbose: false) + def self.unlink_versioned_formulae(formula, verbose: false) formula.versioned_formulae .select(&:keg_only?) .select(&:linked?) @@ -18,7 +16,7 @@ module Homebrew end end - def unlink(keg, dry_run: false, verbose: false) + def self.unlink(keg, dry_run: false, verbose: false) options = { dry_run: dry_run, verbose: verbose } keg.lock do diff --git a/Library/Homebrew/utils/analytics.rb b/Library/Homebrew/utils/analytics.rb index 1c1fd04adc..3fb59c4e68 100644 --- a/Library/Homebrew/utils/analytics.rb +++ b/Library/Homebrew/utils/analytics.rb @@ -1,4 +1,4 @@ -# typed: false +# typed: true # frozen_string_literal: true require "context" @@ -42,7 +42,6 @@ module Utils --data av=#{HOMEBREW_VERSION} ] metadata.each do |key, value| - next unless key next unless value key = ERB::Util.url_encode key @@ -146,13 +145,13 @@ module Utils report_influx(measurement, package_and_options, on_request, additional_tags_influx) end - sig { params(exception: Exception).void } + sig { params(exception: BuildError).void } def report_build_error(exception) report_google_build_error(exception) report_influx_error(exception) end - sig { params(exception: Exception).void } + sig { params(exception: BuildError).void } def report_google_build_error(exception) return if not_this_run? || disabled? @@ -165,10 +164,10 @@ module Utils else formula_full_name end - report_google_event("BuildError", package_and_options) + report_google_event(:BuildError, package_and_options) end - sig { params(exception: Exception).void } + sig { params(exception: BuildError).void } def report_influx_error(exception) return if not_this_run? || disabled? diff --git a/Library/Homebrew/utils/github/api.rb b/Library/Homebrew/utils/github/api.rb index a49773a167..0d122975a9 100644 --- a/Library/Homebrew/utils/github/api.rb +++ b/Library/Homebrew/utils/github/api.rb @@ -1,4 +1,4 @@ -# typed: false +# typed: true # frozen_string_literal: true require "tempfile" @@ -214,7 +214,7 @@ module GitHub headers_tmpfile = Tempfile.new("github_api_headers", HOMEBREW_TEMP) begin - if data + if data_tmpfile data_tmpfile.write data data_tmpfile.close args += ["--data", "@#{data_tmpfile.path}"] @@ -222,7 +222,7 @@ module GitHub args += ["--request", request_method.to_s] if request_method end - args += ["--dump-header", headers_tmpfile.path] + args += ["--dump-header", T.must(headers_tmpfile.path)] output, errors, status = curl_output("--location", url.to_s, *args, secrets: [token]) output, _, http_code = output.rpartition("\n") diff --git a/Library/Homebrew/utils/github/api.rbi b/Library/Homebrew/utils/github/api.rbi new file mode 100644 index 0000000000..bb72675c19 --- /dev/null +++ b/Library/Homebrew/utils/github/api.rbi @@ -0,0 +1,5 @@ +# typed: strict + +module GitHub::API + include Kernel +end diff --git a/Library/Homebrew/utils/repology.rb b/Library/Homebrew/utils/repology.rb index 14396de368..dc9fc61cec 100644 --- a/Library/Homebrew/utils/repology.rb +++ b/Library/Homebrew/utils/repology.rb @@ -1,4 +1,4 @@ -# typed: false +# typed: true # frozen_string_literal: true require "utils/curl" diff --git a/Library/Homebrew/utils/repology.rbi b/Library/Homebrew/utils/repology.rbi new file mode 100644 index 0000000000..57ed1c467f --- /dev/null +++ b/Library/Homebrew/utils/repology.rbi @@ -0,0 +1,6 @@ +# typed: strict + +module Repology + include Kernel + requires_ancestor { Utils::Curl } +end