sorbet / sorbet-typed Goto Github PK
View Code? Open in Web Editor NEWA central repository for sharing type definitions for Ruby gems
Home Page: https://sorbet.org/docs/rbi#hand-written-rbis-for-gems
License: Apache License 2.0
A central repository for sharing type definitions for Ruby gems
Home Page: https://sorbet.org/docs/rbi#hand-written-rbis-for-gems
License: Apache License 2.0
I have a rails model which has a scope:
scope :ready, -> (date = DateTime.now) { where('date < ?', date) }
I get the following error:
Expected T.any(T.proc.returns(T.untyped), T.proc.params(arg0: T.proc.returns(T.untyped)).returns(T.untyped), T.proc.params(arg0: T.untyped, arg1: T.untyped).returns(T.untyped), T.proc.params(arg0: T.untyped, arg1: T.untyped, arg2: T.untyped).returns(T.untyped), T.proc.params(arg0: T.untyped, arg1: T.untyped, arg2: T.untyped, arg3: T.untyped).returns(T.untyped), T.proc.params(arg0: T.untyped, arg1: T.untyped, arg2: T.untyped, arg3: T.untyped, arg4: T.untyped).returns(T.untyped), T.proc.params(arg0: T.untyped, arg1: T.untyped, arg2: T.untyped, arg3: T.untyped, arg4: T.untyped, arg5: T.untyped).returns(T.untyped), T.proc.params(arg0: T.untyped, arg1: T.untyped, arg2: T.untyped, arg3: T.untyped, arg4: T.untyped, arg5: T.untyped, arg6: T.untyped).returns(T.untyped), T.proc.params(arg0: T.untyped, arg1: T.untyped, arg2: T.untyped, arg3: T.untyped, arg4: T.untyped, arg5: T.untyped, arg6: T.untyped, arg7: T.untyped).returns(T.untyped), T.proc.params(arg0: T.untyped, arg1: T.untyped, arg2: T.untyped, arg3: T.untyped, arg4: T.untyped, arg5: T.untyped, arg6: T.untyped, arg7: T.untyped, arg8: T.untyped).returns(T.untyped), T.proc.params(arg0: T.untyped, arg1: T.untyped, arg2: T.untyped, arg3: T.untyped, arg4: T.untyped, arg5: T.untyped, arg6: T.untyped, arg7: T.untyped, arg8: T.untyped, arg9: T.untyped).returns(T.untyped)) but found Proc for argument body https://srb.help/7002
44 | scope :ready, -> (date = DateTime.now) { where('date < ?', date) }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The problem is with the definition of the body, which is a VariadicUntypedFunction: https://github.com/sorbet/sorbet-typed/blob/master/lib/activerecord/all/activerecord.rbi#L3
There is no problem with
`scope :ready, -> (date) { where('date < ?', date) }`
I tried adding
VariadicUntypedFunction = T.type_alias do
T.any(
Proc,
.....
Which work -- is that what we want?
I've noticed that prior to #235 that for a given gem all signatures were written in a single file, e.g. activerecord.rbi
. While it works fine for small gems it becomes much disorganised and unreadable when everything is put in one file. I'd like to open up PR that brings a little bit more structure to the activerecord type signatures, similar to the original gem structure:
/activerecord
/all
aggregation.rbi
associations.rbi
...
base.rbi
callbacks.rbi
...
If you like the idea I'd open up a PR down the road.
There should be a way to test the rbi so that reviewing PR is easier.
I expect it would be:
srb tc
When people add rbi, they should also add test cases & expected output
As of a year ago, Stripe stated that tapioca was the appropriate way to generate RBI files. That post indicates that the central repository for tapioca is rbi-central rather than this one.
There isn't any indication in either repo that one or the other of them are recommended.
Are there plans to deprecate this repo in favor of rbi-central?
I'm unable to find a definition for the sole
method from ActiveSupport.
After updating sorbet-typed I'm facing the following errors in typecheck:
sorbet/rbi/sorbet-typed/lib/activerecord/all/activerecord.rbi:253: Unable to resolve constant FilterEncryptedAttributes https://srb.help/5002
253 | extend(::FilterEncryptedAttributes)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
This change was introduced in a627c6b and I'm not sure if it should be there.
ActiveRecord supports providing a class as the argument to a callback: https://guides.rubyonrails.org/active_record_callbacks.html#callback-classes
class PictureFile < ApplicationRecord
after_destroy PictureFileCallbacks
end
But the types for ActiveRecord classes only allow a nilable Symbol as the first argument to callbacks:
When I ran "rake" this morning, I got 33 errors from actionpack_test.rb.
Here is the output: https://gist.github.com/jasnow/8053687016d5f076fe3f0737317f9e0b
I am getting this error after updating sorbet-typed
and I am unsure how to resolve the issue:
./sorbet/rbi/sorbet-typed/lib/bundler/all/bundler.rbi:1218: Type Elem declared by parent T.class_of(Gem::Specification) must be re-declared in T.class_of(Bundler::EndpointSpecification) https://srb.help/5036
1218 |class Bundler::EndpointSpecification < Gem::Specification
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
https://github.com/sorbet/sorbet/tree/16d69282147e20e65490971bf2986ac6fa54de23/rbi/stdlib/gem.rbi#L207: Elem declared in parent here
207 |class Gem::Specification < Gem::BasicSpecification
^^^^^^^^^^^^^^^^^^
Rails apps have a content_security_policy.rb
file that looks something like this:
Rails.application.config.content_security_policy do |policy|
policy.default_src :self, :https
policy.font_src :self, :https, :data
policy.img_src :self, :https, :data
policy.object_src :none
policy.script_src :self, :https, :unsafe_eval
policy.style_src :self, :https, :unsafe_inline
# Allow Webpacker to connect in development
policy.connect_src :self, :https, 'http://localhost:3035', 'ws://localhost:3035' if Rails.env.development?
# Specify URI for violation reports
# policy.report_uri "/csp-violation-report-endpoint"
end
# If you are using UJS then enable automatic nonce generation
Rails.application.config.content_security_policy_nonce_generator = ->(_request) { SecureRandom.base64(16) }
The method signatures for this belong in actionpack.rbi
, but they're a bit complex to figure out. The problem is that they're defined dynamically with define_method
.
The methods are detected by srb init
and look like this in the generated gem rbi:
class ActionDispatch::ContentSecurityPolicy
def apply_mapping(source); end
def apply_mappings(sources); end
def base_uri(*sources); end
def block_all_mixed_content(enabled = nil); end
def build(context = nil, nonce = nil); end
def build_directive(sources, context); end
def build_directives(context, nonce); end
def child_src(*sources); end
def connect_src(*sources); end
def default_src(*sources); end
def directives; end
def font_src(*sources); end
def form_action(*sources); end
def frame_ancestors(*sources); end
def frame_src(*sources); end
def img_src(*sources); end
def initialize; end
def initialize_copy(other); end
def manifest_src(*sources); end
def media_src(*sources); end
def nonce_directive?(directive); end
def object_src(*sources); end
def plugin_types(*types); end
def report_uri(uri); end
def require_sri_for(*types); end
def resolve_source(source, context); end
def sandbox(*values); end
def script_src(*sources); end
def style_src(*sources); end
def upgrade_insecure_requests(enabled = nil); end
def worker_src(*sources); end
end
Unfortunately, none of the methods have any actual documentation in https://api.rubyonrails.org/. As far as I call tell, *sources
is pretty much always an array of either Strings or Symbols.
The CSP module was added in Rails 5.2 and 6.0 only adds prefetch_src
to the directives list.
I hope this is useful to someone, I don't intend to add types for this yet but if someone else wants to, they're welcome to :)
Many methods in the Open3 module allow us to optionally pass a Hash
as the first positional argument (the hash is used to set the environment for the spawned process) e.g.
stdin, stdout, stderr, wait_thr = Open3.popen3([env,] cmd... [, opts])
The type signatures for these methods currently forbid this e.g.
sorbet-typed/lib/ruby/all/open3.rbi
Lines 3 to 11 in 22dd994
I can "fix" this by changing the type of the cmd
from T.any(String, T::Array[String])
to cmd: T.any(String, T::Array[String], T::Hash[String, String])
but that seems wrong to me. Is there a better way to express these method signatures?
I'm happy to attempt a PR for this.
sorbet-typed/lib/actionpack/all/actionpack.rbi
Line 1373 in e2c66cd
This line causes srb rbi update
to segfault on my version of Rails (5.2). ActiveSupport::ParameterFilter was introduced in Rails 6. Happy to help but unsure what the most pragmatic fix would be. Should Rails rbi's just be major version-scoped?
It's currently not entirely clear to me how to contribute types from a gem to sorbet-typed, should I be writing the type signatures by hand, or can I use srb init
in the gem's directory to get all the methods/constants/etc. to start with? And is there a specific file that I should be taking from the gem if I use srb init
?
Essentially, in the "Adding .rbi files" section it seems like there should be a step after #2, and there isn't.
I updated sorbet-typed
lib and run into this issue when running srb tc
. We don't add any special logic to Struct so I assume this comes from Sorbet.
./sorbet/rbi/sorbet-typed/lib/bundler/all/bundler.rbi:3345: Type Elem declared by parent Struct must be declared again http://go/e/5036
3345 |class Bundler::GemHelpers::PlatformMatch < Struct
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The changes from #347 appear to have exposed conflicts between the generated activerecord.rbi and the sorbet-typed files. After this update was pulled into an update we found that the activerecord.rbi
file generated by the gems sorbet command conflicts with definitions with the sorbet-typed version:
sorbet/rbi/sorbet-typed/lib/activerecord/all/activerecord.rbi:566: Method ActiveRecord::Base.around_save redefined without matching argument count. Expected: 1, got: 3 https://srb.help/4010
566 | def self.around_save(
567 | *args,
568 | if: nil,
569 | unless: nil,
570 | &block
571 | ); end
sorbet/rbi/gems/activerecord.rbi:2482: Previous definition
2482 | def self.around_save(*args, &block); end
This is resulting in the activerecord.rbi
file being marked as typed: ignored
and causing sorbet to think that many common activerecord methods (such as find_by
and where
) are not known to the type checker.
In a Rails application the type of Rails.application is actually a subclass of Rails::Application
that is generated when you type rails new
. For example, rails new blog
, Rails.application
will create a class called Blog::Application
defined in config/application.rb
.
The impact of this is that any custom methods you define in application.rb
on Blog::Application
will not be visible to the type checker. If I have config/application.rb
that looks like this:
# typed: true
require_relative 'boot'
require 'rails'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module Blog
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.2
def name
"Brad's awesome blog"
end
end
end
then I can't call Rails.application.name
in a type-safe way. I'm new to sorbet and unsure what the best way is to fix this, open to suggestions / ideas.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.