sorbet-runtime 0.5.12155 → 0.6.12632

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 (41) hide show
  1. checksums.yaml +4 -4
  2. data/lib/types/_types.rb +1 -1
  3. data/lib/types/boolean.rb +1 -1
  4. data/lib/types/configuration.rb +4 -14
  5. data/lib/types/enum.rb +35 -35
  6. data/lib/types/non_forcing_constants.rb +1 -1
  7. data/lib/types/private/abstract/data.rb +1 -1
  8. data/lib/types/private/methods/_methods.rb +21 -5
  9. data/lib/types/private/methods/call_validation.rb +3 -16
  10. data/lib/types/private/methods/decl_builder.rb +6 -1
  11. data/lib/types/private/methods/signature.rb +8 -8
  12. data/lib/types/private/methods/signature_validation.rb +42 -3
  13. data/lib/types/private/mixins/mixins.rb +1 -1
  14. data/lib/types/private/runtime_levels.rb +1 -1
  15. data/lib/types/private/sealed.rb +5 -5
  16. data/lib/types/props/_props.rb +6 -9
  17. data/lib/types/props/constructor.rb +1 -1
  18. data/lib/types/props/custom_type.rb +5 -5
  19. data/lib/types/props/decorator.rb +127 -64
  20. data/lib/types/props/generated_code_validation.rb +4 -4
  21. data/lib/types/props/has_lazily_specialized_methods.rb +13 -13
  22. data/lib/types/props/optional.rb +2 -2
  23. data/lib/types/props/pretty_printable.rb +6 -6
  24. data/lib/types/props/private/apply_default.rb +15 -15
  25. data/lib/types/props/private/deserializer_generator.rb +10 -6
  26. data/lib/types/props/private/serde_transform.rb +8 -8
  27. data/lib/types/props/private/serializer_generator.rb +9 -5
  28. data/lib/types/props/private/setter_factory.rb +4 -4
  29. data/lib/types/props/serializable.rb +14 -19
  30. data/lib/types/props/type_validation.rb +5 -5
  31. data/lib/types/props/utils.rb +1 -1
  32. data/lib/types/props/weak_constructor.rb +3 -3
  33. data/lib/types/sig.rb +2 -2
  34. data/lib/types/struct.rb +2 -2
  35. data/lib/types/types/base.rb +6 -6
  36. data/lib/types/types/fixed_array.rb +1 -1
  37. data/lib/types/types/fixed_hash.rb +6 -6
  38. data/lib/types/types/intersection.rb +2 -2
  39. data/lib/types/types/t_enum.rb +2 -0
  40. data/lib/types/types/union.rb +7 -7
  41. metadata +3 -3
@@ -12,8 +12,8 @@ module T::Props::PrettyPrintable
12
12
  pp.group(1, "<#{klass.inspect_class_with_decoration(self)}", ">") do
13
13
  klass.all_props.sort.each do |prop|
14
14
  pp.breakable
15
- val = klass.get(self, prop)
16
15
  rules = klass.prop_rules(prop)
16
+ val = klass.get(self, prop, rules)
17
17
  pp.text("#{prop}=")
18
18
  if (custom_inspect = rules[:inspect])
19
19
  inspected = if T::Utils.arity(custom_inspect) == 1
@@ -22,8 +22,8 @@ module T::Props::PrettyPrintable
22
22
  custom_inspect.call(val, {multiline: multiline})
23
23
  end
24
24
  pp.text(inspected.nil? ? "nil" : inspected)
25
- elsif rules[:sensitivity] && !rules[:sensitivity].empty? && !val.nil?
26
- pp.text("<REDACTED #{rules[:sensitivity].join(', ')}>")
25
+ elsif (sensitivity = rules[:sensitivity]) && !sensitivity.empty? && !val.nil?
26
+ pp.text("<REDACTED #{sensitivity.join(', ')}>")
27
27
  else
28
28
  val.pretty_print(pp)
29
29
  end
@@ -49,21 +49,21 @@ module T::Props::PrettyPrintable
49
49
  module DecoratorMethods
50
50
  extend T::Sig
51
51
 
52
- sig {params(key: Symbol).returns(T::Boolean).checked(:never)}
52
+ sig { params(key: Symbol).returns(T::Boolean).checked(:never) }
53
53
  def valid_rule_key?(key)
54
54
  super || key == :inspect
55
55
  end
56
56
 
57
57
  # Overridable method to specify how the first part of a `pretty_print`d object's class should look like
58
58
  # NOTE: This is just to support Stripe's `PrettyPrintableModel` case, and not recommended to be overridden
59
- sig {params(instance: T::Props::PrettyPrintable).returns(String)}
59
+ sig { params(instance: T::Props::PrettyPrintable).returns(String).checked(:never) }
60
60
  def inspect_class_with_decoration(instance)
61
61
  T.unsafe(instance).class.to_s
62
62
  end
63
63
 
