sorbet-runtime 0.6.12645 → 0.6.12793

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 175eb48572fc002a5d56441bd62522b66f8a83364dbdcd4d7600f7e3792308b5
4
- data.tar.gz: 961dbe8f3e80ab8f494004c1eab1adc11c180463405a8d01554421687960657c
3
+ metadata.gz: cebe71c475edd5bcf34591ff0144ff3830b27cdfafa46abe60baff07d92e8d89
4
+ data.tar.gz: 37b350eeae2cc1ab69f24f944a56a42582855f1c39e86bfd4d39cba685b75b0a
5
5
  SHA512:
6
- metadata.gz: 87a91dd727979933d8371337fdba628951020ffe5226d48f362a7734c6d9795d1e3de6d4c5193726e89b74f243ddc710fa6fee4a432f2655b3bd8f798a447f8a
7
- data.tar.gz: 44040ae432325a2cba5452e84e79c0e6138ef5e13afbcc844d8b55ad86ab481a73f293e580050d8143c041d51e851b3c6aa4956e62ca5ff9bac05dc0e841615d
6
+ metadata.gz: 273367cb15c029f3a777051605387a4b6e800edf45ee05f80eadd5a65c6d8a357dbe25ec31533992835fd9f8a96cff2369408e2572027c666bc7896d993f790c
7
+ data.tar.gz: c237722b26b2456014f2347b16b64d159603d4081664a519cb029aa6d719c64ed56c47c50fa4543d8de8f63717956cd6b5c56c333d85cc4f7725db7726f9ce1f
@@ -85,6 +85,7 @@ require_relative 'types/boolean'
85
85
 
86
86
  # Depends on types/utils
87
87
  require_relative 'types/types/typed_array'
88
+ require_relative 'types/types/typed_module'
88
89
  require_relative 'types/types/typed_class'
89
90
 
90
91
  # Props dependencies
@@ -114,6 +115,5 @@ require_relative 'types/props/private/parser'
114
115
  require_relative 'types/props/generated_code_validation'
115
116
 
116
117
  require_relative 'types/struct'
117
- require_relative 'types/non_forcing_constants'
118
118
 
119
119
  require_relative 'types/compatibility_patches'
data/lib/types/_types.rb CHANGED
@@ -367,4 +367,16 @@ module T
367
367
  end
368
368
  end
369
369
  end
370
+
371
+ module Module
372
+ def self.[](type)
373
+ if type.is_a?(T::Types::Untyped)
374
+ T::Types::TypedModule::Untyped::Private::INSTANCE
375
+ elsif type.is_a?(T::Types::Anything)
376
+ T::Types::TypedModule::Anything::Private::INSTANCE
377
+ else
378
+ T::Types::TypedModule::Private::Pool.type_for_module(type)
379
+ end
380
+ end
381
+ end
370
382
  end
@@ -2,9 +2,6 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module T::Configuration
5
- # Cache this comparisonn to avoid two allocations all over the place.
6
- AT_LEAST_RUBY_2_7 = Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.7')
7
-
8
5
  # Announces to Sorbet that we are currently in a test environment, so it
9
6
  # should treat any sigs which are marked `.checked(:tests)` as if they were
10
7
  # just a normal sig.
@@ -448,11 +445,7 @@ module T::Configuration
448
445
  MODULE_NAME = Module.instance_method(:name)
449
446
  private_constant :MODULE_NAME
450
447
 
451
- @default_module_name_mangler = if T::Configuration::AT_LEAST_RUBY_2_7
452
- ->(type) { MODULE_NAME.bind_call(type) }
453
- else
454
- ->(type) { MODULE_NAME.bind(type).call } # rubocop:disable Performance/BindCall
455
- end
448
+ @default_module_name_mangler = ->(type) { MODULE_NAME.bind_call(type) }
456
449
 
457
450
  @module_name_mangler = nil
458
451
 
@@ -462,7 +455,7 @@ module T::Configuration
462
455
 
