'"Attempted to redefine prop" error with T::Struct
I'm trying to make use of T::Struct to avoid some boilerplate in a class:
srb tc
doesn't report any errors, but when I run the tests it fails with:
ArgumentError:
Attempted to redefine prop :check_name that's already defined without specifying :override => true: {:type=>String, :type_is_custom_type=>nil, :type_is_serializable=>nil, :type_is_array_of_serializable=>false, :type_is_hash_of_serializable_values=>false, :type_is_hash_of_custom_type_keys=>false, :type_object=>#<T::Types::Simple:0x00007fc611266e78 @raw_type=String>, :type_needs_clone=>false, :accessor_key=>:@check_name, :sensitivity=>nil, :pii=>nil, :extra=>nil, :serialized_form=>"check_name", :fully_optional=>false, :need_nil_read_check=>nil}
# /Users/awaite/.rvm/gems/ruby-2.4.4/gems/sorbet-runtime-0.5.5188/lib/types/props/decorator.rb:56:in `add_prop_definition'
# /Users/awaite/.rvm/gems/ruby-2.4.4/gems/sorbet-runtime-0.5.5188/lib/types/private/methods/call_validation.rb:126:in `call'
# /Users/awaite/.rvm/gems/ruby-2.4.4/gems/sorbet-runtime-0.5.5188/lib/types/private/methods/call_validation.rb:126:in `validate_call'
# /Users/awaite/.rvm/gems/ruby-2.4.4/gems/sorbet-runtime-0.5.5188/lib/types/private/methods/call_validation.rb:186:in `block in create_validator_slow'
# /Users/awaite/.rvm/gems/ruby-2.4.4/gems/sorbet-runtime-0.5.5188/lib/types/props/optional.rb:48:in `add_prop_definition'
# /Users/awaite/.rvm/gems/ruby-2.4.4/gems/sorbet-runtime-0.5.5188/lib/types/props/serializable.rb:311:in `add_prop_definition'
# /Users/awaite/.rvm/gems/ruby-2.4.4/gems/sorbet-runtime-0.5.5188/lib/types/props/decorator.rb:490:in `prop_defined'
# /Users/awaite/.rvm/gems/ruby-2.4.4/gems/sorbet-runtime-0.5.5188/lib/types/private/methods/call_validation.rb:126:in `call'
# /Users/awaite/.rvm/gems/ruby-2.4.4/gems/sorbet-runtime-0.5.5188/lib/types/private/methods/call_validation.rb:126:in `validate_call'
# /Users/awaite/.rvm/gems/ruby-2.4.4/gems/sorbet-runtime-0.5.5188/lib/types/private/methods/call_validation.rb:186:in `block in create_validator_slow'
# /Users/awaite/.rvm/gems/ruby-2.4.4/gems/sorbet-runtime-0.5.5188/lib/types/props/_props.rb:109:in `prop'
# ./lib/cc_engine/issue.rb:8:in `<class:Issue>'
If I re-order the props, I get the same error but for description
instead.
Is it perhaps because I'm using a struct but also defining methods on it? I know Ruby's Struct
supports that but I'm not sure about Sorbet's T::Struct
.
Solution 1:[1]
Solved: I had to add a dependency, require "cc_engine/location"
(although I don't yet understand why it caused that particular error message).
Solution 2:[2]
I got this error when I tried to type a value prior to its type being defined
class MyStruct < T::Struct
extend T::Sig
const :some_const, MyEnumType
class MyEnumType < T::Enum
...
end
end
Moving the definition of MyEnumType
to be above the definition of some_const
fixed the issue.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Andy Waite |
Solution 2 | BlueRaja - Danny Pflughoeft |