0

I need to annotate a method but, for unclear reasons, Sorbet seems not to be able to correctly perform the type analysis.

The code is the following:

module MyModule class Object < BasicObject sig { returns(T.class_of(MyModule::Object)) } def singleton_class class << self self end end end end # rbi file # module MyModule class Object < BasicObject sig { returns(T.class_of(MyModule::Object)) } def singleton_class; end end end 

This causes the error:

Expected `T.class_of(MyModule::Object)` but found `NilClass` for method result type7005 object.rb(linenum, 5): Expected `T.class_of(MyModule::Object)` for result type of method `singleton_class`: object.rb(linenum, 5): Got `NilClass` originating from: Possibly uninitialized (`NilClass`) in: 

Using T.must solves the issue, however, there is a business constraint not to put annotations inside Ruby files.

What's confusing is that type narrowing doesn't work:

 def singleton_class (class << self; self; end) || raise("Error") end 
Left side of `||` condition was always `falsy` Note: If this is intentional, either delete the redundant code or restructure it to use `T.absurd` so that Sorbet can check for exhaustiveness.7006 object.rb(linenum2, 32): This condition was always `falsy` (`NilClass`) object.rb(linenum1, 5): Got `NilClass` originating from: Possibly uninitialized (`NilClass`) in: 

I've tried a few workarounds, but I seem to get absurd errors.

How can I solve this (besides using T.untyped)? Is this a bug?

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.