463
456
  # Set to override the default behavior for converting types
464
457
  # to names in generated code. Used by the runtime implementation
465
- # associated with `--stripe-packages` mode.
458
+ # associated with `--sorbet-packages` mode.
466
459
  #
467
460
  # @param [Lambda, Proc, nil] handler Proc that converts a type (Class/Module)
468
461
  # to a String (pass nil to reset to default behavior)
data/lib/types/enum.rb CHANGED
@@ -273,7 +273,7 @@ class T::Enum
273
273
 
274
274
  ### Private implementation ###
275
275
 
276
- UNSET = T.let(Module.new.freeze, Module)
276
+ UNSET = T.let(Module.new.freeze, T::Module[T.anything])
277
277
  private_constant :UNSET
278
278
 
279
279
  sig { params(serialized_val: SerializedVal).void }
@@ -257,10 +257,8 @@ module T::Private::Methods
257
257
  # make sure to keep changes in sync.
258
258
  elsif method_sig.check_level == :always || (method_sig.check_level == :tests && T::Private::RuntimeLevels.check_tests?)
259
259
  CallValidation.validate_call(self, original_method, method_sig, args, blk)
260
- elsif T::Configuration::AT_LEAST_RUBY_2_7
261
- original_method.bind_call(self, *args, &blk)
262
260
  else
263
- original_method.bind(self).call(*args, &blk) # rubocop:disable Performance/BindCall
261
+ original_method.bind_call(self, *args, &blk)
264
262
  end
265
263
  end
266
264
  end
@@ -530,27 +528,15 @@ module T::Private::Methods
530
528
  @old_hooks = nil
531
529
  else
532
530
  old_included = T::Private::ClassUtils.replace_method(Module, :included, true) do |arg|
533
- if T::Configuration::AT_LEAST_RUBY_2_7
534
- old_included.bind_call(self, arg)
535
- else
536
- old_included.bind(self).call(arg) # rubocop:disable Performance/BindCall
537
- end
531
+ old_included.bind_call(self, arg)
538
532
  ::T::Private::Methods._hook_impl(arg, false, self)
539
533
  end
540
534
  old_extended = T::Private::ClassUtils.replace_method(Module, :extended, true) do |arg|
541
- if T::Configuration::AT_LEAST_RUBY_2_7
542
- old_extended.bind_call(self, arg)
543
- else
544
- old_extended.bind(self).call(arg) # rubocop:disable Performance/BindCall
545
- end
535
+ old_extended.bind_call(self, arg)
546
536
  ::T::Private::Methods._hook_impl(arg, true, self)
547
537
  end
548
538
  old_inherited = T::Private::ClassUtils.replace_method(Class, :inherited, true) do |arg|
549
- if T::Configuration::AT_LEAST_RUBY_2_7
550
- old_inherited.bind_call(self, arg)
551
- else
552
- old_inherited.bind(self).call(arg) # rubocop:disable Performance/BindCall
553
- end
539
+ old_inherited.bind_call(self, arg)
554
540
  ::T::Private::Methods._hook_impl(arg, false, self)
555
541
  end
556
542
  @old_hooks = [old_included, old_extended, old_inherited]
@@ -79,13 +79,13 @@ module T::Private::Methods::CallValidation
79
79
 
80
80
  # All the types for which valid? unconditionally returns `true`
81
81
  return_is_ignorable =
