Ruby Sorbet即使我已经将其包装在T.unsafe()中,仍然在自动更正一行。

huangapple go评论66阅读模式
英文:

Ruby Sorbet is autocorrecting a line even though I have wrapped it in T.unsafe()

问题

在我的test_helper.rb文件中,我有以下代码:

```ruby
require 'simplecov'
class SimpleCov::Formatter::MergedFormatter
  def format(result)
    simplecov = T.unsafe(SimpleCov::Formatter::HTMLFormatter).new
    T.unsafe(simplecov.format(result))
  end
end

这个 gem 非常旧,没有 .rbi 文件。根据 https://sorbet.org/docs/error-reference#5002 上的文档,我不理解的是:

这是来自一个 gem 吗?Sorbet 不会查看 gem 的源代码。相反,必须有一个与这个 gem 相对应的 *.rbi 文件。尝试找到与这个 gem 相对应的 *.rbi 文件,并在其中搜索常量。

但是这个 gem 没有任何 .rbi 文件。

Sorbet 会将 SimpleCov::Formatter::HTMLFormatter 自动更正为 RuboCop::Formatter::HTMLFormatter,并显示以下错误:无法解析常量 HTMLFormatter https://srb.help/5002

但是这个对象是完全正确的,而且我已经将它包装在 T.unsafe(...) 中,它仍然会自动更正。

期望的结果是:如何绕过这个问题?(我还尝试在文件顶部设置 # typed: false,但是 Sorbet 仍然在这一行上报错)


<details>
<summary>英文:</summary>

I have this code in my test_helper.rb

require 'simplecov'
class SimpleCov::Formatter::MergedFormatter
def format(result)
simplecov = T.unsafe(SimpleCov::Formatter::HTMLFormatter).new
T.unsafe(simplecov.format(result))
end
end


this gem is very old and does not have an `.rbi` file. What I do not understand by the documentation at https://sorbet.org/docs/error-reference#5002 

&gt; Is it coming from a gem? Sorbet does not look through the gem’s source
&gt; code. Instead, there must be an *.rbi file for this gem. Try finding
&gt; the *.rbi corresponding to this gem, and searching through it for the
&gt; constant.

but this gem does not have any rbi files. 



Sorbet autocorrects `SimpleCov::Formatter::HTMLFormatter` to `RuboCop::Formatter::HTMLFormatter`

with this error: Unable to resolve constant HTMLFormatter https://srb.help/5002

But the object is perfectly fine, and also I&#39;ve wrapped it in ` T.unsafe(...)` and it still autocorrects. 

expected result: How can I escape hatch this? (I also tried setting `# typed: false` at the top of the file but sorbet still complains on this line)


</details>


# 答案1
**得分**: 1

Sorbet采用的最低入门要求是解析所有常量(即类/模块)。在这种情况下,Sorbet对常量`SimpleCov::Formatter::HTMLFormatter`毫无了解,因此无论是否使用`T.unsafe`,都无法处理它。

我希望你正在使用建议的基于Tapioca的工作流程来生成你的RBI文件,在这种情况下,simplecov的RBI文件应该已经为你生成了。你不需要从其他地方找到一个`.rbi`文件来使用这个gem,Tapioca会从gem本身生成它。

如果你使用不同的工作流程,你可以轻松地创建一个RBI文件(最好放在`sorbet/rbi`文件夹下),来定义所需的常量和方法以解除阻塞。像下面这样的内容应该可以很好地适用于你的示例:
```ruby
class SimpleCov::Formatter::HTMLFormatter
  def format(result); end
end
英文:

The minimum bar of entry for Sorbet adoption is for all of your constants (i.e. classes/modules) to be resolved. In this case, Sorbet has no idea about the constant SimpleCov::Formatter::HTMLFormatter so it can't deal with it, regardless of T.unsafe or not.

I am hoping that you are using the suggested Tapioca based workflow to generate your RBI files, in which case, the RBI file for simplecov should have been generated for you. You don't need to find an .rbi file for the gem from somewhere, Tapioca will generate it from the gem itself.

If you are using a different workflow, you can easily just create an RBI file (preferably under sorbet/rbi folder) that defines the constant and any methods that you need on it to get unblocked. Something like the following should work fine for your example:

class SimpleCov::Formatter::HTMLFormatter
  def format(result); end
end

huangapple
  • 本文由 发表于 2023年6月29日 02:33:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76575845.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定