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)
MacOSRequirement.new([args.first], comparator: "==")
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))
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.
else # rubocop:disable Lint/DuplicateBranch
MacOSRequirement.new([args.first], comparator: "==")

View File

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