Skip to content

Commit

Permalink
Fix record shape
Browse files Browse the repository at this point in the history
Add a default overloading with the *backtype* of the keys, instead of the union of the key types.
  • Loading branch information
soutaro committed Oct 8, 2024
1 parent f929265 commit 937e1cb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/steep/interface/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ def tuple_shape(tuple)

def record_shape(record)
all_key_type = AST::Types::Union.build(
types: record.elements.each_key.map {|value| AST::Types::Literal.new(value: value) }
types: record.elements.each_key.map {|value| AST::Types::Literal.new(value: value).back_type }
)
all_value_type = AST::Types::Union.build(types: record.elements.values)
hash_type = AST::Builtin::Hash.instance_type(all_key_type, all_value_type)
Expand Down
12 changes: 6 additions & 6 deletions test/interface_builder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ def special_types: () -> [self, class, instance]

assert_includes(shape.methods[:[]].method_types, parse_method_type("(:id) -> ::Integer"))
assert_includes(shape.methods[:[]].method_types, parse_method_type("(:name) -> ::String"))
assert_includes(shape.methods[:[]].method_types, parse_method_type("(:name | :id) -> (::String | ::Integer)"))
assert_includes(shape.methods[:[]].method_types, parse_method_type("(::Symbol) -> (::String | ::Integer)"))

assert_includes(shape.methods[:[]=].method_types, parse_method_type("(:id, ::Integer) -> ::Integer"))
assert_includes(shape.methods[:[]=].method_types, parse_method_type("(:name, ::String) -> ::String"))
Expand All @@ -346,16 +346,16 @@ def special_types: () -> [self, class, instance]
assert_includes(shape.methods[:fetch].method_types, parse_method_type("(:name) -> ::String"))
assert_includes(shape.methods[:fetch].method_types, parse_method_type("[T] (:id, T) -> (::Integer | T)"))
assert_includes(shape.methods[:fetch].method_types, parse_method_type("[T] (:name, T) -> (::String | T)"))
assert_includes(shape.methods[:fetch].method_types, parse_method_type("[T] (:id) { (:id | :name) -> T } -> (::Integer | T)"))
assert_includes(shape.methods[:fetch].method_types, parse_method_type("[T] (:name) { (:id | :name) -> T } -> (::String | T)"))
assert_includes(shape.methods[:fetch].method_types, parse_method_type("[T] (:id) { (::Symbol) -> T } -> (::Integer | T)"))
assert_includes(shape.methods[:fetch].method_types, parse_method_type("[T] (:name) { (::Symbol) -> T } -> (::String | T)"))
end

builder.shape(parse_type("{ id: ::Integer, name: self }"), config).tap do |shape|
assert_equal parse_type("{ id: ::Integer, name: self }"), shape.type

assert_includes(shape.methods[:[]].method_types, parse_method_type("(:id) -> ::Integer"))
assert_includes(shape.methods[:[]].method_types, parse_method_type("(:name) -> self"))
assert_includes(shape.methods[:[]].method_types, parse_method_type("(:name | :id) -> (self | ::Integer)"))
assert_includes(shape.methods[:[]].method_types, parse_method_type("(::Symbol) -> (self | ::Integer)"))

assert_includes(shape.methods[:[]=].method_types, parse_method_type("(:id, ::Integer) -> ::Integer"))
assert_includes(shape.methods[:[]=].method_types, parse_method_type("(:name, self) -> self"))
Expand All @@ -364,8 +364,8 @@ def special_types: () -> [self, class, instance]
assert_includes(shape.methods[:fetch].method_types, parse_method_type("(:name) -> self"))
assert_includes(shape.methods[:fetch].method_types, parse_method_type("[T] (:id, T) -> (::Integer | T)"))
assert_includes(shape.methods[:fetch].method_types, parse_method_type("[T] (:name, T) -> (self | T)"))
assert_includes(shape.methods[:fetch].method_types, parse_method_type("[T] (:id) { (:id | :name) -> T } -> (::Integer | T)"))
assert_includes(shape.methods[:fetch].method_types, parse_method_type("[T] (:name) { (:id | :name) -> T } -> (self | T)"))
assert_includes(shape.methods[:fetch].method_types, parse_method_type("[T] (:id) { (::Symbol) -> T } -> (::Integer | T)"))
assert_includes(shape.methods[:fetch].method_types, parse_method_type("[T] (:name) { (::Symbol) -> T } -> (self | T)"))
end
end
end
Expand Down

0 comments on commit 937e1cb

Please sign in to comment.