82
- (method_sig.return_type.equal?(T::Types::Untyped::Private::INSTANCE) ||
83
- method_sig.return_type.equal?(T::Types::Anything::Private::INSTANCE) ||
84
- method_sig.return_type.equal?(T::Types::AttachedClassType::Private::INSTANCE) ||
85
- method_sig.return_type.equal?(T::Types::SelfType::Private::INSTANCE) ||
86
- method_sig.return_type.is_a?(T::Types::TypeParameter) ||
87
- method_sig.return_type.is_a?(T::Types::TypeVariable) ||
88
- (method_sig.return_type.is_a?(T::Types::Simple) && method_sig.return_type.raw_type.equal?(BasicObject)))
82
+ method_sig.return_type.equal?(T::Types::Untyped::Private::INSTANCE) ||
83
+ method_sig.return_type.equal?(T::Types::Anything::Private::INSTANCE) ||
84
+ method_sig.return_type.equal?(T::Types::AttachedClassType::Private::INSTANCE) ||
85
+ method_sig.return_type.equal?(T::Types::SelfType::Private::INSTANCE) ||
86
+ method_sig.return_type.is_a?(T::Types::TypeParameter) ||
87
+ method_sig.return_type.is_a?(T::Types::TypeVariable) ||
88
+ (method_sig.return_type.is_a?(T::Types::Simple) && method_sig.return_type.raw_type.equal?(BasicObject))
89
89
 
90
90
  returns_anything_method = all_args_are_simple && return_is_ignorable
91
91
 
@@ -176,7 +176,7 @@ module T::Private::Methods::CallValidation
176
176
  # this code is sig validation code.
177
177
  # Please issue `finish` to step out of it
178
178
 
179
- return_value = T::Configuration::AT_LEAST_RUBY_2_7 ? original_method.bind_call(instance, *args, &blk) : original_method.bind(instance).call(*args, &blk)
179
+ return_value = original_method.bind_call(instance, *args, &blk)
180
180
 
181
181
  # The only type that is allowed to change the return value is `.void`.
182
182
  # It ignores what you returned and changes it to be a private singleton.
@@ -279,7 +279,7 @@ module T::Private::Methods::CallValidation
279
279
  # this code is sig validation code.
280
280
  # Please issue `finish` to step out of it
281
281
 
282
- return_value = T::Configuration::AT_LEAST_RUBY_2_7 ? original_method.bind_call(instance, *args, &blk) : original_method.bind(instance).call(*args, &blk)
282
+ return_value = original_method.bind_call(instance, *args, &blk)
283
283
 
284
284
  # The only type that is allowed to change the return value is `.void`.
285
285
  # It ignores what you returned and changes it to be a private singleton.
@@ -332,8 +332,4 @@ module T::Private::Methods::CallValidation
332
332
  end
333
333
  end
334
334
 
335
- if T::Configuration::AT_LEAST_RUBY_2_7
336
- require_relative './call_validation_2_7'
337
- else
338
- require_relative './call_validation_2_6'
339
- end
335
+ require_relative './call_validation_2_7'
@@ -50,7 +50,7 @@ module T::Props
50
50
  sig { abstract.params(scalar: T.untyped).returns(T.untyped).checked(:never) }
51
51
  def deserialize(scalar); end
52
52
 
53
- sig { override.params(_base: Module).void }
53
+ sig { override.params(_base: T::Module[T.anything]).void }
54
54
  def self.included(_base)
55
55
  super
56
56
 
@@ -13,8 +13,10 @@ class T::Props::Decorator
13
13
  Rules = T.type_alias { T::Hash[Symbol, T.untyped] }
14
14
  DecoratedInstance = T.type_alias { Object } # Would be T::Props, but that produces circular reference errors in some circumstances
15
15
  PropType = T.type_alias { T::Types::Base }
16
- PropTypeOrClass = T.type_alias { T.any(PropType, Module) }
16
+ PropTypeOrClass = T.type_alias { T.any(PropType, T::Module[T.anything]) }
17
17
  OverrideRules = T.type_alias { T::Hash[Symbol, {allow_incompatible: T::Boolean}] }
18
+ DecoratedClassType = T.type_alias { T.all(T::Module[T.anything], T::Props::ClassMethods) }
19
+ private_constant :DecoratedClassType
18
20
 
19
21
  class NoRulesError < StandardError; end
20
22
 
@@ -38,7 +40,7 @@ class T::Props::Decorator
38
40
 
39
41
  sig { params(klass: T.untyped).void.checked(:never) }
40
42
  def initialize(klass)
41
- @class = T.let(klass, T.all(Module, T::Props::ClassMethods))
43
+ @class = T.let(klass, DecoratedClassType)
42
44
  @class.plugins.each do |mod|
43
45
  T::Props::Plugin::Private.apply_decorator_methods(mod, self)
44
46
  end
@@ -102,7 +104,7 @@ class T::Props::Decorator
102
104
  end
103
105
 
104
106
  # checked(:never) - O(prop accesses)
105
- sig { returns(T.all(Module, T::Props::ClassMethods)).checked(:never) }
107
+ sig { returns(DecoratedClassType).checked(:never) }
106
108
  def decorated_class
107
109
  @class
108
110
  end
@@ -207,7 +209,7 @@ class T::Props::Decorator
207
209
  params(
208
210
  instance: DecoratedInstance,
209
211
  prop: Symbol,
210
- foreign_class: Module,
212
+ foreign_class: T::Module[T.anything],
211
213
  rules: Rules,
212
214
  opts: T::Hash[Symbol, T.untyped],
213
215
  )
@@ -226,7 +228,7 @@ class T::Props::Decorator
226
228
  sig do
227
229
  params(
228
230
  name: Symbol,
229
- cls: Module,
231
+ cls: T::Module[T.anything],
230
232
  rules: Rules,
231
233
  type: PropTypeOrClass
232
234
  )
@@ -264,9 +266,9 @@ class T::Props::Decorator
264
266
  nil
265
267
  end
266
268
 
267
- SAFE_NAME = T.let(/\A[A-Za-z_][A-Za-z0-9_-]*\z/.freeze, Regexp, checked: false)
269
+ SAFE_NAME = T.let(/\A[A-Za-z_][A-Za-z0-9_-]*\z/, Regexp, checked: false)
268
270
  # Should be exactly the same as `SAFE_NAME`, but with a leading `@`.
269
- SAFE_ACCESSOR_KEY_NAME = T.let(/\A@[A-Za-z_][A-Za-z0-9_-]*\z/.freeze, Regexp, checked: false)
271
+ SAFE_ACCESSOR_KEY_NAME = T.let(/\A@[A-Za-z_][A-Za-z0-9_-]*\z/, Regexp, checked: false)
270
272
 
271
273
  # Used to validate both prop names and serialized forms
272
274
  sig { params(name: T.any(Symbol, String)).void.checked(:never) }
@@ -277,7 +279,7 @@ class T::Props::Decorator
277
279
  end
278
280
 
279
281
  # This converts the type from a T::Type to a regular old ruby class.
280
- sig { params(type: T::Types::Base).returns(Module).checked(:never) }
282
+ sig { params(type: T::Types::Base).returns(T::Module[T.anything]).checked(:never) }
281
283
  private def convert_type_to_class(type)
282
284
  case type
283
285
  when T::Types::TypedArray, T::Types::FixedArray
@@ -327,9 +329,9 @@ class T::Props::Decorator
327
329
  sig(:final) { params(name: Symbol).returns(T::Boolean).checked(:never) }
328
330
  private def method_defined_on_ancestor?(name)
329
331
  (@class.method_defined?(name) || @class.private_method_defined?(name)) &&
330
- # Unfortunately, older versions of ruby don't allow the second parameter on
331
- # `private_method_defined?`.
332
- (!@class.method_defined?(name, false) && !@class.private_method_defined?(name, false))
332
+ # Unfortunately, older versions of ruby don't allow the second parameter on
333
+ # `private_method_defined?`.
334
+ !@class.method_defined?(name, false) && !@class.private_method_defined?(name, false)
333
335
  end
334
336
 
335
337
  sig(:final) { params(name: Symbol, rules: Rules).void.checked(:never) }
@@ -586,7 +588,7 @@ class T::Props::Decorator
586
588
  sig do
587
589
  params(
588
590
  prop_name: Symbol,
589
- prop_cls: Module,
591
+ prop_cls: T::Module[T.anything],
590
592
  rules: Rules,
591
593
  foreign: T.untyped,
592
594
  )
