sorbet-runtime 0.5.11144 → 0.5.11218
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/sorbet-runtime.rb +0 -1
- data/lib/types/private/abstract/declare.rb +4 -5
- data/lib/types/private/methods/_methods.rb +23 -23
- data/lib/types/private/methods/call_validation.rb +16 -2
- data/lib/types/private/methods/call_validation_2_6.rb +518 -0
- data/lib/types/private/methods/call_validation_2_7.rb +518 -0
- data/lib/types/private/methods/decl_builder.rb +1 -1
- data/lib/types/private/methods/signature.rb +9 -0
- data/lib/types/private/methods/signature_validation.rb +9 -2
- data/lib/types/private/types/not_typed.rb +4 -0
- data/lib/types/private/types/string_holder.rb +4 -0
- data/lib/types/private/types/type_alias.rb +4 -0
- data/lib/types/private/types/void.rb +4 -0
- data/lib/types/props/_props.rb +2 -2
- data/lib/types/props/decorator.rb +7 -6
- data/lib/types/props/pretty_printable.rb +1 -1
- data/lib/types/props/private/setter_factory.rb +129 -69
- data/lib/types/types/anything.rb +4 -0
- data/lib/types/types/attached_class.rb +4 -0
- data/lib/types/types/base.rb +6 -0
- data/lib/types/types/class_of.rb +4 -0
- data/lib/types/types/enum.rb +4 -0
- data/lib/types/types/fixed_array.rb +19 -12
- data/lib/types/types/fixed_hash.rb +16 -9
- data/lib/types/types/intersection.rb +13 -6
- data/lib/types/types/noreturn.rb +4 -0
- data/lib/types/types/proc.rb +19 -9
- data/lib/types/types/self_type.rb +4 -0
- data/lib/types/types/simple.rb +4 -0
- data/lib/types/types/t_enum.rb +4 -0
- data/lib/types/types/type_parameter.rb +4 -0
- data/lib/types/types/type_variable.rb +4 -0
- data/lib/types/types/typed_array.rb +6 -1
- data/lib/types/types/typed_class.rb +21 -4
- data/lib/types/types/typed_enumerable.rb +18 -11
- data/lib/types/types/typed_enumerator.rb +1 -3
- data/lib/types/types/typed_enumerator_chain.rb +1 -3
- data/lib/types/types/typed_enumerator_lazy.rb +1 -3
- data/lib/types/types/typed_hash.rb +17 -7
- data/lib/types/types/typed_range.rb +1 -3
- data/lib/types/types/typed_set.rb +1 -3
- data/lib/types/types/union.rb +12 -5
- data/lib/types/types/untyped.rb +4 -0
- data/lib/types/utils.rb +2 -2
- metadata +2 -3
- data/lib/types/interface_wrapper.rb +0 -162
@@ -7,6 +7,7 @@ module T::Props
|
|
7
7
|
extend T::Sig
|
8
8
|
|
9
9
|
SetterProc = T.type_alias {T.proc.params(val: T.untyped).void}
|
10
|
+
ValueValidationProc = T.type_alias {T.proc.params(val: T.untyped).void}
|
10
11
|
ValidateProc = T.type_alias {T.proc.params(prop: Symbol, value: T.untyped).void}
|
11
12
|
|
12
13
|
sig do
|
@@ -15,7 +16,7 @@ module T::Props
|
|
15
16
|
prop: Symbol,
|
16
17
|
rules: T::Hash[Symbol, T.untyped]
|
17
18
|
)
|
18
|
-
.returns(SetterProc)
|
19
|
+
.returns([SetterProc, ValueValidationProc])
|
19
20
|
.checked(:never)
|
20
21
|
end
|
21
22
|
def self.build_setter_proc(klass, prop, rules)
|
@@ -55,20 +56,32 @@ module T::Props
|
|
55
56
|
non_nil_type: Module,
|
56
57
|
klass: T.all(Module, T::Props::ClassMethods),
|
57
58
|
)
|
58
|
-
.returns(SetterProc)
|
59
|
+
.returns([SetterProc, ValueValidationProc])
|
59
60
|
end
|
60
61
|
private_class_method def self.simple_non_nil_proc(prop, accessor_key, non_nil_type, klass)
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
62
|
+
[
|
63
|
+
proc do |val|
|
64
|
+
unless val.is_a?(non_nil_type)
|
65
|
+
T::Props::Private::SetterFactory.raise_pretty_error(
|
66
|
+
klass,
|
67
|
+
prop,
|
68
|
+
T::Utils.coerce(non_nil_type),
|
69
|
+
val,
|
70
|
+
)
|
71
|
+
end
|
72
|
+
instance_variable_set(accessor_key, val)
|
73
|
+
end,
|
74
|
+
proc do |val|
|
75
|
+
unless val.is_a?(non_nil_type)
|
76
|
+
T::Props::Private::SetterFactory.raise_pretty_error(
|
77
|
+
klass,
|
78
|
+
prop,
|
79
|
+
T::Utils.coerce(non_nil_type),
|
80
|
+
val,
|
81
|
+
)
|
82
|
+
end
|
83
|
+
end,
|
84
|
+
]
|
72
85
|
end
|
73
86
|
|
74
87
|
sig do
|
@@ -79,27 +92,46 @@ module T::Props
|
|
79
92
|
klass: T.all(Module, T::Props::ClassMethods),
|
80
93
|
validate: T.nilable(ValidateProc)
|
81
94
|
)
|
82
|
-
.returns(SetterProc)
|
95
|
+
.returns([SetterProc, ValueValidationProc])
|
83
96
|
end
|
84
97
|
private_class_method def self.non_nil_proc(prop, accessor_key, non_nil_type, klass, validate)
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
98
|
+
[
|
99
|
+
proc do |val|
|
100
|
+
# this use of recursively_valid? is intentional: unlike for
|
101
|
+
# methods, we want to make sure data at the 'edge'
|
102
|
+
# (e.g. models that go into databases or structs serialized
|
103
|
+
# from disk) are correct, so we use more thorough runtime
|
104
|
+
# checks there
|
105
|
+
if non_nil_type.recursively_valid?(val)
|
106
|
+
validate&.call(prop, val)
|
107
|
+
else
|
108
|
+
T::Props::Private::SetterFactory.raise_pretty_error(
|
109
|
+
klass,
|
110
|
+
prop,
|
111
|
+
non_nil_type,
|
112
|
+
val,
|
113
|
+
)
|
114
|
+
end
|
115
|
+
instance_variable_set(accessor_key, val)
|
116
|
+
end,
|
117
|
+
proc do |val|
|
118
|
+
# this use of recursively_valid? is intentional: unlike for
|
119
|
+
# methods, we want to make sure data at the 'edge'
|
120
|
+
# (e.g. models that go into databases or structs serialized
|
121
|
+
# from disk) are correct, so we use more thorough runtime
|
122
|
+
# checks there
|
123
|
+
if non_nil_type.recursively_valid?(val)
|
124
|
+
validate&.call(prop, val)
|
125
|
+
else
|
126
|
+
T::Props::Private::SetterFactory.raise_pretty_error(
|
127
|
+
klass,
|
128
|
+
prop,
|
129
|
+
non_nil_type,
|
130
|
+
val,
|
131
|
+
)
|
132
|
+
end
|
133
|
+
end,
|
134
|
+
]
|
103
135
|
end
|
104
136
|
|
105
137
|
sig do
|
@@ -109,24 +141,32 @@ module T::Props
|
|
109
141
|
non_nil_type: Module,
|
110
142
|
klass: T.all(Module, T::Props::ClassMethods),
|
111
143
|
)
|
112
|
-
.returns(SetterProc)
|
144
|
+
.returns([SetterProc, ValueValidationProc])
|
113
145
|
end
|
114
146
|
private_class_method def self.simple_nilable_proc(prop, accessor_key, non_nil_type, klass)
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
val,
|
126
|
-
)
|
147
|
+
[
|
148
|
+
proc do |val|
|
149
|
+
unless val.nil? || val.is_a?(non_nil_type)
|
150
|
+
T::Props::Private::SetterFactory.raise_pretty_error(
|
151
|
+
klass,
|
152
|
+
prop,
|
153
|
+
T::Utils.coerce(non_nil_type),
|
154
|
+
val,
|
155
|
+
)
|
156
|
+
end
|
127
157
|
instance_variable_set(accessor_key, val)
|
128
|
-
end
|
129
|
-
|
158
|
+
end,
|
159
|
+
proc do |val|
|
160
|
+
unless val.nil? || val.is_a?(non_nil_type)
|
161
|
+
T::Props::Private::SetterFactory.raise_pretty_error(
|
162
|
+
klass,
|
163
|
+
prop,
|
164
|
+
T::Utils.coerce(non_nil_type),
|
165
|
+
val,
|
166
|
+
)
|
167
|
+
end
|
168
|
+
end,
|
169
|
+
]
|
130
170
|
end
|
131
171
|
|
132
172
|
sig do
|
@@ -137,30 +177,50 @@ module T::Props
|
|
137
177
|
klass: T.all(Module, T::Props::ClassMethods),
|
138
178
|
validate: T.nilable(ValidateProc),
|
139
179
|
)
|
140
|
-
.returns(SetterProc)
|
180
|
+
.returns([SetterProc, ValueValidationProc])
|
141
181
|
end
|
142
182
|
private_class_method def self.nilable_proc(prop, accessor_key, non_nil_type, klass, validate)
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
183
|
+
[
|
184
|
+
proc do |val|
|
185
|
+
if val.nil?
|
186
|
+
instance_variable_set(accessor_key, nil)
|
187
|
+
# this use of recursively_valid? is intentional: unlike for
|
188
|
+
# methods, we want to make sure data at the 'edge'
|
189
|
+
# (e.g. models that go into databases or structs serialized
|
190
|
+
# from disk) are correct, so we use more thorough runtime
|
191
|
+
# checks there
|
192
|
+
elsif non_nil_type.recursively_valid?(val)
|
193
|
+
validate&.call(prop, val)
|
194
|
+
instance_variable_set(accessor_key, val)
|
195
|
+
else
|
196
|
+
T::Props::Private::SetterFactory.raise_pretty_error(
|
197
|
+
klass,
|
198
|
+
prop,
|
199
|
+
non_nil_type,
|
200
|
+
val,
|
201
|
+
)
|
202
|
+
instance_variable_set(accessor_key, val)
|
203
|
+
end
|
204
|
+
end,
|
205
|
+
proc do |val|
|
206
|
+
if val.nil?
|
207
|
+
# this use of recursively_valid? is intentional: unlike for
|
208
|
+
# methods, we want to make sure data at the 'edge'
|
209
|
+
# (e.g. models that go into databases or structs serialized
|
210
|
+
# from disk) are correct, so we use more thorough runtime
|
211
|
+
# checks there
|
212
|
+
elsif non_nil_type.recursively_valid?(val)
|
213
|
+
validate&.call(prop, val)
|
214
|
+
else
|
215
|
+
T::Props::Private::SetterFactory.raise_pretty_error(
|
216
|
+
klass,
|
217
|
+
prop,
|
218
|
+
non_nil_type,
|
219
|
+
val,
|
220
|
+
)
|
221
|
+
end
|
222
|
+
end,
|
223
|
+
]
|
164
224
|
end
|
165
225
|
|
166
226
|
sig do
|
data/lib/types/types/anything.rb
CHANGED
data/lib/types/types/base.rb
CHANGED
@@ -33,6 +33,12 @@ module T::Types
|
|
33
33
|
raise NotImplementedError
|
34
34
|
end
|
35
35
|
|
36
|
+
# Force any lazy initialization that this type might need to do
|
37
|
+
# It's unusual to call this directly; you probably want to call it indirectly via `T::Utils.run_all_sig_blocks`.
|
38
|
+
def build_type
|
39
|
+
raise NotImplementedError
|
40
|
+
end
|
41
|
+
|
36
42
|
# Equality is based on name, so be sure the name reflects all relevant state when implementing.
|
37
43
|
def name
|
38
44
|
raise NotImplementedError
|
data/lib/types/types/class_of.rb
CHANGED
data/lib/types/types/enum.rb
CHANGED
@@ -6,23 +6,30 @@ module T::Types
|
|
6
6
|
# Takes a list of types. Validates each item in an array using the type in the same position
|
7
7
|
# in the list.
|
8
8
|
class FixedArray < Base
|
9
|
-
attr_reader :types
|
10
|
-
|
11
9
|
def initialize(types)
|
12
|
-
@
|
10
|
+
@inner_types = types
|
11
|
+
end
|
12
|
+
|
13
|
+
def types
|
14
|
+
@types ||= @inner_types.map {|type| T::Utils.coerce(type)}
|
15
|
+
end
|
16
|
+
|
17
|
+
def build_type
|
18
|
+
types
|
19
|
+
nil
|
13
20
|
end
|
14
21
|
|
15
22
|
# overrides Base
|
16
23
|
def name
|
17
|
-
"[#{
|
24
|
+
"[#{types.join(', ')}]"
|
18
25
|
end
|
19
26
|
|
20
27
|
# overrides Base
|
21
28
|
def recursively_valid?(obj)
|
22
|
-
if obj.is_a?(Array) && obj.length ==
|
29
|
+
if obj.is_a?(Array) && obj.length == types.length
|
23
30
|
i = 0
|
24
|
-
while i <
|
25
|
-
if
|
31
|
+
while i < types.length
|
32
|
+
if !types[i].recursively_valid?(obj[i])
|
26
33
|
return false
|
27
34
|
end
|
28
35
|
i += 1
|
@@ -35,10 +42,10 @@ module T::Types
|
|
35
42
|
|
36
43
|
# overrides Base
|
37
44
|
def valid?(obj)
|
38
|
-
if obj.is_a?(Array) && obj.length ==
|
45
|
+
if obj.is_a?(Array) && obj.length == types.length
|
39
46
|
i = 0
|
40
|
-
while i <
|
41
|
-
if
|
47
|
+
while i < types.length
|
48
|
+
if !types[i].valid?(obj[i])
|
42
49
|
return false
|
43
50
|
end
|
44
51
|
i += 1
|
@@ -56,7 +63,7 @@ module T::Types
|
|
56
63
|
# Properly speaking, covariance here is unsound since arrays
|
57
64
|
# can be mutated, but sorbet implements covariant tuples for
|
58
65
|
# ease of adoption.
|
59
|
-
|
66
|
+
types.size == other.types.size && types.zip(other.types).all? do |t1, t2|
|
60
67
|
t1.subtype_of?(t2)
|
61
68
|
end
|
62
69
|
when TypedArray
|
@@ -85,7 +92,7 @@ module T::Types
|
|
85
92
|
# overrides Base
|
86
93
|
def describe_obj(obj)
|
87
94
|
if obj.is_a?(Array)
|
88
|
-
if obj.length ==
|
95
|
+
if obj.length == types.length
|
89
96
|
item_classes = obj.map(&:class).join(', ')
|
90
97
|
"type [#{item_classes}]"
|
91
98
|
else
|
@@ -5,30 +5,37 @@ module T::Types
|
|
5
5
|
# Takes a hash of types. Validates each item in a hash using the type in the same position
|
6
6
|
# in the list.
|
7
7
|
class FixedHash < Base
|
8
|
-
attr_reader :types
|
9
|
-
|
10
8
|
def initialize(types)
|
11
|
-
@
|
9
|
+
@inner_types = types
|
10
|
+
end
|
11
|
+
|
12
|
+
def types
|
13
|
+
@types ||= @inner_types.transform_values {|v| T::Utils.coerce(v)}
|
14
|
+
end
|
15
|
+
|
16
|
+
def build_type
|
17
|
+
types
|
18
|
+
nil
|
12
19
|
end
|
13
20
|
|
14
21
|
# overrides Base
|
15
22
|
def name
|
16
|
-
serialize_hash(
|
23
|
+
serialize_hash(types)
|
17
24
|
end
|
18
25
|
|
19
26
|
# overrides Base
|
20
27
|
def recursively_valid?(obj)
|
21
28
|
return false unless obj.is_a?(Hash)
|
22
|
-
return false if
|
23
|
-
return false if obj.any? {|key, _|
|
29
|
+
return false if types.any? {|key, type| !type.recursively_valid?(obj[key])}
|
30
|
+
return false if obj.any? {|key, _| !types[key]}
|
24
31
|
true
|
25
32
|
end
|
26
33
|
|
27
34
|
# overrides Base
|
28
35
|
def valid?(obj)
|
29
36
|
return false unless obj.is_a?(Hash)
|
30
|
-
return false if
|
31
|
-
return false if obj.any? {|key, _|
|
37
|
+
return false if types.any? {|key, type| !type.valid?(obj[key])}
|
38
|
+
return false if obj.any? {|key, _| !types[key]}
|
32
39
|
true
|
33
40
|
end
|
34
41
|
|
@@ -37,7 +44,7 @@ module T::Types
|
|
37
44
|
case other
|
38
45
|
when FixedHash
|
39
46
|
# Using `subtype_of?` here instead of == would be unsound
|
40
|
-
|
47
|
+
types == other.types
|
41
48
|
when TypedHash
|
42
49
|
# warning: covariant hashes
|
43
50
|
|
@@ -4,10 +4,12 @@
|
|
4
4
|
module T::Types
|
5
5
|
# Takes a list of types. Validates that an object matches all of the types.
|
6
6
|
class Intersection < Base
|
7
|
-
attr_reader :types
|
8
|
-
|
9
7
|
def initialize(types)
|
10
|
-
@
|
8
|
+
@inner_types = types
|
9
|
+
end
|
10
|
+
|
11
|
+
def types
|
12
|
+
@types ||= @inner_types.flat_map do |type|
|
11
13
|
type = T::Utils.resolve_alias(type)
|
12
14
|
if type.is_a?(Intersection)
|
13
15
|
# Simplify nested intersections (mostly so `name` returns a nicer value)
|
@@ -18,19 +20,24 @@ module T::Types
|
|
18
20
|
end.uniq
|
19
21
|
end
|
20
22
|
|
23
|
+
def build_type
|
24
|
+
types
|
25
|
+
nil
|
26
|
+
end
|
27
|
+
|
21
28
|
# overrides Base
|
22
29
|
def name
|
23
|
-
"T.all(#{
|
30
|
+
"T.all(#{types.map(&:name).compact.sort.join(', ')})"
|
24
31
|
end
|
25
32
|
|
26
33
|
# overrides Base
|
27
34
|
def recursively_valid?(obj)
|
28
|
-
|
35
|
+
types.all? {|type| type.recursively_valid?(obj)}
|
29
36
|
end
|
30
37
|
|
31
38
|
# overrides Base
|
32
39
|
def valid?(obj)
|
33
|
-
|
40
|
+
types.all? {|type| type.valid?(obj)}
|
34
41
|
end
|
35
42
|
|
36
43
|
# overrides Base
|
data/lib/types/types/noreturn.rb
CHANGED
data/lib/types/types/proc.rb
CHANGED
@@ -8,21 +8,31 @@ module T::Types
|
|
8
8
|
# At present, we only support fixed-arity procs with no optional or
|
9
9
|
# keyword arguments.
|
10
10
|
class Proc < Base
|
11
|
-
attr_reader :arg_types
|
12
|
-
attr_reader :returns
|
13
|
-
|
14
11
|
def initialize(arg_types, returns)
|
15
|
-
@
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
12
|
+
@inner_arg_types = arg_types
|
13
|
+
@inner_returns = returns
|
14
|
+
end
|
15
|
+
|
16
|
+
def arg_types
|
17
|
+
@arg_types ||= @inner_arg_types.map do |key, raw_type|
|
18
|
+
[key, T::Utils.coerce(raw_type)]
|
19
|
+
end.to_h
|
20
|
+
end
|
21
|
+
|
22
|
+
def returns
|
23
|
+
@returns ||= T::Utils.coerce(@inner_returns)
|
24
|
+
end
|
25
|
+
|
26
|
+
def build_type
|
27
|
+
arg_types
|
28
|
+
returns
|
29
|
+
nil
|
20
30
|
end
|
21
31
|
|
22
32
|
# overrides Base
|
23
33
|
def name
|
24
34
|
args = []
|
25
|
-
|
35
|
+
arg_types.each do |k, v|
|
26
36
|
args << "#{k}: #{v.name}"
|
27
37
|
end
|
28
38
|
"T.proc.params(#{args.join(', ')}).returns(#{returns})"
|
data/lib/types/types/simple.rb
CHANGED
data/lib/types/types/t_enum.rb
CHANGED
@@ -5,7 +5,7 @@ module T::Types
|
|
5
5
|
class TypedArray < TypedEnumerable
|
6
6
|
# overrides Base
|
7
7
|
def name
|
8
|
-
"T::Array[#{
|
8
|
+
"T::Array[#{type.name}]"
|
9
9
|
end
|
10
10
|
|
11
11
|
def underlying_class
|
@@ -60,6 +60,11 @@ module T::Types
|
|
60
60
|
obj.is_a?(Array)
|
61
61
|
end
|
62
62
|
|
63
|
+
def freeze
|
64
|
+
build_type # force lazy initialization before freezing the object
|
65
|
+
super
|
66
|
+
end
|
67
|
+
|
63
68
|
module Private
|
64
69
|
INSTANCE = Untyped.new.freeze
|
65
70
|
end
|
@@ -3,15 +3,22 @@
|
|
3
3
|
|
4
4
|
module T::Types
|
5
5
|
class TypedClass < T::Types::Base
|
6
|
-
attr_reader :type
|
7
|
-
|
8
6
|
def initialize(type)
|
9
|
-
@
|
7
|
+
@inner_type = type
|
8
|
+
end
|
9
|
+
|
10
|
+
def type
|
11
|
+
@type ||= T::Utils.coerce(@inner_type)
|
12
|
+
end
|
13
|
+
|
14
|
+
def build_type
|
15
|
+
type
|
16
|
+
nil
|
10
17
|
end
|
11
18
|
|
12
19
|
# overrides Base
|
13
20
|
def name
|
14
|
-
"T::Class[#{
|
21
|
+
"T::Class[#{type.name}]"
|
15
22
|
end
|
16
23
|
|
17
24
|
def underlying_class
|
@@ -67,6 +74,11 @@ module T::Types
|
|
67
74
|
super(T.untyped)
|
68
75
|
end
|
69
76
|
|
77
|
+
def freeze
|
78
|
+
build_type # force lazy initialization before freezing the object
|
79
|
+
super
|
80
|
+
end
|
81
|
+
|
70
82
|
module Private
|
71
83
|
INSTANCE = Untyped.new.freeze
|
72
84
|
end
|
@@ -77,6 +89,11 @@ module T::Types
|
|
77
89
|
super(T.anything)
|
78
90
|
end
|
79
91
|
|
92
|
+
def freeze
|
93
|
+
build_type # force lazy initialization before freezing the object
|
94
|
+
super
|
95
|
+
end
|
96
|
+
|
80
97
|
module Private
|
81
98
|
INSTANCE = Anything.new.freeze
|
82
99
|
end
|