Skip to content

Commit aaf65d4

Browse files
authored
Merge pull request #60 from ruby/type_inspect_params_fix
Improve inspect of recursively nested types
2 parents 25108aa + 4d01fb7 commit aaf65d4

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

lib/repl_type_completor/types.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,9 @@ def rbs_methods
273273
end
274274

275275
def inspect
276-
if params.empty?
276+
if !@params && (@klass == Array || @klass == Hash) && @instances
277+
"#{inspect_without_params}[unresolved]"
278+
elsif params.empty?
277279
inspect_without_params
278280
else
279281
params_string = "[#{params.map { "#{_1}: #{_2.inspect}" }.join(', ')}]"

test/repl_type_completor/test_types.rb

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@ def bo.hash; 42; end # Needed to use this object as a hash key
5555
assert_equal BasicObject, bo_key_hash_type.params[:K].klass
5656
assert_equal BasicObject, bo_value_hash_type.params[:V].klass
5757
assert_equal 'Object', obj_type.inspect
58-
assert_equal 'Array[Elem: Integer | String]', arr_type.inspect
59-
assert_equal 'Hash[K: String, V: Symbol]', hash_type.inspect
58+
assert_equal 'Array[unresolved]', arr_type.inspect
59+
assert_equal 'Array[Elem: Integer | String]', arr_type.tap(&:params).inspect
60+
assert_equal 'Hash[unresolved]', hash_type.inspect
61+
assert_equal 'Hash[K: String, V: Symbol]', hash_type.tap(&:params).inspect
6062
assert_equal 'Array.itself', ReplTypeCompletor::Types.type_from_object(Array).inspect
6163
assert_equal 'ReplTypeCompletor.itself', ReplTypeCompletor::Types.type_from_object(ReplTypeCompletor).inspect
6264
end
@@ -112,7 +114,17 @@ def test_params_lazily_expanded_on_recursive_type
112114
assert_equal expected, elem_type.types.map(&:klass).sort_by(&:name)
113115
type = elem_type.types.find { _1.klass == Array }
114116
end
115-
assert_equal 'Hash[K: Integer, V: Float]', type.params[:Elem].types.find { _1.klass == Hash }.inspect
117+
hash_type = type.params[:Elem].types.find { _1.klass == Hash }
118+
assert_equal 'Hash[unresolved]', hash_type.inspect
119+
assert_equal 'Hash[K: Integer, V: Float]', hash_type.tap(&:params).inspect
120+
end
121+
122+
def test_infinite_nested_type_inspect
123+
a = []
124+
a << a
125+
type = ReplTypeCompletor::Types.type_from_object a
126+
assert_equal 'Array[unresolved]', type.inspect
127+
assert_equal 'Array[Elem: Array[unresolved]]', type.tap(&:params).inspect
116128
end
117129
end
118130
end

0 commit comments

Comments
 (0)