@@ -629,10 +631,10 @@ class T::Props::Decorator
629
631
  #
630
632
  # This gets called when a module or class that extends T::Props gets included, extended,
631
633
  # prepended, or inherited.
632
- sig { params(child: Module).void.checked(:never) }
634
+ sig { params(child: T::Module[T.anything]).void.checked(:never) }
633
635
  def model_inherited(child)
634
636
  child.extend(T::Props::ClassMethods)
635
- child = T.cast(child, T.all(Module, T::Props::ClassMethods))
637
+ child = T.cast(child, DecoratedClassType)
636
638
 
637
639
  child.plugins.concat(decorated_class.plugins)
638
640
  decorated_class.plugins.each do |mod|
@@ -719,19 +721,19 @@ class T::Props::Decorator
719
721
  result
720
722
  end
721
723
 
722
- sig { params(child: T.all(Module, T::Props::ClassMethods), prop: Symbol).returns(T::Boolean).checked(:never) }
724
+ sig { params(child: DecoratedClassType, prop: Symbol).returns(T::Boolean).checked(:never) }
723
725
  private def clobber_getter?(child, prop)
724
726
  !!(child.decorator.method(:prop_get).owner != method(:prop_get).owner &&
725
727
  child.instance_method(prop).source_location&.first == __FILE__)
726
728
  end
727
729
 
728
- sig { params(child: T.all(Module, T::Props::ClassMethods), prop: Symbol).returns(T::Boolean).checked(:never) }
730
+ sig { params(child: DecoratedClassType, prop: Symbol).returns(T::Boolean).checked(:never) }
729
731
  private def clobber_setter?(child, prop)
730
732
  !!(child.decorator.method(:prop_set).owner != method(:prop_set).owner &&
731
733
  child.instance_method("#{prop}=").source_location&.first == __FILE__)
732
734
  end
733
735
 
734
- sig { params(mod: Module).void.checked(:never) }
736
+ sig { params(mod: T::Module[T.anything]).void.checked(:never) }
735
737
  def plugin(mod)
736
738
  decorated_class.plugins << mod
737
739
  T::Props::Plugin::Private.apply_class_methods(mod, decorated_class)
@@ -27,10 +27,10 @@ module T::Props
27
27
  sig { abstract.params(instance: T.all(T::Props::Optional, Object)).void.checked(:never) }
28
28
  def set_default(instance); end
29
29
 
30
- NO_CLONE_TYPES = T.let([TrueClass, FalseClass, NilClass, Symbol, Numeric, T::Enum].freeze, T::Array[Module])
30
+ NO_CLONE_TYPES = T.let([TrueClass, FalseClass, NilClass, Symbol, Numeric, T::Enum].freeze, T::Array[T::Module[T.anything]])
31
31
 
32
32
  # checked(:never) - Rules hash is expensive to check
33
- sig { params(cls: Module, rules: T::Hash[Symbol, T.untyped]).returns(T.nilable(ApplyDefault)).checked(:never) }
33
+ sig { params(cls: T::Module[T.anything], rules: T::Hash[Symbol, T.untyped]).returns(T.nilable(ApplyDefault)).checked(:never) }
34
34
  def self.for(cls, rules)
35
35
  accessor_key = rules.fetch(:accessor_key)
36
36
  setter = rules.fetch(:setter_proc)
@@ -138,7 +138,7 @@ module T::Props
138
138
  # checked(:never) - We do this with `T.let` instead
139
139
  sig do
140
140
  params(
141
- cls: Module,
141
+ cls: T::Module[T.anything],
142
142
  factory: T.any(Proc, Method),
143
143
  accessor_key: Symbol,
144
144
  setter_proc: SetterFactory::SetterProc,
@@ -147,7 +147,7 @@ module T::Props
147
147
  .checked(:never)
148
148
  end
149
149
  def initialize(cls, factory, accessor_key, setter_proc)
150
- @class = T.let(cls, Module)
150
+ @class = T.let(cls, T::Module[T.anything])
151
151
  @factory = T.let(factory, T.any(Proc, Method))
152
152
  super(accessor_key, setter_proc)
153
153
  end
@@ -20,7 +20,7 @@ module T::Props
20
20
 
21
21
  NO_TRANSFORM_TYPES = T.let(
22
22
  [TrueClass, FalseClass, NilClass, Symbol, String].freeze,
23
- T::Array[Module],
23
+ T::Array[T::Module[T.anything]],
24
24
  )
25
25
  private_constant :NO_TRANSFORM_TYPES
26
26
 
@@ -151,7 +151,7 @@ module T::Props
151
151
  end
152
152
  end
153
153
 
154
- sig { params(varname: String, type: Module, mode: ModeType).returns(String).checked(:never) }
154
+ sig { params(varname: String, type: T::Module[T.anything], mode: ModeType).returns(String).checked(:never) }
155
155
  private_class_method def self.handle_serializable_subtype(varname, type, mode)
156
156
  case mode
157
157
  when Serialize
@@ -164,7 +164,7 @@ module T::Props
164
164
  end
165
165
  end
166
166
 
167
- sig { params(varname: String, type: Module, mode: ModeType).returns(String).checked(:never) }
167
+ sig { params(varname: String, type: T::Module[T.anything], mode: ModeType).returns(String).checked(:never) }
168
168
  private_class_method def self.handle_custom_type(varname, type, mode)
169
169
  case mode
170
170
  when Serialize
@@ -177,7 +177,7 @@ module T::Props
177
177
  end
178
178
  end
179
179
 
180
- sig { params(type: Module).returns(T.nilable(String)).checked(:never) }
180
+ sig { params(type: T::Module[T.anything]).returns(T.nilable(String)).checked(:never) }
181
181
  private_class_method def self.module_name(type)
182
182
  T::Configuration.module_name_mangler.call(type)
183
183
  end
@@ -12,7 +12,7 @@ module T::Props
12
12
 
13
13
  sig do
14
14
  params(
15
- klass: T.all(Module, T::Props::ClassMethods),
15
+ klass: T.all(T::Module[T.anything], T::Props::ClassMethods),
16
16
  prop: Symbol,
17
17
  rules: T::Hash[Symbol, T.untyped]
18
18
  )
@@ -53,8 +53,8 @@ module T::Props
53
53
  params(
54
54
  prop: Symbol,
55
55
  accessor_key: Symbol,
56
- non_nil_type: Module,
57
- klass: T.all(Module, T::Props::ClassMethods),
56
+ non_nil_type: T::Module[T.anything],
57
+ klass: T.all(T::Module[T.anything], T::Props::ClassMethods),
58
58
  )
59
59
  .returns([SetterProc, ValueValidationProc])
60
60
  end
@@ -89,7 +89,7 @@ module T::Props
89
89
  prop: Symbol,
90
90
  accessor_key: Symbol,
91
91
  non_nil_type: T::Types::Base,
92
- klass: T.all(Module, T::Props::ClassMethods),
92
+ klass: T.all(T::Module[T.anything], T::Props::ClassMethods),
93
93
  validate: T.nilable(ValidateProc)
94
94
  )
95
95
  .returns([SetterProc, ValueValidationProc])
@@ -138,8 +138,8 @@ module T::Props
138
138
  params(
139
139
  prop: Symbol,
140
140
  accessor_key: Symbol,
141
- non_nil_type: Module,
142
- klass: T.all(Module, T::Props::ClassMethods),
141
+ non_nil_type: T::Module[T.anything],
142
+ klass: T.all(T::Module[T.anything], T::Props::ClassMethods),
143
143
  )
144
144
  .returns([SetterProc, ValueValidationProc])
145
145
  end
@@ -174,7 +174,7 @@ module T::Props
174
174
  prop: Symbol,
175
175
  accessor_key: Symbol,
176
176
  non_nil_type: T::Types::Base,
177
- klass: T.all(Module, T::Props::ClassMethods),
177
+ klass: T.all(T::Module[T.anything], T::Props::ClassMethods),
178
178
  validate: T.nilable(ValidateProc),
179
179
  )
180
180
  .returns([SetterProc, ValueValidationProc])
@@ -225,9 +225,9 @@ module T::Props
225
225
 
226
226
  sig do
227
227
  params(
228
- klass: T.all(Module, T::Props::ClassMethods),
228
+ klass: T.all(T::Module[T.anything], T::Props::ClassMethods),
229
229
  prop: Symbol,
230
- type: T.any(T::Types::Base, Module),
230
+ type: T.any(T::Types::Base, T::Module[T.anything]),
231
231
  val: T.untyped,
232
232
  )
233
233
  .void
@@ -20,9 +20,9 @@ module T::Props::TypeValidation
20
20
  sig do
21
21
  params(
22
22
  name: T.any(Symbol, String),
23
- _cls: Module,
23
+ _cls: T::Module[T.anything],
24
24
  rules: T::Hash[Symbol, T.untyped],
25
- type: T.any(T::Types::Base, Module)
25
+ type: T.any(T::Types::Base, T::Module[T.anything])
26
26
  )
27
27
  .void
28
28
  .checked(:never)
@@ -31,7 +31,7 @@ module T::Types
31
31
  @type.is_a?(other.type.singleton_class)
32
32
  when Simple
33
33
  @type.is_a?(other.raw_type)
34
- when TypedClass
34
+ when TypedClass, TypedModule
35
35
  @type.is_a?(other.underlying_class)
36
36
  else
37
37
  false
@@ -41,7 +41,7 @@ module T::Types
41
41
  case other
42
42
  when Simple
43
43
  @raw_type <= other.raw_type
44
- when TypedClass
44
+ when TypedClass, TypedModule
45
45
  # This case is a bit odd--we would have liked to solve this like we do
46
46
  # for `T::Array` et al., but don't for backwards compatibility.
47
47
  # See `type_for_module` below.
@@ -103,9 +103,9 @@ module T::Types
103
103
  elsif !Object.autoload?(:Set) && Object.const_defined?(:Set) && mod == ::Set
104
104
  T::Set[T.untyped]
105
105
  else
106
- # ideally we would have a case mapping from ::Class -> T::Class here
107
- # but for backwards compatibility we don't have that, and instead
108
- # have a special case in subtype_of_single?
106
+ # ideally we would have a case mapping from ::Class -> T::Class and
107
+ # ::Module -> T::Module here but for backwards compatibility we
108
+ # don't have that, and instead have a special case in subtype_of_single?
109
109
  Simple.new(mod)
110
110
  end
111
111
 
@@ -33,7 +33,7 @@ module T::Types
33
33
  # overrides Base
34
34
  private def subtype_of_single?(type)
35
35
  case type
36
- when TypedClass
36
+ when TypedClass, TypedModule
37
37
  # treat like generics are erased
38
38
  true
39
39
  when Simple
@@ -38,14 +38,12 @@ module T::Types
38
38
  return false unless obj.is_a?(Enumerable)
39
39
  case obj
40
40
  when Array
41
- begin
42
- it = 0
43
- while it < obj.count
44
- return false unless type.recursively_valid?(obj[it])
45
- it += 1
46
- end
47
- true
41
+ it = 0
42
+ while it < obj.count
43
+ return false unless type.recursively_valid?(obj[it])
44
+ it += 1
48
45
  end
46
+ true
49
47
  when Hash
50
48
  type_ = self.type
51
49
  return false unless type_.is_a?(FixedArray)
@@ -168,11 +166,7 @@ module T::Types
168
166
  obj.class
169
167
  else
170
168
  # This is a specialized enumerable type, just return the class.
