Sorbet 覆盖来自内核的 URI
我有如下方法
sig do
params(uri: URI).returns(String)
end
def get(uri)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.start
response = http.request_get(uri.path)
response.body
ensure
http&.finish
end
测试方法如下(不使用冰糕)
def test_get_retry
uri = URI('http://localhost:4567')
instance = BookStoreHttpClient.new
begin
instance.get_with_retry(uri)
rescue StandardError
assert(true)
end
end
但冰糕抱怨“方法主机
在URI
上不存在”,但它是一个内核类实际上。
有没有办法告诉 Sorbet 在 sorbet/rbi/hidden-definitions/hidden.rbi
中使用 Kernel::URI 而不是 URI
I have method as below
sig do
params(uri: URI).returns(String)
end
def get(uri)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.start
response = http.request_get(uri.path)
response.body
ensure
http&.finish
end
Test method is like below (does not use sorbet)
def test_get_retry
uri = URI('http://localhost:4567')
instance = BookStoreHttpClient.new
begin
instance.get_with_retry(uri)
rescue StandardError
assert(true)
end
end
But Sorbet complains with "Method host
does not exist on URI
", but it is a Kernel class actually.
Is there a way to tell Sorbet to use Kernel::URI instead of URI in sorbet/rbi/hidden-definitions/hidden.rbi
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Sorbet 是正确的,问题不在于它使用了错误的
URI
。URI
不是一种类型,它是一个包含类型的模块:URI::Generic
、URI::HTTPS
等。Kernel: :URI
也不是一种类型,它是一个函数,返回URI
模块中包含的类型之一的实例。例如:
您应该指定所需的 URI 类型,例如:
或者选择一个子集并使用联合类型:
Sorbet is correct, and the problem is not that it's using the wrong
URI
.URI
is not a type, it's a module that contains types:URI::Generic
,URI::HTTPS
etc.Kernel::URI
is also not a type, it's a function that returns an instance of one of the types contained in theURI
module.For example:
You should either specify the type of URI you want, for example:
Or pick a subset and use a union type: