Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorbet asking a `sig` for `attr_reader`

Tags:

ruby

sorbet

Sorbet is showing an error for the attr_reader, but correct me if I am wrong, sigs are required when the function is declared, not called, right?

I have tried going through the documentation but all I got is this note

Note: Many Ruby constructs that look like local variables are actually method calls without parens! Specifically, watch out for attr_reader and zero-argument method definitions.

app/util/hodor.rb:125: This function does not have a `sig` https://sorbet.org/docs/error-reference#7017
     125 |  attr_reader(:collection_name)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
like image 759
Utkarsh Bhimte Avatar asked Jun 19 '19 12:06

Utkarsh Bhimte


1 Answers

The error does not have to do with the attr_reader method itself needing a sig, but with :collection_name. The signature for attr_reader is already known, but the new method it dynamically creates, #collection_name does not have a known sig, and Sorbet expects this to be the place where you give it one.

You can do this to fix it:

sig { returns(String) }
attr_reader(:collection_name)
like image 199
jaredsmith Avatar answered Oct 19 '22 06:10

jaredsmith