64
64
  # Overridable method to add anything that is not a prop
65
65
  # NOTE: This is to support cases like Serializable's `@_extra_props`, and Stripe's `PrettyPrintableModel#@_deleted`
66
- sig {params(instance: T::Props::PrettyPrintable, pp: T.any(PrettyPrint, PP::SingleLine)).void}
66
+ sig { params(instance: T::Props::PrettyPrintable, pp: T.any(PrettyPrint, PP::SingleLine)).void.checked(:never) }
67
67
  def pretty_print_extra(instance, pp); end
68
68
  end
69
69
  end
@@ -9,28 +9,28 @@ module T::Props
9
9
  abstract!
10
10
 
11
11
  # checked(:never) - O(object construction x prop count)
12
- sig {returns(SetterFactory::SetterProc).checked(:never)}
12
+ sig { returns(SetterFactory::SetterProc).checked(:never) }
13
13
  attr_reader :setter_proc
14
14
 
15
15
  # checked(:never) - We do this with `T.let` instead
16
- sig {params(accessor_key: Symbol, setter_proc: SetterFactory::SetterProc).void.checked(:never)}
16
+ sig { params(accessor_key: Symbol, setter_proc: SetterFactory::SetterProc).void.checked(:never) }
17
17
  def initialize(accessor_key, setter_proc)
18
18
  @accessor_key = T.let(accessor_key, Symbol)
19
19
  @setter_proc = T.let(setter_proc, SetterFactory::SetterProc)
20
20
  end
21
21
 
22
22
  # checked(:never) - O(object construction x prop count)
23
- sig {abstract.returns(T.untyped).checked(:never)}
23
+ sig { abstract.returns(T.untyped).checked(:never) }
24
24
  def default; end
25
25
 
26
26
  # checked(:never) - O(object construction x prop count)
27
- sig {abstract.params(instance: T.all(T::Props::Optional, Object)).void.checked(:never)}
27
+ sig { abstract.params(instance: T.all(T::Props::Optional, Object)).void.checked(:never) }
28
28
  def set_default(instance); end
29
29
 
30
30
  NO_CLONE_TYPES = T.let([TrueClass, FalseClass, NilClass, Symbol, Numeric, T::Enum].freeze, T::Array[Module])
31
31
 
32
32
  # checked(:never) - Rules hash is expensive to check
33
- sig {params(cls: Module, rules: T::Hash[Symbol, T.untyped]).returns(T.nilable(ApplyDefault)).checked(:never)}
33
+ sig { params(cls: Module, rules: T::Hash[Symbol, T.untyped]).returns(T.nilable(ApplyDefault)).checked(:never) }
34
34
  def self.for(cls, rules)
35
35
  accessor_key = rules.fetch(:accessor_key)
36
36
  setter = rules.fetch(:setter_proc)
@@ -67,7 +67,7 @@ module T::Props
67
67
  abstract!
68
68
 
69
69
  # checked(:never) - We do this with `T.let` instead
70
- sig {params(default: BasicObject, accessor_key: Symbol, setter_proc: SetterFactory::SetterProc).void.checked(:never)}
70
+ sig { params(default: BasicObject, accessor_key: Symbol, setter_proc: SetterFactory::SetterProc).void.checked(:never) }
71
71
  def initialize(default, accessor_key, setter_proc)
72
72
  # FIXME: Ideally we'd check here that the default is actually a valid
73
73
  # value for this field, but existing code relies on the fact that we don't.
@@ -80,7 +80,7 @@ module T::Props
80
80
  end
81
81
 
82
82
  # checked(:never) - O(object construction x prop count)
83
- sig {override.params(instance: T.all(T::Props::Optional, Object)).void.checked(:never)}
83
+ sig { override.params(instance: T.all(T::Props::Optional, Object)).void.checked(:never) }
84
84
  def set_default(instance)
85
85
  instance.instance_variable_set(@accessor_key, default)
86
86
  end
@@ -88,13 +88,13 @@ module T::Props
88
88
 
89
89
  class ApplyPrimitiveDefault < ApplyFixedDefault
90
90
  # checked(:never) - O(object construction x prop count)
91
- sig {override.returns(T.untyped).checked(:never)}
91
+ sig { override.returns(T.untyped).checked(:never) }
92
92
  attr_reader :default
93
93
  end
94
94
 
95
95
  class ApplyComplexDefault < ApplyFixedDefault
96
96
  # checked(:never) - O(object construction x prop count)
97
- sig {override.returns(T.untyped).checked(:never)}
97
+ sig { override.returns(T.untyped).checked(:never) }
98
98
  def default
99
99
  T::Props::Utils.deep_clone_object(@default)
100
100
  end
@@ -105,13 +105,13 @@ module T::Props
105
105
  # `some_empty_array.dup`
106
106
  class ApplyEmptyArrayDefault < ApplyDefault
107
107
  # checked(:never) - O(object construction x prop count)
