sorbet-runtime 0.5.5841

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. checksums.yaml +7 -0
  2. data/lib/sorbet-runtime.rb +116 -0
  3. data/lib/types/_types.rb +285 -0
  4. data/lib/types/abstract_utils.rb +50 -0
  5. data/lib/types/boolean.rb +8 -0
  6. data/lib/types/compatibility_patches.rb +95 -0
  7. data/lib/types/configuration.rb +428 -0
  8. data/lib/types/enum.rb +349 -0
  9. data/lib/types/generic.rb +23 -0
  10. data/lib/types/helpers.rb +39 -0
  11. data/lib/types/interface_wrapper.rb +158 -0
  12. data/lib/types/non_forcing_constants.rb +51 -0
  13. data/lib/types/private/abstract/data.rb +36 -0
  14. data/lib/types/private/abstract/declare.rb +48 -0
  15. data/lib/types/private/abstract/hooks.rb +43 -0
  16. data/lib/types/private/abstract/validate.rb +128 -0
  17. data/lib/types/private/casts.rb +22 -0
  18. data/lib/types/private/class_utils.rb +111 -0
  19. data/lib/types/private/decl_state.rb +30 -0
  20. data/lib/types/private/final.rb +51 -0
  21. data/lib/types/private/methods/_methods.rb +460 -0
  22. data/lib/types/private/methods/call_validation.rb +1149 -0
  23. data/lib/types/private/methods/decl_builder.rb +228 -0
  24. data/lib/types/private/methods/modes.rb +16 -0
  25. data/lib/types/private/methods/signature.rb +196 -0
  26. data/lib/types/private/methods/signature_validation.rb +229 -0
  27. data/lib/types/private/mixins/mixins.rb +27 -0
  28. data/lib/types/private/retry.rb +10 -0
  29. data/lib/types/private/runtime_levels.rb +56 -0
  30. data/lib/types/private/sealed.rb +65 -0
  31. data/lib/types/private/types/not_typed.rb +23 -0
  32. data/lib/types/private/types/string_holder.rb +26 -0
  33. data/lib/types/private/types/type_alias.rb +26 -0
  34. data/lib/types/private/types/void.rb +34 -0
  35. data/lib/types/profile.rb +31 -0
  36. data/lib/types/props/_props.rb +161 -0
  37. data/lib/types/props/constructor.rb +40 -0
  38. data/lib/types/props/custom_type.rb +108 -0
  39. data/lib/types/props/decorator.rb +672 -0
  40. data/lib/types/props/errors.rb +8 -0
  41. data/lib/types/props/generated_code_validation.rb +268 -0
  42. data/lib/types/props/has_lazily_specialized_methods.rb +92 -0
  43. data/lib/types/props/optional.rb +81 -0
  44. data/lib/types/props/plugin.rb +37 -0
  45. data/lib/types/props/pretty_printable.rb +107 -0
  46. data/lib/types/props/private/apply_default.rb +170 -0
  47. data/lib/types/props/private/deserializer_generator.rb +165 -0
  48. data/lib/types/props/private/parser.rb +32 -0
  49. data/lib/types/props/private/serde_transform.rb +192 -0
  50. data/lib/types/props/private/serializer_generator.rb +77 -0
  51. data/lib/types/props/private/setter_factory.rb +134 -0
  52. data/lib/types/props/serializable.rb +330 -0
  53. data/lib/types/props/type_validation.rb +111 -0
  54. data/lib/types/props/utils.rb +59 -0
  55. data/lib/types/props/weak_constructor.rb +67 -0
  56. data/lib/types/runtime_profiled.rb +24 -0
  57. data/lib/types/sig.rb +30 -0
  58. data/lib/types/struct.rb +18 -0
  59. data/lib/types/types/attached_class.rb +37 -0
  60. data/lib/types/types/base.rb +151 -0
  61. data/lib/types/types/class_of.rb +38 -0
  62. data/lib/types/types/enum.rb +42 -0
  63. data/lib/types/types/fixed_array.rb +60 -0
  64. data/lib/types/types/fixed_hash.rb +59 -0
  65. data/lib/types/types/intersection.rb +37 -0
  66. data/lib/types/types/noreturn.rb +29 -0
  67. data/lib/types/types/proc.rb +51 -0
  68. data/lib/types/types/self_type.rb +35 -0
  69. data/lib/types/types/simple.rb +33 -0
  70. data/lib/types/types/t_enum.rb +38 -0
  71. data/lib/types/types/type_member.rb +7 -0
  72. data/lib/types/types/type_parameter.rb +23 -0
  73. data/lib/types/types/type_template.rb +7 -0
  74. data/lib/types/types/type_variable.rb +31 -0
  75. data/lib/types/types/typed_array.rb +34 -0
  76. data/lib/types/types/typed_enumerable.rb +161 -0
  77. data/lib/types/types/typed_enumerator.rb +36 -0
  78. data/lib/types/types/typed_hash.rb +43 -0
  79. data/lib/types/types/typed_range.rb +26 -0
  80. data/lib/types/types/typed_set.rb +36 -0
  81. data/lib/types/types/union.rb +56 -0
  82. data/lib/types/types/untyped.rb +29 -0
  83. data/lib/types/utils.rb +217 -0
  84. metadata +223 -0
