-
Notifications
You must be signed in to change notification settings - Fork 506
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow shape types in T::Struct props (#4365)
* Allow shape types in T::Struct props Similar in spirit to #4361. Fixes #3485 * Fix failing tests * Fix test assertion
- Loading branch information
Showing
6 changed files
with
98 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# typed: true | ||
extend T::Sig | ||
|
||
class StructWithShapes < T::Struct | ||
prop :empty_shape, {} | ||
prop :symbol_key_shape, {foo: Integer} | ||
prop :string_key_shape, {'foo' => Integer} | ||
prop :multiple_keys, {foo: Integer, bar: String, baz: Symbol} | ||
prop :array_of_shape, T::Array[{foo: Integer}] | ||
prop :shape_with_combinators, {foo: T.nilable(Integer)} | ||
prop :combinator_with_shape, T.nilable({foo: Integer}) | ||
end | ||
|
||
class StructWithBadShapes < T::Struct | ||
prop :bad_literal, {foo: 42} | ||
end | ||
|
||
x = StructWithShapes.new( | ||
empty_shape: {}, | ||
symbol_key_shape: {foo: 0}, | ||
string_key_shape: {'foo' => 0}, | ||
multiple_keys: {foo: 0, bar: '', baz: :a_symbol}, | ||
array_of_shape: [{foo: 0}], | ||
shape_with_combinators: {foo: nil} | ||
) | ||
|
||
T.reveal_type(x.empty_shape) # error: `{} (shape of T::Hash[T.untyped, T.untyped])` | ||
T.reveal_type(x.symbol_key_shape) # error: `{foo: Integer} (shape of T::Hash[T.untyped, T.untyped])` | ||
T.reveal_type(x.string_key_shape) # error: `{String("foo") => Integer} (shape of T::Hash[T.untyped, T.untyped])` | ||
T.reveal_type(x.multiple_keys) # error: `{foo: Integer, bar: String, baz: Symbol} (shape of T::Hash[T.untyped, T.untyped])` | ||
|
||
T.reveal_type(x.array_of_shape) # error: `T::Array[{foo: Integer}]` | ||
T.reveal_type(x.shape_with_combinators) # error: `{foo: T.nilable(Integer)} (shape of T::Hash[T.untyped, T.untyped])` | ||
T.reveal_type(x.combinator_with_shape) # error: `T.nilable({foo: Integer})` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
# typed: true | ||
|
||
class A < T::Struct | ||
# This prop won't be processed by the Prop dsl, as it contains a shape type | ||
const :foo, T::Array[{foo: Integer}] | ||
end | ||
|
||
A.new(foo: []) | ||
# ^^^^^^^^^^^^^^ error: Too many arguments | ||
A.new(foo: []) |