171
- if T::Configuration::AT_LEAST_RUBY_2_7
172
- Object.instance_method(:class).bind_call(obj)
173
- else
174
- Object.instance_method(:class).bind(obj).call # rubocop:disable Performance/BindCall
175
- end
169
+ Object.instance_method(:class).bind_call(obj)
176
170
  end
177
171
  end
178
172
 
@@ -0,0 +1,102 @@
1
+ # frozen_string_literal: true
2
+ # typed: true
3
+
4
+ module T::Types
5
+ class TypedModule < T::Types::Base
6
+ def initialize(type)
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
17
+ end
18
+
19
+ # overrides Base
20
+ def name
21
+ "T::Module[#{type.name}]"
22
+ end
23
+
24
+ def underlying_class
25
+ Module
26
+ end
27
+
28
+ # overrides Base
29
+ def valid?(obj)
30
+ Module.===(obj)
31
+ end
32
+
33
+ # overrides Base
34
+ private def subtype_of_single?(type)
35
+ case type
36
+ when TypedModule
37
+ # treat like generics are erased
38
+ true
39
+ when Simple
40
+ Module <= type.raw_type
41
+ else
42
+ false
43
+ end
44
+ end
45
+
46
+ module Private
47
+ module Pool
48
+ CACHE_FROZEN_OBJECTS =
49
+ begin
50
+ ObjectSpace::WeakMap.new[1] = 1
51
+ true # Ruby 2.7 and newer
52
+ rescue ArgumentError
53
+ false # Ruby 2.6 and older
54
+ end
55
+
56
+ @cache = ObjectSpace::WeakMap.new
57
+
58
+ def self.type_for_module(mod)
59
+ cached = @cache[mod]
60
+ return cached if cached
61
+
62
+ type = TypedModule.new(mod)
63
+
64
+ if CACHE_FROZEN_OBJECTS || (!mod.frozen? && !type.frozen?)
65
+ @cache[mod] = type
66
+ end
67
+ type
68
+ end
69
+ end
70
+ end
71
+
72
+ class Untyped < TypedModule
73
+ def initialize
74
+ super(T::Types::Untyped::Private::INSTANCE)
75
+ end
76
+
77
+ def freeze
78
+ build_type # force lazy initialization before freezing the object
79
+ super
80
+ end
81
+
82
+ module Private
83
+ INSTANCE = Untyped.new.freeze
84
+ end
85
+ end
86
+
87
+ class Anything < TypedModule
88
+ def initialize
89
+ super(T.anything)
90
+ end
91
+
92
+ def freeze
93
+ build_type # force lazy initialization before freezing the object
94
+ super
95
+ end
96
+
97
+ module Private
98
+ INSTANCE = Anything.new.freeze
99
+ end
100
+ end
101
+ end
102
+ end
@@ -14,7 +14,6 @@ module T::Types
14
14
 
15
15
  # overrides Base
16
16
  def recursively_valid?(obj)
17
- # Re-implements non_forcing_is_a?
18
17
  return false if Object.autoload?(:Set) # Set is meant to be autoloaded but not yet loaded, this value can't be a Set
19
18
  return false unless Object.const_defined?(:Set) # Set is not loaded yet
20
19
  obj.is_a?(Set) && super
@@ -22,7 +21,6 @@ module T::Types
22
21
 
23
22
  # overrides Base
24
23
  def valid?(obj)
25
- # Re-implements non_forcing_is_a?
26
24
  return false if Object.autoload?(:Set) # Set is meant to be autoloaded but not yet loaded, this value can't be a Set
27
25
  return false unless Object.const_defined?(:Set) # Set is not loaded yet
28
26
  obj.is_a?(Set)
@@ -41,7 +39,6 @@ module T::Types
41
39
  end
42
40
 
43
41
  def valid?(obj)
44
- # Re-implements non_forcing_is_a?
45
42
  return false if Object.autoload?(:Set) # Set is meant to be autoloaded but not yet loaded, this value can't be a Set
46
43
  return false unless Object.const_defined?(:Set) # Set is not loaded yet
47
44
  obj.is_a?(Set)