sorbet-runtime 0.5.10813 → 0.5.10820
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 +1 -0
- data/lib/types/_types.rb +12 -0
- data/lib/types/private/methods/signature.rb +22 -16
- data/lib/types/private/types/not_typed.rb +2 -0
- data/lib/types/types/class_of.rb +2 -0
- data/lib/types/types/typed_class.rb +85 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 31e1d95e4d9432b739e9ff5d24434c73387a008a8545730f014ab21b54154706
|
4
|
+
data.tar.gz: 51caacddddeb714f872de2531bef586126df4b01ecaaa15b6925c7588c3684e1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1ebd9c1f1ceabcc31b5923f849c66fd2dfd6ea90ee50cce8340f771d5f135aee783016a60dade22f90b4576e2df1e566046147c55e52b6a5093b28d1c69a4815
|
7
|
+
data.tar.gz: 9ec4e2036b31c3394ecfcaf7c84fcf7fd8c3062ded3c853e041ecd6ae55c65f84f862e51ce8d5c32f0e4c0c29889bf90a7afe094ce8237b13363a2cf327c1fc7
|
data/lib/sorbet-runtime.rb
CHANGED
data/lib/types/_types.rb
CHANGED
@@ -355,4 +355,16 @@ module T
|
|
355
355
|
end
|
356
356
|
end
|
357
357
|
end
|
358
|
+
|
359
|
+
module Class
|
360
|
+
def self.[](type)
|
361
|
+
if type.is_a?(T::Types::Untyped)
|
362
|
+
T::Types::TypedClass::Untyped::Private::INSTANCE
|
363
|
+
elsif type.is_a?(T::Types::Anything)
|
364
|
+
T::Types::TypedClass::Anything::Private::INSTANCE
|
365
|
+
else
|
366
|
+
T::Types::TypedClass::Private::Pool.type_for_module(type)
|
367
|
+
end
|
368
|
+
end
|
369
|
+
end
|
358
370
|
end
|
@@ -8,19 +8,19 @@ class T::Private::Methods::Signature
|
|
8
8
|
:check_level, :parameters, :on_failure, :override_allow_incompatible,
|
9
9
|
:defined_raw
|
10
10
|
|
11
|
-
SIG_EMPTY_DECLARED_PARAMETERS = [nil].freeze
|
12
11
|
UNNAMED_REQUIRED_PARAMETERS = [[:req]].freeze
|
13
12
|
|
14
13
|
def self.new_untyped(method:, mode: T::Private::Methods::Modes.untyped, parameters: method.parameters)
|
15
|
-
# Using `
|
16
|
-
not_typed = T::Private::Types::NotTyped
|
14
|
+
# Using `NotTyped` ensures we'll get an error if we ever try validation on these.
|
15
|
+
not_typed = T::Private::Types::NotTyped::INSTANCE
|
17
16
|
raw_return_type = not_typed
|
18
17
|
# Map missing parameter names to "argN" positionally
|
19
18
|
parameters = parameters.each_with_index.map do |(param_kind, param_name), index|
|
20
19
|
[param_kind, param_name || "arg#{index}"]
|
21
20
|
end
|
22
|
-
raw_arg_types =
|
23
|
-
|
21
|
+
raw_arg_types = {}
|
22
|
+
parameters.each do |_, param_name|
|
23
|
+
raw_arg_types[param_name] = not_typed
|
24
24
|
end
|
25
25
|
|
26
26
|
self.new(
|
@@ -60,21 +60,23 @@ class T::Private::Methods::Signature
|
|
60
60
|
@override_allow_incompatible = override_allow_incompatible
|
61
61
|
@defined_raw = defined_raw
|
62
62
|
|
63
|
-
declared_param_names = raw_arg_types.keys
|
64
63
|
# If sig params are declared but there is a single parameter with a missing name
|
65
64
|
# **and** the method ends with a "=", assume it is a writer method generated
|
66
65
|
# by attr_writer or attr_accessor
|
67
|
-
writer_method =
|
66
|
+
writer_method = !(raw_arg_types.size == 1 && raw_arg_types.key?(nil)) && parameters == UNNAMED_REQUIRED_PARAMETERS && method_name[-1] == "="
|
68
67
|
# For writer methods, map the single parameter to the method name without the "=" at the end
|
69
68
|
parameters = [[:req, method_name[0...-1].to_sym]] if writer_method
|
70
|
-
|
71
|
-
|
72
|
-
|
69
|
+
is_name_missing = parameters.any? {|_, name| !raw_arg_types.key?(name)}
|
70
|
+
if is_name_missing
|
71
|
+
param_names = parameters.map {|_, name| name}
|
72
|
+
missing_names = param_names - raw_arg_types.keys
|
73
73
|
raise "The declaration for `#{method.name}` is missing parameter(s): #{missing_names.join(', ')}"
|
74
|
-
elsif
|
74
|
+
elsif parameters.length == raw_arg_types.size
|
75
75
|
else
|
76
|
-
|
77
|
-
|
76
|
+
param_names = parameters.map {|_, name| name}
|
77
|
+
has_extra_names = parameters.count {|_, name| raw_arg_types.key?(name)} < raw_arg_types.size
|
78
|
+
if has_extra_names
|
79
|
+
extra_names = raw_arg_types.keys - param_names
|
78
80
|
raise "The declaration for `#{method.name}` has extra parameter(s): #{extra_names.join(', ')}"
|
79
81
|
end
|
80
82
|
end
|
@@ -82,8 +84,10 @@ class T::Private::Methods::Signature
|
|
82
84
|
if parameters.size != raw_arg_types.size
|
83
85
|
raise "The declaration for `#{method.name}` has arguments with duplicate names"
|
84
86
|
end
|
87
|
+
i = 0
|
88
|
+
raw_arg_types.each do |type_name, raw_type|
|
89
|
+
param_kind, param_name = parameters[i]
|
85
90
|
|
86
|
-
parameters.zip(raw_arg_types) do |(param_kind, param_name), (type_name, raw_type)|
|
87
91
|
if type_name != param_name
|
88
92
|
hint = ""
|
89
93
|
# Ruby reorders params so that required keyword arguments
|
@@ -97,8 +101,8 @@ class T::Private::Methods::Signature
|
|
97
101
|
end
|
98
102
|
|
99
103
|
raise "Parameter `#{type_name}` is declared out of order (declared as arg number " \
|
100
|
-
"#{
|
101
|
-
"#{
|
104
|
+
"#{i + 1}, defined in the method as arg number " \
|
105
|
+
"#{parameters.index {|_, name| name == type_name} + 1}).#{hint}\nMethod: #{method_desc}"
|
102
106
|
end
|
103
107
|
|
104
108
|
type = T::Utils.coerce(raw_type)
|
@@ -139,6 +143,8 @@ class T::Private::Methods::Signature
|
|
139
143
|
else
|
140
144
|
raise "Unexpected param_kind: `#{param_kind}`. Method: #{method_desc}"
|
141
145
|
end
|
146
|
+
|
147
|
+
i += 1
|
142
148
|
end
|
143
149
|
end
|
144
150
|
|
data/lib/types/types/class_of.rb
CHANGED
@@ -0,0 +1,85 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# typed: true
|
3
|
+
|
4
|
+
module T::Types
|
5
|
+
class TypedClass < T::Types::Base
|
6
|
+
attr_reader :type
|
7
|
+
|
8
|
+
def initialize(type)
|
9
|
+
@type = T::Utils.coerce(type)
|
10
|
+
end
|
11
|
+
|
12
|
+
# overrides Base
|
13
|
+
def name
|
14
|
+
"T::Class[#{@type.name}]"
|
15
|
+
end
|
16
|
+
|
17
|
+
def underlying_class
|
18
|
+
Class
|
19
|
+
end
|
20
|
+
|
21
|
+
# overrides Base
|
22
|
+
def valid?(obj)
|
23
|
+
Class.===(obj)
|
24
|
+
end
|
25
|
+
|
26
|
+
# overrides Base
|
27
|
+
private def subtype_of_single?(type)
|
28
|
+
case type
|
29
|
+
when TypedClass
|
30
|
+
# treat like generics are erased
|
31
|
+
true
|
32
|
+
when Simple
|
33
|
+
Class <= type.raw_type
|
34
|
+
else
|
35
|
+
false
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
module Private
|
40
|
+
module Pool
|
41
|
+
CACHE_FROZEN_OBJECTS =
|
42
|
+
begin
|
43
|
+
ObjectSpace::WeakMap.new[1] = 1
|
44
|
+
true # Ruby 2.7 and newer
|
45
|
+
rescue ArgumentError
|
46
|
+
false # Ruby 2.6 and older
|
47
|
+
end
|
48
|
+
|
49
|
+
@cache = ObjectSpace::WeakMap.new
|
50
|
+
|
51
|
+
def self.type_for_module(mod)
|
52
|
+
cached = @cache[mod]
|
53
|
+
return cached if cached
|
54
|
+
|
55
|
+
type = TypedClass.new(mod)
|
56
|
+
|
57
|
+
if CACHE_FROZEN_OBJECTS || (!mod.frozen? && !type.frozen?)
|
58
|
+
@cache[mod] = type
|
59
|
+
end
|
60
|
+
type
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
class Untyped < TypedClass
|
66
|
+
def initialize
|
67
|
+
super(T.untyped)
|
68
|
+
end
|
69
|
+
|
70
|
+
module Private
|
71
|
+
INSTANCE = Untyped.new.freeze
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
class Anything < TypedClass
|
76
|
+
def initialize
|
77
|
+
super(T.anything)
|
78
|
+
end
|
79
|
+
|
80
|
+
module Private
|
81
|
+
INSTANCE = Anything.new.freeze
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sorbet-runtime
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.10820
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stripe
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-05-
|
11
|
+
date: 2023-05-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|
@@ -233,6 +233,7 @@ files:
|
|
233
233
|
- lib/types/types/type_template.rb
|
234
234
|
- lib/types/types/type_variable.rb
|
235
235
|
- lib/types/types/typed_array.rb
|
236
|
+
- lib/types/types/typed_class.rb
|
236
237
|
- lib/types/types/typed_enumerable.rb
|
237
238
|
- lib/types/types/typed_enumerator.rb
|
238
239
|
- lib/types/types/typed_enumerator_chain.rb
|