108
- sig {override.params(instance: T.all(T::Props::Optional, Object)).void.checked(:never)}
108
+ sig { override.params(instance: T.all(T::Props::Optional, Object)).void.checked(:never) }
109
109
  def set_default(instance)
110
110
  instance.instance_variable_set(@accessor_key, [])
111
111
  end
112
112
 
113
113
  # checked(:never) - O(object construction x prop count)
114
- sig {override.returns(T::Array[T.untyped]).checked(:never)}
114
+ sig { override.returns(T::Array[T.untyped]).checked(:never) }
115
115
  def default
116
116
  []
117
117
  end
@@ -122,13 +122,13 @@ module T::Props
122
122
  # `some_empty_hash.dup`
123
123
  class ApplyEmptyHashDefault < ApplyDefault
124
124
  # checked(:never) - O(object construction x prop count)
125
- sig {override.params(instance: T.all(T::Props::Optional, Object)).void.checked(:never)}
125
+ sig { override.params(instance: T.all(T::Props::Optional, Object)).void.checked(:never) }
126
126
  def set_default(instance)
127
127
  instance.instance_variable_set(@accessor_key, {})
128
128
  end
129
129
 
130
130
  # checked(:never) - O(object construction x prop count)
131
- sig {override.returns(T::Hash[T.untyped, T.untyped]).checked(:never)}
131
+ sig { override.returns(T::Hash[T.untyped, T.untyped]).checked(:never) }
132
132
  def default
133
133
  {}
134
134
  end
@@ -153,7 +153,7 @@ module T::Props
153
153
  end
154
154
 
155
155
  # checked(:never) - O(object construction x prop count)
156
- sig {override.params(instance: T.all(T::Props::Optional, Object)).void.checked(:never)}
156
+ sig { override.params(instance: T.all(T::Props::Optional, Object)).void.checked(:never) }
157
157
  def set_default(instance)
158
158
  # Use the actual setter to validate the factory returns a legitimate
159
159
  # value every time
@@ -161,7 +161,7 @@ module T::Props
161
161
  end
162
162
 
163
163
  # checked(:never) - O(object construction x prop count)
164
- sig {override.returns(T.untyped).checked(:never)}
164
+ sig { override.returns(T.untyped).checked(:never) }
165
165
  def default
166
166
  @class.class_exec(&@factory)
167
167
  end
@@ -16,6 +16,8 @@ module T::Props
16
16
  module DeserializerGenerator
17
17
  extend T::Sig
18
18
 
19
+ CAN_USE_SYMBOL_NAME = T.let(RUBY_VERSION >= "3.3.0", T::Boolean)
20
+
19
21
  # Generate a method that takes a T::Hash[String, T.untyped] representing
20
22
  # serialized props, sets instance variables for each prop found in the
21
23
  # input, and returns the count of we props set (which we can use to check
@@ -29,19 +31,21 @@ module T::Props
29
31
  .checked(:never)
30
32
  end
31
33
  def self.generate(props, defaults)
32
- stored_props = props.reject {|_, rules| rules[:dont_store]}
33
- parts = stored_props.map do |prop, rules|
34
+ parts = props.filter_map do |prop, rules|
35
+ next if rules[:dont_store]
36
+
34
37
  # All of these strings should already be validated (directly or
35
38
  # indirectly) in `validate_prop_name`, so we don't bother with a nice
36
39
  # error message, but we double check here to prevent a refactoring
37
40
  # from introducing a security vulnerability.
38
- raise unless T::Props::Decorator::SAFE_NAME.match?(prop.to_s)
41
+ raise unless T::Props::Decorator::SAFE_NAME.match?(CAN_USE_SYMBOL_NAME ? prop.name : prop.to_s)
39
42
 
40
43
  hash_key = rules.fetch(:serialized_form)
41
44
  raise unless T::Props::Decorator::SAFE_NAME.match?(hash_key)
42
45
 
43
- ivar_name = rules.fetch(:accessor_key).to_s
44
- raise unless ivar_name.start_with?('@') && T::Props::Decorator::SAFE_NAME.match?(ivar_name[1..-1])
46
+ key = rules.fetch(:accessor_key)
47
+ ivar_name = CAN_USE_SYMBOL_NAME ? key.name : key.to_s
48
+ raise unless ivar_name.start_with?('@') && T::Props::Decorator::SAFE_ACCESSOR_KEY_NAME.match?(ivar_name)
45
49
 
