Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type checking with Sorbet #1148

Draft
wants to merge 24 commits into
base: master
Choose a base branch
from
Draft

Add type checking with Sorbet #1148

wants to merge 24 commits into from

Conversation

mvidner
Copy link
Member

@mvidner mvidner commented Mar 24, 2021

mvidner and others added 22 commits June 7, 2019 18:24
this only works locally
> srbhist
      1 # typed: 4-strong
     27 # typed: 3-strict
     98 # typed: 2-true
    328 # typed: 1-false
      5 # typed: 0-ignore
      2 # typed: autogenerated
Can be triggered by `sudo mv /usr/bin/systemctl{,.away}`

Test:

```sh
Y2DIR=library/systemd/src ruby -ryast -ryast2/systemd/target -e 'pp Yast2::Systemd::Target.get_default'
```

Before:
/usr/share/YaST2/lib/yast2/systemd/target.rb:83:in `get_default': uninitialized constant #<Class:Yast2::Systemd::Target>::SystemctlError (NameError)

After:
/home/martin/svn/yast-yast2/library/systemd/src/lib/yast2/systemd/target.rb:84:in `get_default': Systemctl command failed: #<OpenStruct exit=127, stderr="sh: /usr/bin/systemctl: No such file or directory\n", stdout="", command=" LANG=C TERM=dumb COLUMNS=1024 /usr/bin/systemctl --plain --full --no-legend --no-pager --no-ask-password get-default"> (Yast2::Systemctl::Error)

Found by sorbet
after SystemcltError bugfix
and srb rbi todo to fix module/class mismatch
class A < described_class is too dynamic for sorbet.
declaring a constant in one block and using it in another is legal Ruby
but confuses sorbet. move them a level up.
(git merge -s ours)

At first I tried to do things the normal way: take the old 2019 sorbet
branch and merge master into it. That meant merge conflicts EVERYWHERE
because sorbet added "# typed" at the top while master deleted
"encoding" at the top. Scratch that.

In 2021 I re-did the earlier work on top of master and then formally
merged the old branch to signify it does the same thing.
You may wonder why we have to do this because it is pretty obvious that a
StageClass.new is of type StageClass. Well this inference works for a
variable (stage = StageClass.new) but not for a Stage constant.
otherwise bundler will find a different bundler and stop
@coveralls
Copy link

coveralls commented Mar 24, 2021

Coverage Status

Coverage decreased (-0.005%) to 40.515% when pulling 542c96e on sorbet into fd5e1e0 on master.

@mvidner
Copy link
Member Author

mvidner commented Mar 24, 2021

@@ -78,6 +80,8 @@ Requires: augeas-lenses
# For converting to/from punycode strings
Requires: sysconfig >= 0.80.0
Requires: rubygem(%{rb_default_ruby_abi}:simpleidn)
# Type checking
BuildRequires: rubygem(%{rb_default_ruby_abi}:sorbet-runtime)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you really want to do type checking during rpm build?

module Yast::ProductProfileClass::YaPI::SubscriptionTools; end
module Yast::Proxy; end
module Yast::SuSEFirewalldClass::FirewallCMDError; end
module Yast::XMLClass::Nokogiri; end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks wrong as nokogiri should be autogenerated, not?

Martin Vidner added 2 commits March 29, 2021 09:09
with new sigs in yast2-ruby-bindings these files can be typechecked
after adding sigs Builtins: foreach regexpmatch, WFM::CallFunction
IMHO this is a design bug in Ruby copied into Sorbet.
A Boolean class should exist in Ruby, or Sorbet should infer it,
without us having to declare it with T.let

Without this:

```
./library/packages/src/modules/PackageKit.rb:52: Changing the type of a variable in a loop is not permitted https://srb.help/7001
    52 |        ret = true if Builtins.regexpmatch(line, "boolean.*true")
                      ^^^^
  Existing variable has type: FalseClass
  Attempting to change type to: TrueClass
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants