sorbet-runtime 0.5.10891 → 0.5.10898
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/types/private/class_utils.rb +13 -6
- data/lib/types/private/methods/_methods.rb +26 -8
- data/lib/types/private/methods/call_validation.rb +1 -1
- data/lib/types/private/methods/signature.rb +18 -10
- data/lib/types/struct.rb +2 -2
- data/lib/types/types/simple.rb +4 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3e3ab9e95814ef422f4bf43b68d621a4729536f6fa50463e9e2af6aa5faf2e78
|
4
|
+
data.tar.gz: 77e0c25399ad50c72f0a10d159d808284bd137b203d138e4d1c6590d870d7bd8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c9f7595af100b0883fbe66464d3341ebf6b3a60548b027fd381752db21f6e2f9526b6bdb85af89ac7ab98e6209fc8ccdb63962f4bad2fd8d24ff5a05ed3dbdb8
|
7
|
+
data.tar.gz: 9db28918c2cb24a1f16f16059002d590fce54a12c18fd1aad6dc03d503e905b5156fe916a8abeb1d26437194e371cd65637d9bbca52f9f02ff65bf3a7622ca24
|
@@ -91,10 +91,13 @@ module T::Private::ClassUtils
|
|
91
91
|
end
|
92
92
|
|
93
93
|
# Replaces a method, either by overwriting it (if it is defined directly on `mod`) or by
|
94
|
-
# overriding it (if it is defined by one of mod's ancestors).
|
95
|
-
# on which you can call `bind(...).call(...)`
|
96
|
-
#
|
97
|
-
|
94
|
+
# overriding it (if it is defined by one of mod's ancestors). If `original_only` is
|
95
|
+
# false, returns a ReplacedMethod instance on which you can call `bind(...).call(...)`
|
96
|
+
# to call the original method, or `restore` to restore the original method (by
|
97
|
+
# overwriting or removing the override).
|
98
|
+
#
|
99
|
+
# If `original_only` is true, return the `UnboundMethod` representing the original method.
|
100
|
+
def self.replace_method(mod, name, original_only=false, &blk)
|
98
101
|
original_method = mod.instance_method(name)
|
99
102
|
original_visibility = visibility_method_name(mod, name)
|
100
103
|
original_owner = original_method.owner
|
@@ -120,8 +123,12 @@ module T::Private::ClassUtils
|
|
120
123
|
def_with_visibility(mod, name, original_visibility, &blk)
|
121
124
|
end
|
122
125
|
end
|
123
|
-
new_method = mod.instance_method(name)
|
124
126
|
|
125
|
-
|
127
|
+
if original_only
|
128
|
+
original_method
|
129
|
+
else
|
130
|
+
new_method = mod.instance_method(name)
|
131
|
+
ReplacedMethod.new(mod, original_method, new_method, overwritten, original_visibility)
|
132
|
+
end
|
126
133
|
end
|
127
134
|
end
|
@@ -252,7 +252,7 @@ module T::Private::Methods
|
|
252
252
|
# (or unwrap back to the original method).
|
253
253
|
key = method_owner_and_name_to_key(mod, method_name)
|
254
254
|
unless current_declaration.raw
|
255
|
-
T::Private::ClassUtils.replace_method(mod, method_name) do |*args, &blk|
|
255
|
+
T::Private::ClassUtils.replace_method(mod, method_name, true) do |*args, &blk|
|
256
256
|
method_sig = T::Private::Methods.maybe_run_sig_block_for_key(key)
|
257
257
|
method_sig ||= T::Private::Methods._handle_missing_method_signature(
|
258
258
|
self,
|
@@ -501,19 +501,37 @@ module T::Private::Methods
|
|
501
501
|
return
|
502
502
|
end
|
503
503
|
if is_enabled
|
504
|
-
|
504
|
+
# A cut-down version of T::Private::ClassUtils::ReplacedMethod#restore, because we
|
505
|
+
# should only be resetting final hooks during tests.
|
506
|
+
T::Configuration.without_ruby_warnings do
|
507
|
+
Module.define_method(:included, @old_hooks[0])
|
508
|
+
Module.define_method(:extended, @old_hooks[1])
|
509
|
+
Class.define_method(:inherited, @old_hooks[2])
|
510
|
+
end
|
505
511
|
@old_hooks = nil
|
506
512
|
else
|
507
|
-
old_included = T::Private::ClassUtils.replace_method(Module, :included) do |arg|
|
508
|
-
|
513
|
+
old_included = T::Private::ClassUtils.replace_method(Module, :included, true) do |arg|
|
514
|
+
if T::Configuration::AT_LEAST_RUBY_2_7
|
515
|
+
old_included.bind_call(self, arg)
|
516
|
+
else
|
517
|
+
old_included.bind(self).call(arg)
|
518
|
+
end
|
509
519
|
::T::Private::Methods._hook_impl(arg, false, self)
|
510
520
|
end
|
511
|
-
old_extended = T::Private::ClassUtils.replace_method(Module, :extended) do |arg|
|
512
|
-
|
521
|
+
old_extended = T::Private::ClassUtils.replace_method(Module, :extended, true) do |arg|
|
522
|
+
if T::Configuration::AT_LEAST_RUBY_2_7
|
523
|
+
old_extended.bind_call(self, arg)
|
524
|
+
else
|
525
|
+
old_extended.bind(self).call(arg)
|
526
|
+
end
|
513
527
|
::T::Private::Methods._hook_impl(arg, true, self)
|
514
528
|
end
|
515
|
-
old_inherited = T::Private::ClassUtils.replace_method(Class, :inherited) do |arg|
|
516
|
-
|
529
|
+
old_inherited = T::Private::ClassUtils.replace_method(Class, :inherited, true) do |arg|
|
530
|
+
if T::Configuration::AT_LEAST_RUBY_2_7
|
531
|
+
old_inherited.bind_call(self, arg)
|
532
|
+
else
|
533
|
+
old_inherited.bind(self).call(arg)
|
534
|
+
end
|
517
535
|
::T::Private::Methods._hook_impl(arg, false, self)
|
518
536
|
end
|
519
537
|
@old_hooks = [old_included, old_extended, old_inherited]
|
@@ -14,7 +14,7 @@ module T::Private::Methods::CallValidation
|
|
14
14
|
def self.wrap_method_if_needed(mod, method_sig, original_method)
|
15
15
|
original_visibility = visibility_method_name(mod, method_sig.method_name)
|
16
16
|
if method_sig.mode == T::Private::Methods::Modes.abstract
|
17
|
-
T::Private::ClassUtils.replace_method(mod, method_sig.method_name) do |*args, &blk|
|
17
|
+
T::Private::ClassUtils.replace_method(mod, method_sig.method_name, true) do |*args, &blk|
|
18
18
|
# TODO: write a cop to ensure that abstract methods have an empty body
|
19
19
|
#
|
20
20
|
# We allow abstract methods to be implemented by things further down the ancestor chain.
|
@@ -39,8 +39,6 @@ class T::Private::Methods::Signature
|
|
39
39
|
def initialize(method:, method_name:, raw_arg_types:, raw_return_type:, bind:, mode:, check_level:, on_failure:, parameters: method.parameters, override_allow_incompatible: false, defined_raw: false)
|
40
40
|
@method = method
|
41
41
|
@method_name = method_name
|
42
|
-
@arg_types = []
|
43
|
-
@kwarg_types = {}
|
44
42
|
@block_type = nil
|
45
43
|
@block_name = nil
|
46
44
|
@rest_type = nil
|
@@ -51,8 +49,6 @@ class T::Private::Methods::Signature
|
|
51
49
|
@bind = bind ? T::Utils.coerce(bind) : bind
|
52
50
|
@mode = mode
|
53
51
|
@check_level = check_level
|
54
|
-
@req_arg_count = 0
|
55
|
-
@req_kwarg_names = []
|
56
52
|
@has_rest = false
|
57
53
|
@has_keyrest = false
|
58
54
|
@parameters = parameters
|
@@ -60,6 +56,12 @@ class T::Private::Methods::Signature
|
|
60
56
|
@override_allow_incompatible = override_allow_incompatible
|
61
57
|
@defined_raw = defined_raw
|
62
58
|
|
59
|
+
# Use T.untyped in lieu of T.nilable to try to avoid unnecessary allocations.
|
60
|
+
arg_types = T.let(nil, T.untyped)
|
61
|
+
kwarg_types = T.let(nil, T.untyped)
|
62
|
+
req_arg_count = 0
|
63
|
+
req_kwarg_names = T.let(nil, T.untyped)
|
64
|
+
|
63
65
|
# If sig params are declared but there is a single parameter with a missing name
|
64
66
|
# **and** the method ends with a "=", assume it is a writer method generated
|
65
67
|
# by attr_writer or attr_accessor
|
@@ -109,7 +111,7 @@ class T::Private::Methods::Signature
|
|
109
111
|
|
110
112
|
case param_kind
|
111
113
|
when :req
|
112
|
-
if
|
114
|
+
if (arg_types ? arg_types.length : 0) > req_arg_count
|
113
115
|
# Note that this is actually is supported by Ruby, but it would add complexity to
|
114
116
|
# support it here, and I'm happy to discourage its use anyway.
|
115
117
|
#
|
@@ -120,14 +122,14 @@ class T::Private::Methods::Signature
|
|
120
122
|
# see this error. The simplest resolution is to rename your method.
|
121
123
|
raise "Required params after optional params are not supported in method declarations. Method: #{method_desc}"
|
122
124
|
end
|
123
|
-
|
124
|
-
|
125
|
+
(arg_types ||= []) << [param_name, type]
|
126
|
+
req_arg_count += 1
|
125
127
|
when :opt
|
126
|
-
|
128
|
+
(arg_types ||= []) << [param_name, type]
|
127
129
|
when :key, :keyreq
|
128
|
-
|
130
|
+
(kwarg_types ||= {})[param_name] = type
|
129
131
|
if param_kind == :keyreq
|
130
|
-
|
132
|
+
(req_kwarg_names ||= []) << param_name
|
131
133
|
end
|
132
134
|
when :block
|
133
135
|
@block_name = param_name
|
@@ -146,6 +148,11 @@ class T::Private::Methods::Signature
|
|
146
148
|
|
147
149
|
i += 1
|
148
150
|
end
|
151
|
+
|
152
|
+
@arg_types = arg_types || EMPTY_LIST
|
153
|
+
@kwarg_types = kwarg_types || EMPTY_HASH
|
154
|
+
@req_arg_count = req_arg_count
|
155
|
+
@req_kwarg_names = req_kwarg_names || EMPTY_LIST
|
149
156
|
end
|
150
157
|
|
151
158
|
attr_writer :method_name
|
@@ -238,5 +245,6 @@ class T::Private::Methods::Signature
|
|
238
245
|
"#{@method} at #{loc}"
|
239
246
|
end
|
240
247
|
|
248
|
+
EMPTY_LIST = [].freeze
|
241
249
|
EMPTY_HASH = {}.freeze
|
242
250
|
end
|
data/lib/types/struct.rb
CHANGED
@@ -10,7 +10,7 @@ end
|
|
10
10
|
class T::Struct < T::InexactStruct
|
11
11
|
def self.inherited(subclass)
|
12
12
|
super(subclass)
|
13
|
-
T::Private::ClassUtils.replace_method(subclass.singleton_class, :inherited) do |s|
|
13
|
+
T::Private::ClassUtils.replace_method(subclass.singleton_class, :inherited, true) do |s|
|
14
14
|
super(s)
|
15
15
|
raise "#{self.name} is a subclass of T::Struct and cannot be subclassed"
|
16
16
|
end
|
@@ -23,7 +23,7 @@ class T::ImmutableStruct < T::InexactStruct
|
|
23
23
|
def self.inherited(subclass)
|
24
24
|
super(subclass)
|
25
25
|
|
26
|
-
T::Private::ClassUtils.replace_method(subclass.singleton_class, :inherited) do |s|
|
26
|
+
T::Private::ClassUtils.replace_method(subclass.singleton_class, :inherited, true) do |s|
|
27
27
|
super(s)
|
28
28
|
raise "#{self.name} is a subclass of T::ImmutableStruct and cannot be subclassed"
|
29
29
|
end
|
data/lib/types/types/simple.rb
CHANGED
@@ -4,6 +4,9 @@
|
|
4
4
|
module T::Types
|
5
5
|
# Validates that an object belongs to the specified class.
|
6
6
|
class Simple < Base
|
7
|
+
NAME_METHOD = Module.instance_method(:name)
|
8
|
+
private_constant(:NAME_METHOD)
|
9
|
+
|
7
10
|
attr_reader :raw_type
|
8
11
|
|
9
12
|
def initialize(raw_type)
|
@@ -16,7 +19,7 @@ module T::Types
|
|
16
19
|
#
|
17
20
|
# `name` isn't normally a hot path for types, but it is used in initializing a T::Types::Union,
|
18
21
|
# and so in `T.nilable`, and so in runtime constructions like `x = T.let(nil, T.nilable(Integer))`.
|
19
|
-
@name ||= @raw_type.name.freeze
|
22
|
+
@name ||= (NAME_METHOD.bind(@raw_type).call || @raw_type.name).freeze
|
20
23
|
end
|
21
24
|
|
22
25
|
# overrides Base
|
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.10898
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stripe
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-07-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|