处理“sorbet-typed”和“gems” RBI 文件之间的冲突

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

Handling conflicts between "sorbet-typed" and "gems" RBI files

问题

我已在Rails代码库中安装了Sorbet,并看到许多以下错误的实例:

sorbet/rbi/sorbet-typed/lib/activerecord/all/activerecord.rbi:958:
Method ActiveRecord::ConnectionAdapters::TableDefinition#column 重新定义时没有匹配的参数计数。期望: 3,得到: 5 https://srb.help/4010
     958 |  def column(
     959 |    name,
     960 |    type,
     961 |    index: nil,
     962 |    default: nil,
     963 |    **options
     964 |  ); end
    sorbet/rbi/gems/activerecord.rbi:5256: 先前的定义
    5256 |  def column(name, type, **options); end

要消除类型错误,我要么必须在其中一个文件中将 typed: true 更改为 typed: false(这会排除一些重叠的类型),要么必须手动进入并删除/注释掉重叠的方法(这很费时且脆弱)。

我想知道是否有一种编程解决方案,我可以说“在冲突的情况下,使用参数更多的方法”或“始终选择Sorbet-typed的定义”。

再次强调,这是在一个Rails代码库中,但我想象这对于手动生成和自动生成的RBI之间的任何冲突都适用。

英文:

I've installed Sorbet in a Rails codebase and am seeing many instances of the following error:

sorbet/rbi/sorbet-typed/lib/activerecord/all/activerecord.rbi:958:
Method ActiveRecord::ConnectionAdapters::TableDefinition#column redefined without matching argument count. Expected: 3, got: 5 https://srb.help/4010
     958 |  def column(
     959 |    name,
     960 |    type,
     961 |    index: nil,
     962 |    default: nil,
     963 |    **options
     964 |  ); end
    sorbet/rbi/gems/activerecord.rbi:5256: Previous definition
    5256 |  def column(name, type, **options); end

To make the type errors go away, I either have to change typed: true to typed: false in one of the files (which excludes some of the types that don't overlap), or I have to manually go in and delete/comment out the overlapping methods (which is time-consuming and brittle).

I'm wondering if there's some sort of programmatic solution, where I can say "in case of conflict, use the method with higher arity" or "always choose the sorbet-typed definition."

Again this is in a Rails codebase but I imagine this would exist for any conflict between the hand-generated and auto-generated RBIs.

答案1

得分: 1

抱歉,你的请求是只返回翻译好的部分,不包括代码。以下是翻译好的部分:

我写了一些这些方法,不幸的是,由于“Shapes”(Sorbet的类型化哈希的实验性实现)不允许可选键,它们无法准确地表示这些方法。因此,许多方法的参数数量 - 尤其是在Rails中的复杂内容 - 在sorbet-typed和sorbet的自动生成文件之间不同。

我一直在使用 srb rbi suggest-typed 自动设置我的存储库中文件的类型信息,它只将这些标记为false。据我所知,即使文件标记为false,仍然会使用sorbet-typed中的类型信息(虽然我应该更仔细地研究一下以验证这一点)。

不幸的是,我目前没有更好的解决方案,但只是使用suggest-typed 对我来说似乎已经足够。

英文:

I wrote some of these methods, unfortunately since "Shapes" (Sorbet's experimental implementation of typeable Hashes) don't allow optional keys, they can't be used to represent these methods accurately. As a result, the arity of a lot of methods - especially complex stuff in Rails - is different between sorbet-typed and sorbet's autogenerated files.

I've just been using srb rbi suggest-typed to automatically set the typedness of files in my repo, and it just marks those as false. As far as I can tell, the type information from sorbet-typed is still used even if the file is marked as false (though I should probably look into that more to verify that that's the case).

Unfortunately I don't have a better solution for the problem at the moment, but just using suggest-typed seems to be sufficient for me so far.

huangapple
  • 本文由 发表于 2020年1月7日 01:00:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616105.html
匿名

发表评论

匿名网友

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

确定