sorbet-runtime 0.5.5841

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.
Files changed (84) hide show
  1. checksums.yaml +7 -0
  2. data/lib/sorbet-runtime.rb +116 -0
  3. data/lib/types/_types.rb +285 -0
  4. data/lib/types/abstract_utils.rb +50 -0
  5. data/lib/types/boolean.rb +8 -0
  6. data/lib/types/compatibility_patches.rb +95 -0
  7. data/lib/types/configuration.rb +428 -0
  8. data/lib/types/enum.rb +349 -0
  9. data/lib/types/generic.rb +23 -0
  10. data/lib/types/helpers.rb +39 -0
  11. data/lib/types/interface_wrapper.rb +158 -0
  12. data/lib/types/non_forcing_constants.rb +51 -0
  13. data/lib/types/private/abstract/data.rb +36 -0
  14. data/lib/types/private/abstract/declare.rb +48 -0
  15. data/lib/types/private/abstract/hooks.rb +43 -0
  16. data/lib/types/private/abstract/validate.rb +128 -0
  17. data/lib/types/private/casts.rb +22 -0
  18. data/lib/types/private/class_utils.rb +111 -0
  19. data/lib/types/private/decl_state.rb +30 -0
  20. data/lib/types/private/final.rb +51 -0
  21. data/lib/types/private/methods/_methods.rb +460 -0
  22. data/lib/types/private/methods/call_validation.rb +1149 -0
  23. data/lib/types/private/methods/decl_builder.rb +228 -0
  24. data/lib/types/private/methods/modes.rb +16 -0
  25. data/lib/types/private/methods/signature.rb +196 -0
  26. data/lib/types/private/methods/signature_validation.rb +229 -0
  27. data/lib/types/private/mixins/mixins.rb +27 -0
  28. data/lib/types/private/retry.rb +10 -0
  29. data/lib/types/private/runtime_levels.rb +56 -0
  30. data/lib/types/private/sealed.rb +65 -0
  31. data/lib/types/private/types/not_typed.rb +23 -0
  32. data/lib/types/private/types/string_holder.rb +26 -0
  33. data/lib/types/private/types/type_alias.rb +26 -0
  34. data/lib/types/private/types/void.rb +34 -0
  35. data/lib/types/profile.rb +31 -0
  36. data/lib/types/props/_props.rb +161 -0
  37. data/lib/types/props/constructor.rb +40 -0
  38. data/lib/types/props/custom_type.rb +108 -0
  39. data/lib/types/props/decorator.rb +672 -0
  40. data/lib/types/props/errors.rb +8 -0
  41. data/lib/types/props/generated_code_validation.rb +268 -0
  42. data/lib/types/props/has_lazily_specialized_methods.rb +92 -0
  43. data/lib/types/props/optional.rb +81 -0
  44. data/lib/types/props/plugin.rb +37 -0
  45. data/lib/types/props/pretty_printable.rb +107 -0
  46. data/lib/types/props/private/apply_default.rb +170 -0
  47. data/lib/types/props/private/deserializer_generator.rb +165 -0
  48. data/lib/types/props/private/parser.rb +32 -0
  49. data/lib/types/props/private/serde_transform.rb +192 -0
  50. data/lib/types/props/private/serializer_generator.rb +77 -0
  51. data/lib/types/props/private/setter_factory.rb +134 -0
  52. data/lib/types/props/serializable.rb +330 -0
  53. data/lib/types/props/type_validation.rb +111 -0
  54. data/lib/types/props/utils.rb +59 -0
  55. data/lib/types/props/weak_constructor.rb +67 -0
  56. data/lib/types/runtime_profiled.rb +24 -0
  57. data/lib/types/sig.rb +30 -0
  58. data/lib/types/struct.rb +18 -0
  59. data/lib/types/types/attached_class.rb +37 -0
  60. data/lib/types/types/base.rb +151 -0
  61. data/lib/types/types/class_of.rb +38 -0
  62. data/lib/types/types/enum.rb +42 -0
  63. data/lib/types/types/fixed_array.rb +60 -0
  64. data/lib/types/types/fixed_hash.rb +59 -0
  65. data/lib/types/types/intersection.rb +37 -0
  66. data/lib/types/types/noreturn.rb +29 -0
  67. data/lib/types/types/proc.rb +51 -0
  68. data/lib/types/types/self_type.rb +35 -0
  69. data/lib/types/types/simple.rb +33 -0
  70. data/lib/types/types/t_enum.rb +38 -0
  71. data/lib/types/types/type_member.rb +7 -0
  72. data/lib/types/types/type_parameter.rb +23 -0
  73. data/lib/types/types/type_template.rb +7 -0
  74. data/lib/types/types/type_variable.rb +31 -0
  75. data/lib/types/types/typed_array.rb +34 -0
  76. data/lib/types/types/typed_enumerable.rb +161 -0
  77. data/lib/types/types/typed_enumerator.rb +36 -0
  78. data/lib/types/types/typed_hash.rb +43 -0
  79. data/lib/types/types/typed_range.rb +26 -0
  80. data/lib/types/types/typed_set.rb +36 -0
  81. data/lib/types/types/union.rb +56 -0
  82. data/lib/types/types/untyped.rb +29 -0
  83. data/lib/types/utils.rb +217 -0
  84. metadata +223 -0
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+ # typed: true
3
+
4
+ module T::Private
5
+ module MixesInClassMethods
6
+ def included(other)
7
+ mod = Abstract::Data.get(self, :class_methods_mixin)
8
+ other.extend(mod)
9
+ super
10
+ end
11
+ end
12
+
13
+ module Mixins
14
+ def self.declare_mixes_in_class_methods(mixin, class_methods)
15
+ if mixin.is_a?(Class)
16
+ raise "Classes cannot be used as mixins, and so mixes_in_class_methods cannot be used on a Class."
17
+ end
18
+
19
+ if Abstract::Data.key?(mixin, :class_methods_mixin)
20
+ raise "mixes_in_class_methods can only be used once per module."
21
+ end
22
+
23
+ mixin.singleton_class.include(MixesInClassMethods)
24
+ Abstract::Data.set(mixin, :class_methods_mixin, class_methods)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+ # typed: true
3
+
4
+ module T::Private::Retry
5
+
6
+ # A special singleton used for static analysis of exceptions.
7
+ module RETRY
8
+ freeze
9
+ end
10
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+ # typed: true
3
+
4
+ # Used in `sig.checked(level)` to determine when runtime type checking
5
+ # is enabled on a method.
6
+ module T::Private::RuntimeLevels
7
+ LEVELS = [
8
+ # Validate every call in every environment
9
+ :always,
10
+ # Validate in tests, but not in production
11
+ :tests,
12
+ # Don't even validate in tests, b/c too expensive,
13
+ # or b/c we fully trust the static typing
14
+ :never,
15
+ ].freeze
16
+
17
+ @check_tests = false
18
+ @wrapped_tests_with_validation = false
19
+
20
+ @has_read_default_checked_level = false
21
+ @default_checked_level = :always
22
+
23
+ def self.check_tests?
24
+ # Assume that this code path means that some `sig.checked(:tests)`
25
+ # has been wrapped (or not wrapped) already, which is a trapdoor
26
+ # for toggling `@check_tests`.
27
+ @wrapped_tests_with_validation = true
28
+
29
+ @check_tests
30
+ end
31
+
32
+ def self.enable_checking_in_tests
33
+ if !@check_tests && @wrapped_tests_with_validation
34
+ raise "Toggle `:tests`-level runtime type checking earlier. " \
35
+ "There are already some methods wrapped with `sig.checked(:tests)`." \
36
+ end
37
+
38
+ _toggle_checking_tests(true)
39
+ end
40
+
41
+ def self.default_checked_level
42
+ @has_read_default_checked_level = true
43
+ @default_checked_level
44
+ end
45
+
46
+ def self.default_checked_level=(default_checked_level)
47
+ if @has_read_default_checked_level
48
+ raise "Set the default checked level earlier. There are already some methods whose sig blocks have evaluated which would not be affected by the new default."
49
+ end
50
+ @default_checked_level = default_checked_level
51
+ end
52
+
53
+ def self._toggle_checking_tests(checked)
54
+ @check_tests = checked
55
+ end
56
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+ # typed: false
3
+
4
+ module T::Private::Sealed
5
+ module NoInherit
6
+ def inherited(other)
7
+ super
8
+ this_line = Kernel.caller.find {|line| !line.match(/in `inherited'$/)}
9
+ T::Private::Sealed.validate_inheritance(this_line, self, 'inherited')
10
+ end
11
+ end
12
+
13
+ module NoIncludeExtend
14
+ def included(other)
15
+ super
16
+ this_line = Kernel.caller.find {|line| !line.match(/in `included'$/)}
17
+ T::Private::Sealed.validate_inheritance(this_line, self, 'included')
18
+ end
19
+
20
+ def extended(other)
21
+ super
22
+ this_line = Kernel.caller.find {|line| !line.match(/in `extended'$/)}
23
+ T::Private::Sealed.validate_inheritance(this_line, self, 'extended')
24
+ end
25
+ end
26
+
27
+ def self.declare(mod, decl_file)
28
+ if !mod.is_a?(Module)
29
+ raise "#{mod} is not a class or module and cannot be declared `sealed!`"
30
+ end
31
+ if sealed_module?(mod)
32
+ raise "#{mod} was already declared `sealed!` and cannot be re-declared `sealed!`"
33
+ end
34
+ if T::Private::Final.final_module?(mod)
35
+ raise "#{mod} was already declared `final!` and cannot be declared `sealed!`"
36
+ end
37
+ mod.extend(mod.is_a?(Class) ? NoInherit : NoIncludeExtend)
38
+ if !decl_file
39
+ raise "Couldn't determine declaration file for sealed class."
40
+ end
41
+ mod.instance_variable_set(:@sorbet_sealed_module_decl_file, decl_file)
42
+ end
43
+
44
+ def self.sealed_module?(mod)
45
+ mod.instance_variable_defined?(:@sorbet_sealed_module_decl_file)
46
+ end
47
+
48
+ def self.validate_inheritance(this_line, parent, verb)
49
+ this_file = this_line&.split(':').first
50
+ decl_file = parent.instance_variable_get(:@sorbet_sealed_module_decl_file)
51
+
52
+ if !this_file || !decl_file
53
+ raise "Couldn't determine enough file information for checking sealed modules"
54
+ end
55
+
56
+ if !this_file.start_with?(decl_file)
57
+ whitelist = T::Configuration.sealed_violation_whitelist
58
+ if whitelist != nil && whitelist.any? {|pattern| this_file =~ pattern}
59
+ return
60
+ end
61
+
62
+ raise "#{parent} was declared sealed and can only be #{verb} in #{decl_file}, not #{this_file}"
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+ # typed: true
3
+
4
+ # A placeholder for when an untyped thing must provide a type.
5
+ # Raises an exception if it is ever used for validation.
6
+ class T::Private::Types::NotTyped < T::Types::Base
7
+ ERROR_MESSAGE = "Validation is being done on a `NotTyped`. Please report to #dev-productivity."
8
+
9
+ # @override Base
10
+ def name
11
+ "<NOT-TYPED>"
12
+ end
13
+
14
+ # @override Base
15
+ def valid?(obj)
16
+ raise ERROR_MESSAGE
17
+ end
18
+
19
+ # @override Base
20
+ private def subtype_of_single?(other)
21
+ raise ERROR_MESSAGE
22
+ end
23
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+ # typed: true
3
+
4
+ # Holds a string. Useful for showing type aliases in error messages
5
+ class T::Private::Types::StringHolder < T::Types::Base
6
+ attr_reader :string
7
+
8
+ def initialize(string)
9
+ @string = string
10
+ end
11
+
12
+ # @override Base
13
+ def name
14
+ string
15
+ end
16
+
17
+ # @override Base
18
+ def valid?(obj)
19
+ false
20
+ end
21
+
22
+ # @override Base
23
+ private def subtype_of_single?(other)
24
+ false
25
+ end
26
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+ # typed: true
3
+
4
+ module T::Private::Types
5
+ # Wraps a proc for a type alias to defer its evaluation.
6
+ class TypeAlias < T::Types::Base
7
+
8
+ def initialize(callable)
9
+ @callable = callable
10
+ end
11
+
12
+ def aliased_type
13
+ @aliased_type ||= T::Utils.coerce(@callable.call)
14
+ end
15
+
16
+ # @override Base
17
+ def name
18
+ aliased_type.name
19
+ end
20
+
21
+ # @override Base
22
+ def valid?(obj)
23
+ aliased_type.valid?(obj)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+ # typed: true
3
+
4
+ # A marking class for when methods return void.
5
+ # Should never appear in types directly.
6
+ class T::Private::Types::Void < T::Types::Base
7
+ ERROR_MESSAGE = "Validation is being done on an `Void`. Please report to #dev-productivity."
8
+
9
+ # The actual return value of `.void` methods.
10
+ #
11
+ # Uses `module VOID` because this gives it a readable name when someone
12
+ # examines it in Pry or with `#inspect` like:
13
+ #
14
+ # T::Private::Types::Void::VOID
15
+ #
16
+ module VOID
17
+ freeze
18
+ end
19
+
20
+ # @override Base
21
+ def name
22
+ "<VOID>"
23
+ end
24
+
25
+ # @override Base
26
+ def valid?(obj)
27
+ raise ERROR_MESSAGE
28
+ end
29
+
30
+ # @override Base
31
+ private def subtype_of_single?(other)
32
+ raise ERROR_MESSAGE
33
+ end
34
+ end
@@ -0,0 +1,31 @@
1
+ # typed: true
2
+ # frozen_string_literal: true
3
+
4
+ module T::Profile
5
+ SAMPLE_RATE = 101 # 1 out of that many typechecks will be measured
6
+ class <<self
7
+ attr_accessor :typecheck_duration
8
+ attr_accessor :typecheck_samples
9
+ attr_accessor :typecheck_sample_attempts
10
+ def typecheck_duration_estimate
11
+ total_typechecks = typecheck_samples * SAMPLE_RATE + (SAMPLE_RATE - typecheck_sample_attempts)
12
+ typechecks_measured = typecheck_samples * SAMPLE_RATE
13
+ if typechecks_measured.positive?
14
+ typecheck_duration * SAMPLE_RATE * 1.0 * total_typechecks / typechecks_measured
15
+ else
16
+ 0.0
17
+ end
18
+ end
19
+
20
+ def typecheck_count_estimate
21
+ typecheck_samples * SAMPLE_RATE
22
+ end
23
+
24
+ def reset
25
+ @typecheck_duration = 0
26
+ @typecheck_samples = 0
27
+ @typecheck_sample_attempts = SAMPLE_RATE
28
+ end
29
+ end
30
+ self.reset
31
+ end
@@ -0,0 +1,161 @@
1
+ # frozen_string_literal: true
2
+ # typed: true
3
+
4
+ # A mixin for defining typed properties (attributes).
5
+ # To get serialization methods (to/from JSON-style hashes), add T::Props::Serializable.
6
+ # To get a constructor based on these properties, inherit from T::Struct.
7
+ module T::Props
8
+ extend T::Helpers
9
+
10
+ #####
11
+ # CAUTION: This mixin is used in hundreds of classes; we want to keep its surface area as narrow
12
+ # as possible and avoid polluting (and possibly conflicting with) the classes that use it.
13
+ #
14
+ # It currently has *zero* instance methods; let's try to keep it that way.
15
+ # For ClassMethods (below), try to add things to T::Props::Decorator instead unless you are sure
16
+ # it needs to be exposed here.
17
+ #####
18
+
19
+ module ClassMethods
20
+ extend T::Sig
21
+ extend T::Helpers
22
+
23
+ def props; decorator.props; end
24
+ def plugins; @plugins ||= []; end
25
+
26
+ def decorator_class; Decorator; end
27
+
28
+ def decorator; @decorator ||= decorator_class.new(self); end
29
+ def reload_decorator!; @decorator = decorator_class.new(self); end
30
+
31
+ # @!method self.prop(name, type, opts={})
32
+ #
33
+ # Define a new property. See {file:README.md} for some concrete
34
+ # examples.
35
+ #
36
+ # Defining a property defines a method with the same name as the
37
+ # property, that returns the current value, and a `prop=` method
38
+ # to set its value. Properties will be inherited by subclasses of
39
+ # a document class.
40
+ #
41
+ # @param name [Symbol] The name of this property
42
+ # @param cls [Class,T::Types::Base] The type of this
43
+ # property. If the type is itself a {Document} subclass, this
44
+ # property will be recursively serialized/deserialized.
45
+ # @param rules [Hash] Options to control this property's behavior.
46
+ # @option rules [T::Boolean,Symbol] :optional If `true`, this property
47
+ # is never required to be set before an instance is serialized.
48
+ # If `:on_load` (default), when this property is missing or nil, a
49
+ # new model cannot be saved, and an existing model can only be
50
+ # saved if the property was already missing when it was loaded.
51
+ # If `false`, when the property is missing/nil after deserialization, it
52
+ # will be set to the default value (as defined by the `default` or
53
+ # `factory` option) or will raise if they are not present.
54
+ # Deprecated: For {Model}s, if `:optional` is set to the special value
55
+ # `:existing`, the property can be saved as nil even if it was
56
+ # deserialized with a non-nil value. (Deprecated because there should
57
+ # never be a need for this behavior; the new behavior of non-optional
58
+ # properties should be sufficient.)
59
+ # @option rules [Array] :enum An array of legal values; The
60
+ # property is required to take on one of those values.
61
+ # @option rules [T::Boolean] :dont_store If true, this property will
62
+ # not be saved on the hash resulting from {#serialize}
63
+ # @option rules [Object] :ifunset A value to be returned if this
64
+ # property is requested but has never been set (is set to
65
+ # `nil`). It is applied at property-access time, and never saved
66
+ # back onto the object or into the database.
67
+ #
68
+ # ``:ifunset`` is considered **DEPRECATED** and should not be used
69
+ # in new code, in favor of just setting a default value.
70
+ # @option rules [Model, Symbol, Proc] :foreign A model class that this
71
+ # property is a reference to. Passing `:foreign` will define a
72
+ # `:"#{name}_"` method, that will load and return the
73
+ # corresponding foreign model.
74
+ #
75
+ # A symbol can be passed to avoid load-order dependencies; It
76
+ # will be lazily resolved relative to the enclosing module of the
77
+ # defining class.
78
+ #
79
+ # A callable (proc or method) can be passed to dynamically specify the
80
+ # foreign model. This will be passed the object instance so that other
81
+ # properties of the object can be used to determine the relevant model
82
+ # class. It should return a string/symbol class name or the foreign model
83
+ # class directly.
84
+ #
85
+ # @option rules [Object] :default A default value that will be set
86
+ # by {#initialize} if none is provided in the initialization
87
+ # hash. This will not affect objects loaded by {.from_hash}.
88
+ # @option rules [Proc] :factory A `Proc` that will be called to
89
+ # generate an initial value for this prop on {#initialize}, if
90
+ # none is provided.
91
+ # @option rules [T::Boolean] :immutable If true, this prop cannot be
92
+ # modified after an instance is created or loaded from a hash.
93
+ # @option rules [T::Boolean] :override It is an error to redeclare a
94
+ # `prop` that has already been declared (including on a
95
+ # superclass), unless `:override` is set to `true`.
96
+ # @option rules [Symbol, Array] :redaction A redaction directive that may
97
+ # be passed to Chalk::Tools::RedactionUtils.redact_with_directive to
98
+ # sanitize this parameter for display. Will define a
99
+ # `:"#{name}_redacted"` method, which will return the value in sanitized
100
+ # form.
101
+ #
102
+ # @return [void]
103
+ def prop(name, cls, rules={})
104
+ cls = T::Utils.coerce(cls) if !cls.is_a?(Module)
105
+ decorator.prop_defined(name, cls, rules)
106
+ end
107
+
108
+ # @!method validate_prop_value(propname, value)
109
+ #
110
+ # Validates the value of the specified prop. This method allows the caller to
111
+ # validate a value for a prop without having to set the data on the instance.
112
+ # Throws if invalid.
113
+ #
114
+ # @param prop [Symbol]
115
+ # @param val [Object]
116
+ # @return [void]
117
+ def validate_prop_value(prop, val)
118
+ decorator.validate_prop_value(prop, val)
119
+ end
120
+
121
+ # Needs to be documented
122
+ def plugin(mod)
123
+ decorator.plugin(mod)
124
+ end
125
+
126
+ # Shorthand helper to define a `prop` with `immutable => true`
127
+ sig {params(name: T.any(Symbol, String), cls_or_args: T.untyped, args: T::Hash[Symbol, T.untyped]).void}
128
+ def const(name, cls_or_args, args={})
129
+ if (cls_or_args.is_a?(Hash) && cls_or_args.key?(:immutable)) || args.key?(:immutable)
130
+ Kernel.raise ArgumentError.new("Cannot pass 'immutable' argument when using 'const' keyword to define a prop")
131
+ end
132
+
133
+ if cls_or_args.is_a?(Hash)
134
+ self.prop(name, cls_or_args.merge(immutable: true))
135
+ else
136
+ self.prop(name, cls_or_args, args.merge(immutable: true))
137
+ end
138
+ end
139
+
140
+ def included(child)
141
+ decorator.model_inherited(child)
142
+ super
143
+ end
144
+
145
+ def prepended(child)
146
+ decorator.model_inherited(child)
147
+ super
148
+ end
149
+
150
+ def extended(child)
151
+ decorator.model_inherited(child.singleton_class)
152
+ super
153
+ end
154
+
155
+ def inherited(child)
156
+ decorator.model_inherited(child)
157
+ super
158
+ end
159
+ end
160
+ mixes_in_class_methods(ClassMethods)
161
+ end