Skip to content

Commit 3d7328a

Browse files
committed
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.
1 parent b85dc27 commit 3d7328a

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

Library/Homebrew/cask/dsl/depends_on.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ def macos=(*args)
6060
elsif MacOSVersion::SYMBOLS.key?(args.first)
6161
MacOSRequirement.new([args.first], comparator: "==")
6262
elsif (md = /^\s*(?<comparator><|>|[=<>]=)\s*:(?<version>\S+)\s*$/.match(first_arg))
63-
MacOSRequirement.new([T.must(md[:version]).to_sym], comparator: md[:comparator])
63+
MacOSRequirement.new([T.must(md[:version]).to_sym], comparator: T.must(md[:comparator]))
6464
elsif (md = /^\s*(?<comparator><|>|[=<>]=)\s*(?<version>\S+)\s*$/.match(first_arg))
65-
MacOSRequirement.new([md[:version]], comparator: md[:comparator])
65+
MacOSRequirement.new([md[:version]], comparator: T.must(md[:comparator]))
6666
# This is not duplicate of the first case: see `args.first` and a different comparator.
6767
else # rubocop:disable Lint/DuplicateBranch
6868
MacOSRequirement.new([args.first], comparator: "==")

Library/Homebrew/requirements/macos_requirement.rb

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@
77
class MacOSRequirement < Requirement
88
fatal true
99

10-
attr_reader :comparator, :version
10+
sig { returns(String) }
11+
attr_reader :comparator
12+
13+
attr_reader :version
1114

1215
# TODO: when Yosemite is removed here, keep these around as empty arrays so we
1316
# can keep the deprecation/disabling code the same.
14-
DISABLED_MACOS_VERSIONS = [
17+
DISABLED_MACOS_VERSIONS = T.let([
1518
:yosemite,
16-
].freeze
17-
DEPRECATED_MACOS_VERSIONS = [].freeze
19+
].freeze, T::Array[Symbol])
20+
DEPRECATED_MACOS_VERSIONS = T.let([].freeze, T::Array[Symbol])
1821

1922
def initialize(tags = [], comparator: ">=")
2023
@version = begin
@@ -44,10 +47,11 @@ def initialize(tags = [], comparator: ">=")
4447
MacOSVersion.new(HOMEBREW_MACOS_OLDEST_ALLOWED) if comparator == ">="
4548
end
4649

47-
@comparator = comparator
50+
@comparator = T.let(comparator, String)
4851
super(tags.drop(1))
4952
end
5053

54+
sig { returns(T::Boolean) }
5155
def version_specified?
5256
@version.present?
5357
end
@@ -61,20 +65,23 @@ def version_specified?
6165
false
6266
end
6367

68+
sig { returns(MacOSVersion) }
6469
def minimum_version
6570
return MacOSVersion.new(HOMEBREW_MACOS_OLDEST_ALLOWED) if @comparator == "<=" || !version_specified?
6671
return @version.min if @version.respond_to?(:to_ary)
6772

6873
@version
6974
end
7075

76+
sig { returns(MacOSVersion) }
7177
def maximum_version
7278
return MacOSVersion.new(HOMEBREW_MACOS_NEWEST_UNSUPPORTED) if @comparator == ">=" || !version_specified?
7379
return @version.max if @version.respond_to?(:to_ary)
7480

7581
@version
7682
end
7783

84+
sig { params(other: MacOSVersion).returns(T::Boolean) }
7885
def allows?(other)
7986
return true unless version_specified?
8087

@@ -98,6 +105,7 @@ def highest_allowed
98105
end
99106
end
100107

108+
sig { params(type: Symbol).returns(String) }
101109
def message(type: :formula)
102110
return "macOS is required for this software." unless version_specified?
103111

@@ -113,6 +121,8 @@ def message(type: :formula)
113121
EOS
114122
when :cask
115123
"This cask does not run on macOS versions newer than #{@version.pretty_name}."
124+
else
125+
"This does not run on macOS versions newer than #{@version.pretty_name}."
116126
end
117127
else
118128
if @version.respond_to?(:to_ary)
@@ -129,6 +139,7 @@ def ==(other)
129139
end
130140
alias eql? ==
131141

142+
sig { returns(Integer) }
132143
def hash
133144
[super, comparator, version].hash
134145
end
@@ -151,6 +162,7 @@ def display_s
151162
end
152163
end
153164

165+
sig { params(options: T.untyped).returns(String) }
154166
def to_json(options)
155167
comp = @comparator.to_s
156168
return { comp => @version.map(&:to_s) }.to_json(options) if @version.is_a?(Array)

0 commit comments

Comments
 (0)