46
50
  transformation = SerdeTransform.generate(
47
51
  T::Utils::Nilable.get_underlying_type_object(rules.fetch(:type_object)),
@@ -90,7 +94,7 @@ module T::Props
90
94
 
91
95
  <<~RUBY
92
96
  def __t_props_generated_deserialize(hash)
93
- found = #{stored_props.size}
97
+ found = #{parts.size}
94
98
  #{parts.join("\n\n")}
95
99
  found
96
100
  end
@@ -10,7 +10,7 @@ module T::Props
10
10
  private_constant :Serialize
11
11
  class Deserialize; end
12
12
  private_constant :Deserialize
13
- ModeType = T.type_alias {T.any(Serialize, Deserialize)}
13
+ ModeType = T.type_alias { T.any(Serialize, Deserialize) }
14
14
  private_constant :ModeType
15
15
 
16
16
  module Mode
@@ -63,7 +63,7 @@ module T::Props
63
63
  end
64
64
  when T::Types::Simple
65
65
  raw = type.raw_type
66
- if NO_TRANSFORM_TYPES.any? {|cls| raw <= cls}
66
+ if NO_TRANSFORM_TYPES.any? { |cls| raw <= cls }
67
67
  nil
68
68
  elsif raw <= Float
69
69
  case mode
@@ -99,7 +99,7 @@ module T::Props
99
99
  else
100
100
  "#{varname}.nil? ? nil : #{inner}"
101
101
  end
102
- elsif type.types.all? {|t| generate(t, mode, varname).nil?}
102
+ elsif type.types.all? { |t| generate(t, mode, varname).nil? }
103
103
  # Handle, e.g., T::Boolean
104
104
  nil
105
105
  else
@@ -122,8 +122,8 @@ module T::Props
122
122
  # NB: This deliberately does include `nil`, which means we know we
123
123
  # don't need to do any transforming.
124
124
  inner_known = type.types
125
- .map {|t| generate(t, mode, varname)}
126
- .reject {|t| t == dynamic_fallback}
125
+ .map { |t| generate(t, mode, varname) }
126
+ .reject { |t| t == dynamic_fallback }
127
127
  .uniq
128
128
 
129
129
  if inner_known.size != 1
@@ -151,7 +151,7 @@ module T::Props
151
151
  end
152
152
  end
153
153
 
154
- sig {params(varname: String, type: Module, mode: ModeType).returns(String).checked(:never)}
154
+ sig { params(varname: String, type: Module, mode: ModeType).returns(String).checked(:never) }
155
155
  private_class_method def self.handle_serializable_subtype(varname, type, mode)
156
156
  case mode
157
157
  when Serialize
@@ -164,7 +164,7 @@ module T::Props
164
164
  end
165
165
  end
166
166
 
167
- sig {params(varname: String, type: Module, mode: ModeType).returns(String).checked(:never)}
167
+ sig { params(varname: String, type: Module, mode: ModeType).returns(String).checked(:never) }
168
168
  private_class_method def self.handle_custom_type(varname, type, mode)
169
169
  case mode
170
170
  when Serialize
@@ -177,7 +177,7 @@ module T::Props
177
177
  end
178
178
  end
179
179
 
180
- sig {params(type: Module).returns(T.nilable(String)).checked(:never)}
180
+ sig { params(type: Module).returns(T.nilable(String)).checked(:never) }
181
181
  private_class_method def self.module_name(type)
182
182
  T::Configuration.module_name_mangler.call(type)
183
183
  end
@@ -16,6 +16,8 @@ module T::Props
16
16
  module SerializerGenerator
17
17
  extend T::Sig
18
18
 
19
+ CAN_USE_SYMBOL_NAME = T.let(RUBY_VERSION >= "3.3.0", T::Boolean)
20
+
19
21
  sig do
20
22
  params(
21
23
  props: T::Hash[Symbol, T::Hash[Symbol, T.untyped]],
@@ -24,19 +26,21 @@ module T::Props
24
26
  .checked(:never)
25
27
  end
26
28
  def self.generate(props)
27
- stored_props = props.reject {|_, rules| rules[:dont_store]}
28
- parts = stored_props.map do |prop, rules|
29
+ parts = props.filter_map do |prop, rules|
30
+ next if rules[:dont_store]
31
+
29
32
  # All of these strings should already be validated (directly or
30
33
  # indirectly) in `validate_prop_name`, so we don't bother with a nice
31
34
  # error message, but we double check here to prevent a refactoring
32
35
  # from introducing a security vulnerability.
33
- raise unless T::Props::Decorator::SAFE_NAME.match?(prop.to_s)
36
+ raise unless T::Props::Decorator::SAFE_NAME.match?(CAN_USE_SYMBOL_NAME ? prop.name : prop.to_s)
34
37
 
35
38
  hash_key = rules.fetch(:serialized_form)
36
39
  raise unless T::Props::Decorator::SAFE_NAME.match?(hash_key)
37
40
 
38
- ivar_name = rules.fetch(:accessor_key).to_s
39
- raise unless ivar_name.start_with?('@') && T::Props::Decorator::SAFE_NAME.match?(ivar_name[1..-1])
41
+ key = rules.fetch(:accessor_key)
42
+ ivar_name = CAN_USE_SYMBOL_NAME ? key.name : key.to_s
43
+ raise unless ivar_name.start_with?('@') && T::Props::Decorator::SAFE_ACCESSOR_KEY_NAME.match?(ivar_name)
40
44
 
41
45
  transformed_val = SerdeTransform.generate(
42
46
  T::Utils::Nilable.get_underlying_type_object(rules.fetch(:type_object)),
@@ -6,9 +6,9 @@ module T::Props
6
6
  module SetterFactory
7
7
  extend T::Sig
8
8
 
9
- SetterProc = T.type_alias {T.proc.params(val: T.untyped).void}
10
- ValueValidationProc = T.type_alias {T.proc.params(val: T.untyped).void}
11
- ValidateProc = T.type_alias {T.proc.params(prop: Symbol, value: T.untyped).void}
9
+ SetterProc = T.type_alias { T.proc.params(val: T.untyped).void }
10
+ ValueValidationProc = T.type_alias { T.proc.params(val: T.untyped).void }
11
+ ValidateProc = T.type_alias { T.proc.params(prop: Symbol, value: T.untyped).void }
12
12
 
13
13
  sig do
14
14
  params(
@@ -236,7 +236,7 @@ module T::Props
236
236
  base_message = "Can't set #{klass.name}.#{prop} to #{val.inspect} (instance of #{val.class}) - need a #{type}"
237
237
 
238
238
  pretty_message = "Parameter '#{prop}': #{base_message}\n"
239
- caller_loc = caller_locations.find {|l| !l.to_s.include?('sorbet-runtime/lib/types/props')}
239
+ caller_loc = caller_locations.find { |l| !l.to_s.include?('sorbet-runtime/lib/types/props') }
240
240
  if caller_loc
241
241
  pretty_message += "Caller: #{caller_loc.path}:#{caller_loc.lineno}\n"
242
242
  end
@@ -78,7 +78,7 @@ module T::Props::Serializable
78
78
 
79
79
  if hash.size > hash_keys_matching_props
80
80
  serialized_forms = self.class.decorator.prop_by_serialized_forms
81
- extra = hash.reject {|k, _| serialized_forms.key?(k)}
81
+ extra = hash.reject { |k, _| serialized_forms.key?(k) }
82
82
 
83
83
  # `extra` could still be empty here if the input matches a `dont_store` prop;
84
84
  # historically, we just ignore those
@@ -111,7 +111,7 @@ module T::Props::Serializable
111
111
  new_obj[k.to_s] = recursive_stringify_keys(v)
112
112
  end
113
113
  elsif obj.is_a?(Array)
114
- new_obj = obj.map {|v| recursive_stringify_keys(v)}
114
+ new_obj = obj.map { |v| recursive_stringify_keys(v) }
115
115
  else
116
116
  new_obj = obj
117
117
  end
@@ -121,12 +121,12 @@ module T::Props::Serializable
121
121
  private def with_existing_hash(changed_props, existing_hash:)
122
122
  serialized = existing_hash
123
123
  new_val = self.class.from_hash(serialized.merge(recursive_stringify_keys(changed_props)))
124
- old_extra = self.instance_variable_get(:@_extra_props) if self.instance_variable_defined?(:@_extra_props)
125
- new_extra = new_val.instance_variable_get(:@_extra_props) if new_val.instance_variable_defined?(:@_extra_props)
124
+ old_extra = self.instance_variable_get(:@_extra_props)
125
+ new_extra = new_val.instance_variable_get(:@_extra_props)
126
126
  if old_extra != new_extra
127
127
  difference =
128
128
  if old_extra
129
- new_extra.reject {|k, v| old_extra[k] == v}
129
+ new_extra.reject { |k, v| old_extra[k] == v }
130
130
  else
131
131
  new_extra
132
132
  end
@@ -137,8 +137,7 @@ module T::Props::Serializable
137
137
 
138
138
  # Asserts if this property is missing during strict serialize
139
139
  private def required_prop_missing_from_serialize(prop)
140
- if defined?(@_required_props_missing_from_deserialize) &&
141
- @_required_props_missing_from_deserialize&.include?(prop)
140
+ if @_required_props_missing_from_deserialize&.include?(prop)
142
141
  # If the prop was already missing during deserialization, that means the application
143
142
  # code already had to deal with a nil value, which means we wouldn't be accomplishing
144
143
  # much by raising here (other than causing an unnecessary breakage).
@@ -195,7 +194,7 @@ module T::Props::Serializable::DecoratorMethods
195
194
  end
196
195
 
197
196
  def required_props
198
- @class.props.select {|_, v| T::Props::Utils.required_prop?(v)}.keys
197
+ @class.props.select { |_, v| T::Props::Utils.required_prop?(v) }.keys
199
198
  end
200
199
 
201
200
  def prop_dont_store?(prop)
@@ -228,11 +227,11 @@ module T::Props::Serializable::DecoratorMethods
228
227
  res = super
229
228
  prop_by_serialized_forms[serialized_form] = prop
230
229
  if T::Configuration.use_vm_prop_serde?
231
- enqueue_lazy_vm_method_definition!(:__t_props_generated_serialize) {generate_serialize2}
232
- enqueue_lazy_vm_method_definition!(:__t_props_generated_deserialize) {generate_deserialize2}
230
+ enqueue_lazy_vm_method_definition!(:__t_props_generated_serialize) { generate_serialize2 }
231
+ enqueue_lazy_vm_method_definition!(:__t_props_generated_deserialize) { generate_deserialize2 }
233
232
  else
234
- enqueue_lazy_method_definition!(:__t_props_generated_serialize) {generate_serialize_source}
235
- enqueue_lazy_method_definition!(:__t_props_generated_deserialize) {generate_deserialize_source}
233
+ enqueue_lazy_method_definition!(:__t_props_generated_serialize) { generate_serialize_source }
234
+ enqueue_lazy_method_definition!(:__t_props_generated_deserialize) { generate_deserialize_source }
236
235
  end
237
236
  res
238
237
  end
@@ -263,7 +262,7 @@ module T::Props::Serializable::DecoratorMethods
263
262
  def message_with_generated_source_context(error, generated_method, generate_source_method)
264
263
  generated_method = generated_method.to_s
265
264
  if error.backtrace_locations
266
- line_loc = error.backtrace_locations.find {|l| l.base_label == generated_method}
265
+ line_loc = error.backtrace_locations.find { |l| l.base_label == generated_method }
267
266
  return unless line_loc
268
267
 
269
268
  line_num = line_loc.lineno
@@ -275,7 +274,7 @@ module T::Props::Serializable::DecoratorMethods
275
274
  # in `__t_props_generated_serialize'"
276
275
  "in `#{generated_method}'"
277
276
  end
278
- line_label = error.backtrace.find {|l| l.end_with?(label)}
277
+ line_label = error.backtrace.find { |l| l.end_with?(label) }
279
278
  return unless line_label
280
279
 
281
280
  line_num = if line_label.start_with?("(eval)")
@@ -353,11 +352,7 @@ module T::Props::Serializable::DecoratorMethods
353
352
  private_constant :EMPTY_EXTRA_PROPS
354
353
 
355
354
  def extra_props(instance)
356
- if instance.instance_variable_defined?(:@_extra_props)
357
- instance.instance_variable_get(:@_extra_props) || EMPTY_EXTRA_PROPS
358
- else
359
- EMPTY_EXTRA_PROPS
360
- end
355
+ instance.instance_variable_get(:@_extra_props) || EMPTY_EXTRA_PROPS
361
356
  end
362
357
 
363
358
  # adds to the default result of T::Props::PrettyPrintable
@@ -11,7 +11,7 @@ module T::Props::TypeValidation
11
11
  module DecoratorMethods
12
12
  extend T::Sig
13
13
 
14
- sig {params(key: Symbol).returns(T::Boolean).checked(:never)}
14
+ sig { params(key: Symbol).returns(T::Boolean).checked(:never) }
15
15
  def valid_rule_key?(key)
16
16
  super || key == :DEPRECATED_underspecified_type
17
17
  end
@@ -58,19 +58,19 @@ module T::Props::TypeValidation
58
58
  # If the type is fully valid, returns nil.
59
59
  #
60
60
  # checked(:never) - called potentially many times recursively
61
- sig {params(type: T::Types::Base).returns(T.nilable(T::Types::Base)).checked(:never)}
61
+ sig { params(type: T::Types::Base).returns(T.nilable(T::Types::Base)).checked(:never) }
62
62
  private def find_invalid_subtype(type)
63
63
  case type
64
64
  when T::Types::TypedEnumerable
65
65
  find_invalid_subtype(type.type)
66
66
  when T::Types::FixedHash
67
- type.types.values.map {|subtype| find_invalid_subtype(subtype)}.compact.first
67
+ type.types.values.map { |subtype| find_invalid_subtype(subtype) }.compact.first
68
68
  when T::Types::Union, T::Types::FixedArray
69
69
  # `T.any` is valid if all of the members are valid
70
- type.types.map {|subtype| find_invalid_subtype(subtype)}.compact.first
70
+ type.types.map { |subtype| find_invalid_subtype(subtype) }.compact.first
71
71
  when T::Types::Intersection
72
72
  # `T.all` is valid if at least one of the members is valid
73
- invalid = type.types.map {|subtype| find_invalid_subtype(subtype)}.compact
73
+ invalid = type.types.map { |subtype| find_invalid_subtype(subtype) }.compact
74
74
  if invalid.length == type.types.length
75
75
  invalid.first
76
76
  else
@@ -13,7 +13,7 @@ module T::Props::Utils
13
13
  when Symbol, NilClass, Numeric
14
14
  what
15
15
  when Array
16
- what.map {|v| deep_clone_object(v, freeze: freeze)}
16
+ what.map { |v| deep_clone_object(v, freeze: freeze) }
17
17
  when Hash
18
18
  h = what.class.new
19
19
  what.each do |k, v|
@@ -6,7 +6,7 @@ module T::Props::WeakConstructor
6
6
  extend T::Sig
7
7
 
8
8
  # checked(:never) - O(runtime object construction)
9
- sig {params(hash: T::Hash[Symbol, T.untyped]).void.checked(:never)}
9
+ sig { params(hash: T::Hash[Symbol, T.untyped]).void.checked(:never) }
10
10
  def initialize(hash={})
11
11
  decorator = self.class.decorator
12
12
 
@@ -28,7 +28,7 @@ module T::Props::WeakConstructor::DecoratorMethods
28
28
  # we'll use to check for any unrecognized input.)
29
29
  #
30
30
  # checked(:never) - O(runtime object construction)
31
- sig {params(instance: T::Props::WeakConstructor, hash: T::Hash[Symbol, T.untyped]).returns(Integer).checked(:never)}
31
+ sig { params(instance: T::Props::WeakConstructor, hash: T::Hash[Symbol, T.untyped]).returns(Integer).checked(:never) }
32
32
  def construct_props_without_defaults(instance, hash)
33
33
  # Use `each_pair` rather than `count` because, as of Ruby 2.6, the latter delegates to Enumerator
34
34
  # and therefore allocates for each entry.
@@ -49,7 +49,7 @@ module T::Props::WeakConstructor::DecoratorMethods
49
49
  # we'll use to check for any unrecognized input.)
50
50
  #
51
51
  # checked(:never) - O(runtime object construction)
52
- sig {params(instance: T::Props::WeakConstructor, hash: T::Hash[Symbol, T.untyped]).returns(Integer).checked(:never)}
52
+ sig { params(instance: T::Props::WeakConstructor, hash: T::Hash[Symbol, T.untyped]).returns(Integer).checked(:never) }
53
53
  def construct_props_with_defaults(instance, hash)
54
54
  # Use `each_pair` rather than `count` because, as of Ruby 2.6, the latter delegates to Enumerator
55
55
  # and therefore allocates for each entry.
data/lib/types/sig.rb CHANGED
@@ -14,7 +14,7 @@ module T::Sig
14
14
 
15
15
  # At runtime, does nothing, but statically it is treated exactly the same
16
16
  # as T::Sig#sig. Only use it in cases where you can't use T::Sig#sig.
17
- T::Sig::WithoutRuntime.sig {params(arg0: T.nilable(Symbol), blk: T.proc.bind(T::Private::Methods::DeclBuilder).void).void}
17
+ T::Sig::WithoutRuntime.sig { params(arg0: T.nilable(Symbol), blk: T.proc.bind(T::Private::Methods::DeclBuilder).void).void }
18
18
  def self.sig(arg0=nil, &blk); end # rubocop:disable Lint/DuplicateMethods
19
19
 
20
20
  $VERBOSE = original_verbose
@@ -23,7 +23,7 @@ module T::Sig
23
23
  # Declares a method with type signatures and/or
24
24
  # abstract/override/... helpers. See the documentation URL on
25
25
  # {T::Helpers}
26
- T::Sig::WithoutRuntime.sig {params(arg0: T.nilable(Symbol), blk: T.proc.bind(T::Private::Methods::DeclBuilder).void).void}
26
+ T::Sig::WithoutRuntime.sig { params(arg0: T.nilable(Symbol), blk: T.proc.bind(T::Private::Methods::DeclBuilder).void).void }
27
27
  def sig(arg0=nil, &blk)
28
28
  T::Private::Methods.declare_sig(self, Kernel.caller_locations(1, 1)&.first, arg0, &blk)
29
29
  end
data/lib/types/struct.rb CHANGED
@@ -30,7 +30,7 @@ class T::ImmutableStruct < T::InexactStruct
30
30
  end
31
31
 
32
32
  # Matches the one in WeakConstructor, but freezes the object
33
- sig {params(hash: T::Hash[Symbol, T.untyped]).void.checked(:never)}
33
+ sig { params(hash: T::Hash[Symbol, T.untyped]).void.checked(:never) }
34
34
  def initialize(hash={})
35
35
  super
36
36
 
@@ -38,7 +38,7 @@ class T::ImmutableStruct < T::InexactStruct
38
38
  end
39
39
 
40
40
  # Matches the signature in Props, but raises since this is an immutable struct and only const is allowed
41
- sig {params(name: Symbol, cls: T.untyped, rules: T.untyped).void}
41
+ sig { params(name: Symbol, cls: T.untyped, rules: T.untyped).void }
42
42
  def self.prop(name, cls, **rules)
43
43
  return super if (cls.is_a?(Hash) && cls[:immutable]) || rules[:immutable]
44
44
 
@@ -83,27 +83,27 @@ module T::Types
83
83
  # Note: order of cases here matters!
84
84
  if t1.is_a?(T::Types::Union) # 7, 8, 9
85
85
  # this will be incorrect if/when we have Type members
86
- return t1.types.all? {|t1_member| t1_member.subtype_of?(t2)}
86
+ return t1.types.all? { |t1_member| t1_member.subtype_of?(t2) }
87
87
  end
88
88
 
89
89
  if t2.is_a?(T::Types::Intersection) # 2, 5
90
90
  # this will be incorrect if/when we have Type members
91
- return t2.types.all? {|t2_member| t1.subtype_of?(t2_member)}
91
+ return t2.types.all? { |t2_member| t1.subtype_of?(t2_member) }
92
92
  end
93
93
 
94
94
  if t2.is_a?(T::Types::Union)
95
95
  if t1.is_a?(T::Types::Intersection) # 6
96
96
  # dropping either of parts eagerly make subtype test be too strict.
97
97
  # we have to try both cases, when we normally try only one
98
- return t2.types.any? {|t2_member| t1.subtype_of?(t2_member)} ||
99
- t1.types.any? {|t1_member| t1_member.subtype_of?(t2)}
98
+ return t2.types.any? { |t2_member| t1.subtype_of?(t2_member) } ||
99
+ t1.types.any? { |t1_member| t1_member.subtype_of?(t2) }
100
100
  end
101
- return t2.types.any? {|t2_member| t1.subtype_of?(t2_member)} # 3
101
+ return t2.types.any? { |t2_member| t1.subtype_of?(t2_member) } # 3
102
102
  end
103
103
 
104
104
  if t1.is_a?(T::Types::Intersection) # 4
105
105
  # this will be incorrect if/when we have Type members
106
- return t1.types.any? {|t1_member| t1_member.subtype_of?(t2)}
106
+ return t1.types.any? { |t1_member| t1_member.subtype_of?(t2) }
107
107
  end
108
108
 
109
109
  # 1; Start with some special cases
@@ -11,7 +11,7 @@ module T::Types
11
11
  end
12
12
 
13
13
  def types
14
- @types ||= @inner_types.map {|type| T::Utils.coerce(type)}
14
+ @types ||= @inner_types.map { |type| T::Utils.coerce(type) }
15
15
  end
16
16
 
17
17
  def build_type
@@ -10,7 +10,7 @@ module T::Types
10
10
  end
11
11
 
12
12
  def types
13
- @types ||= @inner_types.transform_values {|v| T::Utils.coerce(v)}
13
+ @types ||= @inner_types.transform_values { |v| T::Utils.coerce(v) }
14
14
  end
15
15
 
16
16
  def build_type
@@ -26,16 +26,16 @@ module T::Types
26
26
  # overrides Base
27
27
  def recursively_valid?(obj)
28
28
  return false unless obj.is_a?(Hash)
29
- return false if types.any? {|key, type| !type.recursively_valid?(obj[key])}
30
- return false if obj.any? {|key, _| !types[key]}
29
+ return false if types.any? { |key, type| !type.recursively_valid?(obj[key]) }
30
+ return false if obj.any? { |key, _| !types[key] }
31
31
  true
32
32
  end
33
33
 
34
34
  # overrides Base
35
35
  def valid?(obj)
36
36
  return false unless obj.is_a?(Hash)
37
- return false if types.any? {|key, type| !type.valid?(obj[key])}
38
- return false if obj.any? {|key, _| !types[key]}
37
+ return false if types.any? { |key, type| !type.valid?(obj[key]) }
38
+ return false if obj.any? { |key, _| !types[key] }
39
39
  true
40
40
  end
41
41
 
@@ -48,7 +48,7 @@ module T::Types
48
48
  when TypedHash
49
49
  # warning: covariant hashes
50
50
 
51
- key1, key2, *keys_rest = types.keys.map {|key| T::Utils.coerce(key.class)}
51
+ key1, key2, *keys_rest = types.keys.map { |key| T::Utils.coerce(key.class) }
52
52
  key_type = if !key2.nil?
53
53
  T::Types::Union::Private::Pool.union_of_types(key1, key2, keys_rest)
54
54
  elsif key1.nil?
@@ -32,12 +32,12 @@ module T::Types
32
32
 
33
33
  # overrides Base
34
34
  def recursively_valid?(obj)
35
- types.all? {|type| type.recursively_valid?(obj)}
35
+ types.all? { |type| type.recursively_valid?(obj) }
36
36
  end
37
37
 
38
38
  # overrides Base
39
39
  def valid?(obj)
40
- types.all? {|type| type.valid?(obj)}
40
+ types.all? { |type| type.valid?(obj) }
41
41
  end
42
42
 
43
43
  # overrides Base
@@ -34,6 +34,8 @@ module T::Types
34
34
  case other
35
35
  when TEnum
36
36
  @val == other.val
37
+ when Simple
38
+ other.raw_type.===(@val)
37
39
  else
38
40
  false
39
41
  end