为可以采用 RBS 中的哈希或关键字参数的方法键入签名

问题描述 投票:0回答:1

我正在尝试在 RBS 中为以下方法添加类型检查支持:

      def create_bridge(caps:, url:, http_client: nil)
        Remote::Bridge.new(http_client: http_client, url: url).tap do |bridge|
          bridge.create_session(caps)
        end
      end

可以通过以下方式调用该方法:

      def initialize(bridge: nil, listener: nil, **opts)
        @devtools = nil
        @bidi = nil
        bridge ||= create_bridge(**opts)
        add_extensions(bridge.browser)
        @bridge = listener ? Support::EventFiringBridge.new(bridge, listener) : bridge
      end

我在苏格兰皇家银行宣布:

def create_bridge: (caps: untyped?, url: untyped?, http_client: untyped? ) -> untyped

当我跑陡峭时,会导致以下错误:

lib/selenium/webdriver/common/driver.rb:74:19: [error] More keyword arguments are required: caps, url, http_client
│ Diagnostic ID: Ruby::InsufficientKeywordArguments
│
└         bridge ||= create_bridge(**opts)
                     ~~~~~~~~~~~~~~~~~~~~~

但是,如果我尝试将所有关键字设为可选,我将面临以下错误:

lib/selenium/webdriver/common/driver.rb:315:24: [error] The method parameter is incompatible with the declaration `(?caps: untyped, ?url: untyped, ?http_client: untyped) -> untyped`
│ Diagnostic ID: Ruby::MethodParameterMismatch
│
└       def create_bridge(caps:, url:, http_client: nil)
                          ~~~~~

lib/selenium/webdriver/common/driver.rb:315:31: [error] The method parameter is incompatible with the declaration `(?caps: untyped, ?url: untyped, ?http_client: untyped) -> untyped`
│ Diagnostic ID: Ruby::MethodParameterMismatch
│
└       def create_bridge(caps:, url:, http_client: nil)

如果我尝试重载方法定义也是一样的

您能告诉我添加正确类型签名的最佳方法吗?

非常感谢您抽出时间

ruby selenium-webdriver rbs steep
1个回答
0
投票

在初始化中赋值怎么样?

      def initialize(bridge: nil, listener: nil, **opts)
        @devtools = nil
        @bidi = nil
        caps = opts[:caps]                   # or handle nil case
        url = opts[:url]                     # or handle nil case
        http_client = opts[:http_client]     # or handle nil case
        bridge ||= create_bridge(caps:, url:, http_client:) # explicitly pass attributes
        add_extensions(bridge.browser)
        @bridge = listener ? Support::EventFiringBridge.new(bridge, listener) : bridge
      end
© www.soinside.com 2019 - 2024. All rights reserved.