MacOSRequirement: add more type signatures

This adds more type signatures to `MacOSRequirement` to move it closer
to `typed: strict`. There are a few areas that aren't quite clear to
me based on the code and existing tests, so I've done what I can at
the moment.
This commit is contained in:
Sam Ford 2025-04-11 17:18:41 -04:00
parent 8350b8a43a
commit ccfc31bc18
No known key found for this signature in database
GPG Key ID: 7AF5CBEE1DD6F76D
2 changed files with 19 additions and 7 deletions

View File

@ -60,9 +60,9 @@ module Cask
elsif MacOSVersion::SYMBOLS.key?(args.first) elsif MacOSVersion::SYMBOLS.key?(args.first)
MacOSRequirement.new([args.first], comparator: "==") MacOSRequirement.new([args.first], comparator: "==")
elsif (md = /^\s*(?<comparator><|>|[=<>]=)\s*:(?<version>\S+)\s*$/.match(first_arg)) elsif (md = /^\s*(?<comparator><|>|[=<>]=)\s*:(?<version>\S+)\s*$/.match(first_arg))
MacOSRequirement.new([T.must(md[:version]).to_sym], comparator: md[:comparator]) MacOSRequirement.new([T.must(md[:version]).to_sym], comparator: T.must(md[:comparator]))
elsif (md = /^\s*(?<comparator><|>|[=<>]=)\s*(?<version>\S+)\s*$/.match(first_arg)) elsif (md = /^\s*(?<comparator><|>|[=<>]=)\s*(?<version>\S+)\s*$/.match(first_arg))
MacOSRequirement.new([md[:version]], comparator: md[:comparator]) MacOSRequirement.new([md[:version]], comparator: T.must(md[:comparator]))
# This is not duplicate of the first case: see `args.first` and a different comparator. # This is not duplicate of the first case: see `args.first` and a different comparator.
else # rubocop:disable Lint/DuplicateBranch else # rubocop:disable Lint/DuplicateBranch
MacOSRequirement.new([args.first], comparator: "==") MacOSRequirement.new([args.first], comparator: "==")

View File

@ -7,14 +7,17 @@ require "requirement"
class MacOSRequirement < Requirement class MacOSRequirement < Requirement
fatal true fatal true
attr_reader :comparator, :version sig { returns(String) }
attr_reader :comparator
attr_reader :version
# TODO: when Yosemite is removed here, keep these around as empty arrays so we # TODO: when Yosemite is removed here, keep these around as empty arrays so we
# can keep the deprecation/disabling code the same. # can keep the deprecation/disabling code the same.
DISABLED_MACOS_VERSIONS = [ DISABLED_MACOS_VERSIONS = T.let([
:yosemite, :yosemite,
].freeze ].freeze, T::Array[Symbol])
DEPRECATED_MACOS_VERSIONS = [].freeze DEPRECATED_MACOS_VERSIONS = T.let([].freeze, T::Array[Symbol])
def initialize(tags = [], comparator: ">=") def initialize(tags = [], comparator: ">=")
@version = begin @version = begin
@ -44,10 +47,11 @@ class MacOSRequirement < Requirement
MacOSVersion.new(HOMEBREW_MACOS_OLDEST_ALLOWED) if comparator == ">=" MacOSVersion.new(HOMEBREW_MACOS_OLDEST_ALLOWED) if comparator == ">="
end end
@comparator = comparator @comparator = T.let(comparator, String)
super(tags.drop(1)) super(tags.drop(1))
end end
sig { returns(T::Boolean) }
def version_specified? def version_specified?
@version.present? @version.present?
end end
@ -61,6 +65,7 @@ class MacOSRequirement < Requirement
false false
end end
sig { returns(MacOSVersion) }
def minimum_version def minimum_version
return MacOSVersion.new(HOMEBREW_MACOS_OLDEST_ALLOWED) if @comparator == "<=" || !version_specified? return MacOSVersion.new(HOMEBREW_MACOS_OLDEST_ALLOWED) if @comparator == "<=" || !version_specified?
return @version.min if @version.respond_to?(:to_ary) return @version.min if @version.respond_to?(:to_ary)
@ -68,6 +73,7 @@ class MacOSRequirement < Requirement
@version @version
end end
sig { returns(MacOSVersion) }
def maximum_version def maximum_version
return MacOSVersion.new(HOMEBREW_MACOS_NEWEST_UNSUPPORTED) if @comparator == ">=" || !version_specified? return MacOSVersion.new(HOMEBREW_MACOS_NEWEST_UNSUPPORTED) if @comparator == ">=" || !version_specified?
return @version.max if @version.respond_to?(:to_ary) return @version.max if @version.respond_to?(:to_ary)
@ -75,6 +81,7 @@ class MacOSRequirement < Requirement
@version @version
end end
sig { params(other: MacOSVersion).returns(T::Boolean) }
def allows?(other) def allows?(other)
return true unless version_specified? return true unless version_specified?
@ -98,6 +105,7 @@ class MacOSRequirement < Requirement
end end
end end
sig { params(type: Symbol).returns(String) }
def message(type: :formula) def message(type: :formula)
return "macOS is required for this software." unless version_specified? return "macOS is required for this software." unless version_specified?
@ -113,6 +121,8 @@ class MacOSRequirement < Requirement
EOS EOS
when :cask when :cask
"This cask does not run on macOS versions newer than #{@version.pretty_name}." "This cask does not run on macOS versions newer than #{@version.pretty_name}."
else
"This does not run on macOS versions newer than #{@version.pretty_name}."
end end
else else
if @version.respond_to?(:to_ary) if @version.respond_to?(:to_ary)
@ -129,6 +139,7 @@ class MacOSRequirement < Requirement
end end
alias eql? == alias eql? ==
sig { returns(Integer) }
def hash def hash
[super, comparator, version].hash [super, comparator, version].hash
end end
@ -151,6 +162,7 @@ class MacOSRequirement < Requirement
end end
end end
sig { params(options: T.untyped).returns(String) }
def to_json(options) def to_json(options)
comp = @comparator.to_s comp = @comparator.to_s
return { comp => @version.map(&:to_s) }.to_json(options) if @version.is_a?(Array) return { comp => @version.map(&:to_s) }.to_json(options) if @version.is_a?(Array)