Giter Club home page Giter Club logo

sorbet-typed's People

Contributors

aaronc81 avatar aisamanra avatar alexevanczuk avatar bdlangton avatar caiofilipemr avatar connorshea avatar danielgilchrist avatar darkdimius avatar dduugg avatar dgodd avatar diego-silva avatar dikond avatar donaldong avatar elliottt avatar frewsxcv avatar ghiculescu avatar hdoan741 avatar hudon avatar iloveitaly avatar jasnow avatar jcoyne avatar jeffcarbs avatar jez avatar morriar avatar okhwaja avatar panthomakos avatar pje avatar ptarjan avatar slike9 avatar wakeless avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sorbet-typed's Issues

VariadicUntypedFunction for a scope with (date = DateTime.now)

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?

Proposal to add a little bit more structure to rbi files

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.

Set up spec for the rbi

There should be a way to test the rbi so that reviewing PR is easier.
I expect it would be:

  • Set up sorbet in this repo
  • Set up structure to add testcase & output from srb tc

When people add rbi, they should also add test cases & expected output

Unable to resolve constant FilterEncryptedAttributes

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.

Type Elem must be re-declared in Bundler::EndpointSpecification

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
                ^^^^^^^^^^^^^^^^^^

Typing for ActionDispatch::ContentSecurityPolicy

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 :)

Open3 method signatures do not allow the optional env hash as first argument

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.

module Open3
sig do
params(
cmd: T.any(String, T::Array[String]),
opts: T::Hash[Symbol, T.untyped],
block: T.proc.params(stdin: IO, stdout: IO, stderr: IO, wait_thr: Thread).void
).returns([IO, IO, IO, Thread])
end
def self.popen3(*cmd, **opts, &block); end

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.

Clarify how to contribute in the README

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.

Issue with bundler.rbi sig

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
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Conflict between activerecord.rbi and sorbet-typed

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.

Type of `Rails.application` should be `YourApp::Application`

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.