Sorbet is a fast, powerful type checker designed for Ruby.
Questions tagged [sorbet]
154 questions
13
votes
3 answers
Why isn't "srb tc" finding the "expect" and "eq" methods for my RSpec tests?
I'm trying out Sorbet in an experimental open-source project (ruby_crystal_codemod). I can't figure out how to get the type-checking to work with some RSpec tests in a nested test project. When I run srb tc, I am seeing some type-checking errors…

ndbroadbent
- 13,513
- 3
- 56
- 85
12
votes
2 answers
How to use sorbet type checking with RSpec mocks?
I have a method that that has a sorbet type signature definition.
While trying to mock this method in tests using RSpec I get a type mismatch error.
I'm trying to understand how I can resolve this issue and can add RSpec based tests without…

Rahil Shah
- 183
- 1
- 4
9
votes
1 answer
Can I specify a duck type in method signatures?
Here's example code:
# typed: true
class KeyGetter
sig {params(env_var_name: String).returns(KeyGetter)}
def self.from_env_var(env_var_name)
return Null.new if env_var_name.nil?
return new(env_var_name)
end
def…

Sergio Tulentsev
- 226,338
- 43
- 373
- 367
8
votes
3 answers
Sorbet asking a `sig` for `attr_reader`
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…

Utkarsh Bhimte
- 325
- 2
- 12
6
votes
1 answer
Plugin for vscode
I am trying to test Sorbet on a Rails project.
followed the adoptions guidelines
installed and run the gem sorbet-rails
changed to # typed: true
restarted vscode
but nothing is really happening, I think I am missing a part of the puzzle.
Has…

Adrien
- 322
- 4
- 14
5
votes
0 answers
How can I override Generated Attribute Methods in Ruby on Rails when using Sorbet?
I have a model with a jsonb column. This column accepts only json objects. Sorbet correctly generates the .rbi file containing the following:
module PushedContent::GeneratedAttributeMethods
sig { returns(T.nilable(T.any(T::Array[T.untyped],…

klmiGR
- 51
- 3
5
votes
1 answer
How to define a signature for a hash with attributes in Sorbet?
(Note that this isn't reproducible on sorbet.run, it's only reproducible with a local copy of Sorbet as far as I can tell)
I was hoping I could use the Typed Structs feature to create a method signature where one of the parameters is an options…

Connor Shea
- 795
- 7
- 22
4
votes
1 answer
How to configure Sorbet with rspec?
I have a simple test but the describe keyword is not working in Sorbet tests.
The error I'm receiving on these methods:
Method `describe` does not exist on `T.class_of()`7003
RSpec.describe(Model) do
describe 'my test' do
before(:each)…

Overload119
- 5,124
- 5
- 29
- 34
4
votes
0 answers
LoadError running `bundle exec srb init` in existing rails project
I'm unable to run bundle exec srb init in an existing Rails project. The first errors I see are LoadErrors for gems and things just go downhill from there.
This is a larger project that has been around for a few years. I believe it started on…

existentialmutt
- 446
- 5
- 12
4
votes
1 answer
In sorbet, can you specify that a type is a descendent of a class?
I have a method that returns an object which could be one of many different types of object but which are all part of the same ancestor class. The precise object type is inferred dynamically.
However, I'm confused as to what to put for the return…

Peter Nixey
- 16,187
- 14
- 79
- 133
4
votes
1 answer
How would you do rose memoization with Sorbet?
Trying to annotate this code, the rose memoization (@||=) gives me an error Use of undeclared variable @git_sha.
# typed: strict
# frozen_string_literal: true
module Util
extend T::Sig
sig { returns(String) }
def self.git_sha
@git_sha…

mikker
- 811
- 7
- 20
3
votes
0 answers
Sorbet type-checking freezes in my Rails 6 project with latest sorbet and tapioca versions
We have a Rails 6 project and running into problems getting Sorbet typechecking to work. We have been following the adoption guide and getting stuck at Step 3 with no debug output or errors.
These are the versions we are running in our Rails…

Pritika N
- 31
- 2
3
votes
0 answers
Sorbet LSP sublime text read typed: false
So I'm using LSP and Sorbet with Sublime Text 3. I have an issue in my RSpec files that have typed: false in them. The engine spits out tons of type errors:
5:3 error Method `it` does not exist on `T.class_of()` unknown-source:
6:5 …

SandeliusME
- 802
- 1
- 10
- 19
3
votes
1 answer
Confusion on when to use T.let vs. T.cast
Based on: https://sorbet.org/docs/type-assertions
In my mind, T.cast seems like a more powerful version of T.let. T.cast(expr, type) forces Sorbet to accept expr as type, even if expr cannot be statically verified as type. T.let(expr, type) hints to…

Billy the Kid
- 271
- 2
- 12
3
votes
1 answer
What is the directory structure for adding Sorbet RBIs to a gem?
I have a gem I want to add types to (either by submitting a PR or by forking to use just in my project), but the documentation does not give any more guidance other than to create an /rbi folder.
Thinking out loud, should it mirror the files in lib/…

jaredsmith
- 7,306
- 3
- 20
- 31