Questions tagged [sorbet]
Sorbet is a fast, powerful type checker designed for Ruby.
116
questions
0
votes
0
answers
15
views
Sorbet powered gem distribution [duplicate]
When releasing a gem build with Sorbet I'm confused on what should be included. In the sorbet/ folder there's ALOT of stuff for development dependencies like RSpec etc.
So question is: What is ...
0
votes
0
answers
32
views
Integrating sorbet with paper-trail
When I try and add a has_paper_trail on any of my models to include versioning for objects created, Sorbet throws the following error:
app/models/model_name.rb:22: Method `has_paper_trail` does not ...
1
vote
1
answer
18
views
What runtime code can I write to extract the type notations as defined by Sorbet, to display to the user?
Is there a way to inspect the type notations in ruby/sorbet?
I actually want to display those values in a form somewhere and instead of hardcoding in values like "this can be nil or a String"...
0
votes
1
answer
42
views
Sorbet: Too many arguments provided for method Proc0#call
I am trying to pass an argument to Proc0#call but Sorbet fails:
foo do |data|
# Do stuff
end
sig { params(block: T.proc.void).void }
def foo(&block)
x = { foo: :bar }
block.call(x)
end
...
3
votes
0
answers
38
views
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 ...
1
vote
1
answer
36
views
How to handle memoization with nil values with Sorbet?
I have this method:
def current_organization
return @current_organization if defined?(@current_organization)
subdomain = request.subdomain.to_s
return @current_organization = nil if ...
0
votes
0
answers
90
views
Sorbet with monorepo in VS Code
I have a monorepo with 2 separate Ruby on Rails apps and they both share the same "domain" folder which houses all of the ActiveRecord models and both Rails applications can query it for ...
1
vote
2
answers
95
views
How do I reuse a T::Struct structure in another T::Struct
I have the following:
class Coordinate < T::Struct
const :x, Integer
const :y, Integer
end
class ThreeDCoordinate < T::Struct
const :x, Integer
const :y, Integer
const :z, Integer
end
...
1
vote
1
answer
36
views
Why does changing Bundlers installation directory cause Sorbet to fall over?
For reasons to do with my CI setup I've needed to change the location where I install my Ruby gems from the default location to: bundle config set path 'vendor/bundle'
However, as soon as I do so, ...
0
votes
1
answer
26
views
Should I expect Sorbet to perform platform-dependently?
I've run into some trouble when changing the image I've been using for my CI process on CircleCI. I changed the image when I upgraded my version of Ruby.
bundle exec srb tc is running fine locally ...
1
vote
0
answers
74
views
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 { ...
2
votes
1
answer
47
views
Sorbet overrides URI from Kernel
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....
1
vote
1
answer
58
views
Can invalid signature errors ever trigger at runtime if code has been statically checked by Sorbet?
I was looking through the list of Sorbet runtime errors from the docs. It seems to me that "Errors from invalid sig procs" and "Errors from invalid sigs" would be caught by the ...
2
votes
1
answer
191
views
How can I resolve sorbet error: "Constants must have type annotations with T.let when specifying # typed: strict"?
This is similar to my question in How can I resolve sorbet error: "Use of undeclared variable"?, but for constants.
I am experimenting with adding sorbet type information to my gem, pdf-...
1
vote
1
answer
121
views
Resolving "code is unreachable" in sorbet
I maintain the pdf-reader ruby gem, and I'm experimenting with static type checking via sorbet.
I have a particular source file that's currently typed: false, and I'd like to change it to typed: true. ...
2
votes
1
answer
182
views
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 ...
1
vote
1
answer
341
views
How can I resolve sorbet error: "Use of undeclared variable"?
I am experimenting with adding sorbet type information to my gem, pdf-reader. I don't want sorbet to be a runtime dependency for the gem, so all type annotations are in an external file in the rbi/ ...
0
votes
1
answer
32
views
Can I use Sorbet without sig in source code?
I would like to use Sorbet in my Ruby projects at work. As I want to make the process as smooth as possible, I'd like to know if it is possible to add static type checking only using RBI files in ...
0
votes
0
answers
63
views
Sorbet setup results in tc complaining about rbi file?
Following the setup instructions here I get the following when I eventually run bundle exec srb tc
sorbet/rbi/sorbet-typed/lib/rspec-core/all/rspec-core.rbi:1638: Unable to resolve constant ...
1
vote
1
answer
216
views
How should I manage sorbet errors in a gem where type info must be in rbi files and not inline?
I maintain the pdf-reader ruby gem and I'm using it to experiment with sorbet. I have no prior experience with sorbet.
I'd like to use types to improve the development experience, and distribute the ...
2
votes
1
answer
293
views
How to dynamically define `prop`s on a `T::Struct` in sorbet at runtime?
I have a struct that is defined as:
# typed: true
require 'sorbet-runtime'
class MyStruct < T::Struct
MyPropType = T.type_alias { T::Hash[Symbol, Class] }
class << self
extend T::...
0
votes
1
answer
241
views
Using sorbet interface abstraction on singleton methods
I love the sorbet interface feature!
And in the sorbet documentation there is a paragraph of making singleton methods abstract. This seems like a great feature for deserialization and migrations (...
0
votes
1
answer
226
views
Sorbet generics question - method with type variable in parameter position
I'm trying to get this Sorbet code working (here on Sorbet playground):
# typed: true
extend T::Sig
extend T::Generic
class Foo
extend T::Sig
extend T::Generic
TypeParam = type_member
end
...
1
vote
1
answer
157
views
Sorbet variable array length
I'm just starting to play around with the sorbet gem. I have a method that expects and returns an array of objects. The thing is, the length of the array is varied. How can I type check the method? I ...
0
votes
1
answer
127
views
Recursive type_alias in sorbet
How would one declare a type alias that can contain a reference to itself? For example to mark a function returning a JSON-like structure.
Trying:
Foo = T.type_alias { T.any(Integer, T::Array[Foo]) }
...
2
votes
1
answer
116
views
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.
...
0
votes
1
answer
141
views
Sorbet `srb init` fails with Kernel#exit due to conflict with yard gem
We're starting to use Sorbet at work and srb init throws an error requiring yard. Files still generated and seemed to work fine until srb tc started to produce errors like the following:
sorbet/rbi/...
2
votes
1
answer
404
views
Tapioca not generating correct RBI file for Rails model
I'm migrating the way I generate RBI files from Sorbet to Tapioca. However, I am getting a recurring problem across a number of the RBI files for my Rails models. The RBI files seem to be tripping up ...
0
votes
1
answer
188
views
Ruby Sorbet Hash Typecheck
I want to understand why Sorbet does not complain for this example: Example:
sig {params(x: T::Hash[String, String]).void}
def foo(x)
x.each do |k, v|
puts "key = #{k}, value = #{v}"
...
1
vote
1
answer
481
views
Serializing and Deserializing of Sorbet T::Struct
I recently started playing around with Sorbet and stumbled upon T::Struct which seems pretty useful for type safe value only objects.
One example could be:
class Order < T::Struct
prop :order_id, ...
1
vote
2
answers
187
views
How do I use `srb rbi suggest-typed` without downgrading sigils?
The Sorbet typechecker for Ruby has a utility, srb rbi suggest-typed, which will automatically update the # typed: sigils on each file. However, as the Sorbet docs explain, “currently, the suggestion ...
1
vote
0
answers
60
views
Sorbet: check for a class that includes a target module
Sorbet's T.class_of can be used to match descendant classes, but not classes that include a module:
module X; end
class A; end
class B < A; include X; end
B.ancestors # => [B, X, A, Object, PP::...
3
votes
2
answers
422
views
Sorbet not finding `has_many` association methods
I have a Rails model which has_many items:
class Plan < ApplicationRecord
extend T::Sig
has_many :items, dependent: :destroy
before_save do
# hyper simple test method to illustrat ...
1
vote
1
answer
592
views
Sorbet can't find `validates` method when including `ActiveModel::Model`
I'm using ActiveModel to validate some form objects in my Ruby on Rails. A simplified reproducible example looks like this:
# typed: true
class Form
include ActiveModel::Model
# ...
...
1
vote
2
answers
1k
views
Trying to understand this ruby syntax
I am new to ruby and lots of things are confusing me. I believe this specific one is from Sorbet which is some type checking library? not sure. This specific piece of code is right before a method ...
1
vote
0
answers
62
views
Sorbet type-checking on tree class with a custom schema?
I have a custom tree-like class (MyTree) that stores data under hierarchical keys, (similar to filesystem paths). It exposes methods get and set, each of which operate on "."-separated keys. ...
0
votes
0
answers
218
views
How to hint Sorbet with const_get and dynamic parent class?
How can I allow a class's parent to be dynamically defined and pass Sorbet type checking?
For example:
class A; end
class B < Object.const_get("A"); end
This is part of a Rails gem ...
0
votes
1
answer
32
views
Error when accessing singleton class of object (https://srb.help/3001)
When I access the singleton class of an object using the following code
(class << object; self; end)
The srb command raises an error:
class << EXPRESSION is only supported for class <&...
-1
votes
1
answer
25
views
5032 Error code does not exist on the sorbet error reference
https://github.com/sorbet/sorbet/blob/c6e86f50872a60adfba7b7472055dcc6c25e492c/core/errors/resolver.h#L38
This error reference - 5032 for IncludesNonModule does not exist on the website
1
vote
1
answer
64
views
Sorbet fails when using creating an instance of an autoloaded class in rails initializer
In a rails app using sorbet, when you have a method that expects an instance of a type, say Foo. And you need to initialize an instance of Foo in an initializer for the app that persists in memory ...
1
vote
1
answer
197
views
Can sorbet handle private definitions of initialize() when called with new?
I am getting an error calling the initialize method:
# typed: true
class A
extend T::Sig
sig {params(x: Integer).void}
private def initialize(x)
end
end
def main
A.new(91)
end
Here is ...
1
vote
1
answer
507
views
Sorbet cannot resolve constant even though it is defined
Given:
# typed: true
module X
class Y
end
end
module X
class X
def y
X::Y
end
end
end
Sorbet gives error :
editor.rb:6: Unable to resolve constant Y https://srb.help/5002
...
0
votes
1
answer
45
views
Variables declared in test setup must be nilable?
I'm writing a test where I have a variable that I need to declare in the setup do.
typed: strict
frozen_string_literal: true
class BunnyTest
extend(T::Sig)
setup do
@bunny = T.let(Bunny.new, ...
0
votes
1
answer
311
views
how do you type instance variable caching in Sorbet?
I have code that looks like this (playground link):
# typed: strict
class A
extend T::Sig
sig { returns(T::Array[Integer]) }
def compute_expensive
[1, 2, 3]
end
sig { returns(T::...
3
votes
0
answers
566
views
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 ...
1
vote
2
answers
258
views
How to Generate .RBI file for an ActiveModel Class
I have a PORO that I'm using as a value object. I'm adding ActiveModel via include ActiveModel::Model for all of the niceties that that brings me. The problem is that I cannot figure out how to ...
0
votes
1
answer
277
views
Running srb rbi hidden-definitions gives unclear error
Context
Our team recently upgrade to rails 6 (from 5.2). AFAIK, things were working pretty well up to that point.
We have a slightly unconventional file structure so we opted to use the :classic ...
2
votes
0
answers
1k
views
"Method << does not exist on T.class_of(A)" use of def_delegators with sorbet
I've got a code that uses Singleton and Forwardable to delegate some methods to the internal Hash.
class A
include Singleton
extend T::Sig
sig { returns(T::Hash[T.untyped, T.untyped]) }
...
3
votes
1
answer
191
views
Is there an acceptable way to split classes into smaller, composeable mixins (which know about the parent) in Ruby?
I find that some classes in Ruby, especially the User class in Ruby on Rails, have a tendency to become absolutely enormous. That in itself is just about manageagable but the tests files for such ...
1
vote
1
answer
612
views
Why does this Sorbet error count as a dynamic constant reference?
I'm getting the following error in Sorbet:
lib/guardian.rb:24: Dynamic constant references are unsupported https://srb.help/5001
24 | self.class::MIN_AUTH || raise("Minimum auth must be ...