@@ -0,0 +1,217 @@
1
+ # frozen_string_literal: true
2
+ # typed: true
3
+
4
+ module T::Utils
5
+ # Used to convert from a type specification to a `T::Types::Base`.
6
+ def self.coerce(val)
7
+ if val.is_a?(T::Private::Types::TypeAlias)
8
+ val.aliased_type
9
+ elsif val.is_a?(T::Types::Base)
10
+ val
11
+ elsif val == ::Array
12
+ T::Array[T.untyped]
13
+ elsif val == ::Set
14
+ T::Set[T.untyped]
15
+ elsif val == ::Hash
16
+ T::Hash[T.untyped, T.untyped]
17
+ elsif val == ::Enumerable
18
+ T::Enumerable[T.untyped]
19
+ elsif val == ::Enumerator
20
+ T::Enumerator[T.untyped]
21
+ elsif val == ::Range
22
+ T::Range[T.untyped]
23
+ elsif val.is_a?(Module)
24
+ T::Types::Simple.new(val) # rubocop:disable PrisonGuard/UseOpusTypesShortcut
25
+ elsif val.is_a?(::Array)
26
+ T::Types::FixedArray.new(val) # rubocop:disable PrisonGuard/UseOpusTypesShortcut
27
+ elsif val.is_a?(::Hash)
28
+ T::Types::FixedHash.new(val) # rubocop:disable PrisonGuard/UseOpusTypesShortcut
29
+ elsif val.is_a?(T::Private::Methods::DeclBuilder)
30
+ T::Private::Methods.finalize_proc(val.decl)
31
+ elsif val.is_a?(::T::Enum)
32
+ T::Types::TEnum.new(val) # rubocop:disable PrisonGuard/UseOpusTypesShortcut
33
+ elsif val.is_a?(::String)
34
+ raise "Invalid String literal for type constraint. Must be an #{T::Types::Base}, a " \
35
+ "class/module, or an array. Got a String with value `#{val}`."
36
+ else
37
+ raise "Invalid value for type constraint. Must be an #{T::Types::Base}, a " \
38
+ "class/module, or an array. Got a `#{val.class}`."
39
+ end
40
+ end
41
+
42
+ # Returns the set of all methods (public, protected, private) defined on a module or its
43
+ # ancestors, excluding Object and its ancestors. Overrides of methods from Object (and its
44
+ # ancestors) are included.
45
+ def self.methods_excluding_object(mod)
46
+ # We can't just do mod.instance_methods - Object.instance_methods, because that would leave out
47
+ # any methods from Object that are overridden in mod.
48
+ mod.ancestors.flat_map do |ancestor|
49
+ # equivalent to checking Object.ancestors.include?(ancestor)
50
+ next [] if Object <= ancestor
51
+ ancestor.instance_methods(false) + ancestor.private_instance_methods(false)
52
+ end.uniq
53
+ end
54
+
55
+ # Associates a signature with a forwarder method that matches the signature of the method it
56
+ # forwards to. This is necessary because forwarder methods are often declared with catch-all
57
+ # splat parameters, rather than the exact set of parameters ultimately used by the target method,
58
+ # so they cannot be validated as strictly.
59
+ #
60
+ # The caller of this method must ensure that the forwarder method properly forwards all parameters
61
+ # such that the signature is accurate.
62
+ def self.register_forwarder(from_method, to_method, remove_first_param: false)
63
+ T::Private::Methods.register_forwarder(
64
+ from_method, to_method, remove_first_param: remove_first_param
65
+ )
66
+ end
67
+
68
+ # Returns the signature for the instance method on the supplied module, or nil if it's not found or not typed.
69
+ #
70
+ # @example T::Utils.signature_for_instance_method(MyClass, :my_method)
71
+ def self.signature_for_instance_method(mod, method_name)
72
+ T::Private::Methods.signature_for_method(mod.instance_method(method_name))
73
+ end
74
+
75
+ def self.wrap_method_with_call_validation_if_needed(mod, method_sig, original_method)
76
+ T::Private::Methods::CallValidation.wrap_method_if_needed(mod, method_sig, original_method)
77
+ end
78
+
79
+ # Unwraps all the sigs.
80
+ def self.run_all_sig_blocks
81
+ T::Private::Methods.run_all_sig_blocks
82
+ end
83
+
84
+ # Return the underlying type for a type alias. Otherwise returns type.
85
+ def self.resolve_alias(type)
86
+ case type
87
+ when T::Private::Types::TypeAlias
88
+ type.aliased_type
89
+ else
90
+ type
91
+ end
92
+ end
93
+
94
+ # Give a type which is a subclass of T::Types::Base, determines if the type is a simple nilable type (union of NilClass and something else).
95
+ # If so, returns the T::Types::Base of the something else. Otherwise, returns nil.
96
+ def self.unwrap_nilable(type)
97
+ case type
98
+ when T::Types::Union
99
+ non_nil_types = type.types.reject {|t| t == Nilable::NIL_TYPE}
100
+ if non_nil_types.length == 1
101
+ non_nil_types.first
102
+ else
103
+ nil
104
+ end
105
+ else
106
+ nil
107
+ end
108
+ end
109
+
110
+ # Returns the arity of a method, unwrapping the sig if needed
111
+ def self.arity(method)
112
+ arity = method.arity # rubocop:disable PrisonGuard/NoArity
113
+ return arity if arity != -1 || method.is_a?(Proc)
114
+ sig = T::Private::Methods.signature_for_method(method)
115
+ sig ? sig.method.arity : arity # rubocop:disable PrisonGuard/NoArity
116
+ end
117
+
118
+ # Elide the middle of a string as needed and replace it with an ellipsis.
119
+ # Keep the given number of characters at the start and end of the string.
120
+ #
121
+ # This method operates on string length, not byte length.
122
+ #
123
+ # If the string is shorter than the requested truncation length, return it
124
+ # without adding an ellipsis. This method may return a longer string than
125
+ # the original if the characters removed are shorter than the ellipsis.
126
+ #
127
+ # @param [String] str
128
+ #
129
+ # @param [Fixnum] start_len The length of string before the ellipsis
130
+ # @param [Fixnum] end_len The length of string after the ellipsis
131
+ #
132
+ # @param [String] ellipsis The string to add in place of the elided text
133
+ #
134
+ # @return [String]
135
+ #
136
+ def self.string_truncate_middle(str, start_len, end_len, ellipsis='...')
137
+ return unless str
138
+
139
+ raise ArgumentError.new('must provide start_len') unless start_len
140
+ raise ArgumentError.new('must provide end_len') unless end_len
141
+
142
+ raise ArgumentError.new('start_len must be >= 0') if start_len < 0
143
+ raise ArgumentError.new('end_len must be >= 0') if end_len < 0
144
+
145
+ str = str.to_s
146
+ return str if str.length <= start_len + end_len
147
+
148
+ start_part = str[0...start_len - ellipsis.length]
149
+ end_part = end_len == 0 ? '' : str[-end_len..-1]
150
+
151
+ "#{start_part}#{ellipsis}#{end_part}"
152
+ end
153
+
154
+ def self.lift_enum(enum)
155
+ unless enum.is_a?(T::Types::Enum)
156
+ raise ArgumentError.new("#{enum.inspect} is not a T.enum")
157
+ end
158
+
159
+ classes = enum.values.map(&:class).uniq
160
+ if classes.empty?
161
+ T.untyped
162
+ elsif classes.length > 1
163
+ T::Types::Union.new(classes)
164
+ else
165
+ T::Types::Simple.new(classes.first)
166
+ end
167
+ end
168
+
169
+ module Nilable
170
+ # :is_union_type, T::Boolean: whether the type is an T::Types::Union type
171
+ # :non_nilable_type, Class: if it is an T.nilable type, the corresponding underlying type; otherwise, nil.
172
+ TypeInfo = Struct.new(:is_union_type, :non_nilable_type)
173
+
174
+ NIL_TYPE = T::Utils.coerce(NilClass)
175
+
176
+ def self.get_type_info(prop_type)
177
+ if prop_type.is_a?(T::Types::Union)
178
+ non_nilable_type = T::Utils.unwrap_nilable(prop_type)
179
+ if non_nilable_type && non_nilable_type.is_a?(T::Types::Simple)
180
+ non_nilable_type = non_nilable_type.raw_type
181
+ end
182
+ TypeInfo.new(true, non_nilable_type)
183
+ else
184
+ TypeInfo.new(false, nil)
185
+ end
186
+ end
187
+
188
+ # Get the underlying type inside prop_type:
189
+ # - if the type is A, the function returns A
190
+ # - if the type is T.nilable(A), the function returns A
191
+ def self.get_underlying_type(prop_type)
192
+ type_info = get_type_info(prop_type)
193
+ if type_info.is_union_type
194
+ type_info.non_nilable_type || prop_type
195
+ elsif prop_type.is_a?(T::Types::Simple)
196
+ prop_type.raw_type
197
+ else
198
+ prop_type
199
+ end
200
+ end
201
+
202
+ # The difference between this function and the above function is that the Sorbet type, like T::Types::Simple
203
+ # is preserved.
204
+ def self.get_underlying_type_object(prop_type)
205
+ T::Utils.unwrap_nilable(prop_type) || prop_type
206
+ end
207
+
208
+ def self.is_union_with_nilclass(prop_type)
209
+ case prop_type
210
+ when T::Types::Union
211
+ prop_type.types.include?(NIL_TYPE)
212
+ else
213
+ false
214
+ end
215
+ end
216
+ end
217
+ end
metadata ADDED
@@ -0,0 +1,223 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sorbet-runtime
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.5841
5
+ platform: ruby
6
+ authors:
7
+ - Stripe
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-07-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: mocha
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: concurrent-ruby
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 1.1.5
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 1.1.5
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-byebug
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: parser
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '2.7'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '2.7'
111
+ description: Sorbet's runtime type checking component
112
+ email:
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - lib/sorbet-runtime.rb
118
+ - lib/types/_types.rb
119
+ - lib/types/abstract_utils.rb
120
+ - lib/types/boolean.rb
121
+ - lib/types/compatibility_patches.rb
122
+ - lib/types/configuration.rb
123
+ - lib/types/enum.rb
124
+ - lib/types/generic.rb
125
+ - lib/types/helpers.rb
126
+ - lib/types/interface_wrapper.rb
127
+ - lib/types/non_forcing_constants.rb
128
+ - lib/types/private/abstract/data.rb
129
+ - lib/types/private/abstract/declare.rb
130
+ - lib/types/private/abstract/hooks.rb
131
+ - lib/types/private/abstract/validate.rb
132
+ - lib/types/private/casts.rb
133
+ - lib/types/private/class_utils.rb
134
+ - lib/types/private/decl_state.rb
135
+ - lib/types/private/final.rb
136
+ - lib/types/private/methods/_methods.rb
137
+ - lib/types/private/methods/call_validation.rb
138
+ - lib/types/private/methods/decl_builder.rb
139
+ - lib/types/private/methods/modes.rb
140
+ - lib/types/private/methods/signature.rb
141
+ - lib/types/private/methods/signature_validation.rb
142
+ - lib/types/private/mixins/mixins.rb
143
+ - lib/types/private/retry.rb
144
+ - lib/types/private/runtime_levels.rb
145
+ - lib/types/private/sealed.rb
146
+ - lib/types/private/types/not_typed.rb
147
+ - lib/types/private/types/string_holder.rb
148
+ - lib/types/private/types/type_alias.rb
149
+ - lib/types/private/types/void.rb
150
+ - lib/types/profile.rb
151
+ - lib/types/props/_props.rb
152
+ - lib/types/props/constructor.rb
153
+ - lib/types/props/custom_type.rb
154
+ - lib/types/props/decorator.rb
155
+ - lib/types/props/errors.rb
156
+ - lib/types/props/generated_code_validation.rb
157
+ - lib/types/props/has_lazily_specialized_methods.rb
158
+ - lib/types/props/optional.rb
159
+ - lib/types/props/plugin.rb
160
+ - lib/types/props/pretty_printable.rb
161
+ - lib/types/props/private/apply_default.rb
162
+ - lib/types/props/private/deserializer_generator.rb
163
+ - lib/types/props/private/parser.rb
164
+ - lib/types/props/private/serde_transform.rb
165
+ - lib/types/props/private/serializer_generator.rb
166
+ - lib/types/props/private/setter_factory.rb
167
+ - lib/types/props/serializable.rb
168
+ - lib/types/props/type_validation.rb
169
+ - lib/types/props/utils.rb
170
+ - lib/types/props/weak_constructor.rb
171
+ - lib/types/runtime_profiled.rb
172
+ - lib/types/sig.rb
173
+ - lib/types/struct.rb
174
+ - lib/types/types/attached_class.rb
175
+ - lib/types/types/base.rb
176
+ - lib/types/types/class_of.rb
177
+ - lib/types/types/enum.rb
178
+ - lib/types/types/fixed_array.rb
179
+ - lib/types/types/fixed_hash.rb
180
+ - lib/types/types/intersection.rb
181
+ - lib/types/types/noreturn.rb
182
+ - lib/types/types/proc.rb
183
+ - lib/types/types/self_type.rb
184
+ - lib/types/types/simple.rb
185
+ - lib/types/types/t_enum.rb
186
+ - lib/types/types/type_member.rb
187
+ - lib/types/types/type_parameter.rb
188
+ - lib/types/types/type_template.rb
189
+ - lib/types/types/type_variable.rb
190
+ - lib/types/types/typed_array.rb
191
+ - lib/types/types/typed_enumerable.rb
192
+ - lib/types/types/typed_enumerator.rb
193
+ - lib/types/types/typed_hash.rb
194
+ - lib/types/types/typed_range.rb
195
+ - lib/types/types/typed_set.rb
196
+ - lib/types/types/union.rb
197
+ - lib/types/types/untyped.rb
198
+ - lib/types/utils.rb
199
+ homepage: https://sorbet.run
200
+ licenses:
201
+ - Apache-2.0
202
+ metadata:
203
+ source_code_uri: https://github.com/sorbet/sorbet
204
+ post_install_message:
205
+ rdoc_options: []
206
+ require_paths:
207
+ - lib
208
+ required_ruby_version: !ruby/object:Gem::Requirement
209
+ requirements:
210
+ - - ">="
211
+ - !ruby/object:Gem::Version
212
+ version: 2.3.0
213
+ required_rubygems_version: !ruby/object:Gem::Requirement
214
+ requirements:
215
+ - - ">="
216
+ - !ruby/object:Gem::Version
217
+ version: '0'
218
+ requirements: []
219
+ rubygems_version: 3.0.3
220
+ signing_key:
221
+ specification_version: 4
222
+ summary: Sorbet runtime
223
+ test_files: []