module T::Props::Private::SetterFactory
def self.build_setter_proc(klass, prop, rules)
def self.build_setter_proc(klass, prop, rules) # Our nil check works differently than a simple T.nilable for various # reasons (including the `raise_on_nil_write` setting, the existence # of defaults & factories, and the fact that we allow `T.nilable(Foo)` # where Foo < T::Props::CustomType as a prop type even though calling # `valid?` on it won't work as expected), so unwrap any T.nilable and # do a check manually. (Note this hack does not fix custom types as # collection elements.) non_nil_type = if rules[:type_is_custom_type] rules.fetch(:type) else T::Utils::Nilable.get_underlying_type_object(rules.fetch(:type_object)) end accessor_key = rules.fetch(:accessor_key) raise_error = ->(val) {raise_pretty_error(klass, prop, non_nil_type, val)} # It seems like a bug that this affects the behavior of setters, but # some existing code relies on this behavior has_explicit_nil_default = rules.key?(:default) && rules.fetch(:default).nil? if !T::Props::Utils.need_nil_write_check?(rules) || has_explicit_nil_default proc do |val| if val.nil? instance_variable_set(accessor_key, nil) elsif non_nil_type.valid?(val) instance_variable_set(accessor_key, val) else raise_error.call(val) end end else proc do |val| if non_nil_type.valid?(val) instance_variable_set(accessor_key, val) else raise_error.call(val) end end end end
def self.raise_pretty_error(klass, prop, type, val)
def self.raise_pretty_error(klass, prop, type, val) 't set #{klass.name}.#{prop} to #{val.inspect} (instance of #{val.class}) - need a #{type}" arameter '#{prop}': #{base_message}\n" _locations&.find {|l| !l.to_s.include?('sorbet-runtime/lib/types/props')} "Caller: #{caller_loc.path}:#{caller_loc.lineno}\n" ll_validation_error_handler( sage, retty_message, , loc,