version: enable Sorbet type checking#10331
Conversation
|
Review period will end on 2021-01-15 at 22:32:42 UTC. |
Library/Homebrew/os/mac/version.rb
Outdated
There was a problem hiding this comment.
Sorbet question: what does this do and why is it needed?
There was a problem hiding this comment.
It casts T.nilable(Type) to Type, i.e. ensuring something is non-nil.
There was a problem hiding this comment.
What happens if it is nil with or without Sorbet enabled at runtime?
There was a problem hiding this comment.
Without, the >= call will fail; with, a TypeError is raised.
Both should never happen since the macOS major version should always be non-nil.
There was a problem hiding this comment.
Thanks. Is it possible to somehow move logic like this into where major is set/returned instead?
There was a problem hiding this comment.
It's calling Version#major, which definitely can have a nil major version.
There was a problem hiding this comment.
One option is to add an RBI file that says OS::Mac::Version#major will always return a non-nil Token. We can do this since we have this check in #initialize:
raise MacOSVersionError, version unless /\A1\d+(?:\.\d+){0,2}\Z/.match?(version)There was a problem hiding this comment.
One option is to add an RBI file that says
OS::Mac::Version#majorwill always return a non-nilToken
Something like that seems good 👍🏻
|
Review period ended. |
6768893 to
a72515c
Compare
|
Library/Homebrew/version.rb
Outdated
There was a problem hiding this comment.
Could we restrict this perhaps to Token, String, Integer, nil or NullToken? Would ease some other code, too.
There was a problem hiding this comment.
I gave this a go, but I came to the conclusion that adding a type to this would require something like Java's generics where a subclass can specify the type of a parent variable. I don't think Sorbet supports this, though
Edit: I'll give this another try and see where I get
There was a problem hiding this comment.
- Added a type for the
valueparameter for#initializeToken→T.nilable(T.any(String, Integer))NullTokenhas no parameters for#initializeStringToken→StringNumericToken→T.any(String, Integer)(converted toIntegerbyvalue.to_i)
- Added overrides for
attr_reader :valuewith the following return types:NullToken→NilClass(The class for plainnilisNilClass)StringToken→StringNumericToken→Integer
- Replaced
@value = valuewith@value = T.let(value, T.untyped), otherwise we get this error forNullToken(and a similar error forStringTokenandNumericToken):
version.rb:107: Expected NilClass but found T.nilable(T.any(String, Integer)) for method result type https://srb.help/7005
107 | attr_reader :value
^^^^^
Expected NilClass
version.rb:107: Method value has return type NilClass
107 | attr_reader :value
^^^^^^^^^^^^^^^^^^
Got T.nilable(T.any(String, Integer)) originating from:
version.rb:59:
59 | @value = value
^^^^^^
Library/Homebrew/os/mac/version.rb
Outdated
There was a problem hiding this comment.
One option is to add an RBI file that says
OS::Mac::Version#majorwill always return a non-nilToken
Something like that seems good 👍🏻
0c5455b to
32ebc02
Compare
brew stylewith your changes locally?brew typecheckwith your changes locally?brew testswith your changes locally?brew manlocally and committed any changes?Notable changes:
NullVersionsingleton class that extendsVersionVersion::Tokento be an abstract classversion.rbito work around Sorbet limitation with aliasesThere are a lot of
T.untypedbut this is a start, at least