Module: T::Private::RuntimeLevels

Defined in:
lib/types/private/runtime_levels.rb

Overview

Used in ‘sig.checked(level)` to determine when runtime type checking is enabled on a method.

Constant Summary collapse

LEVELS =
[
  # Validate every call in every environment
  :always,
  # Validate in tests, but not in production
  :tests,
  # Don't even validate in tests, b/c too expensive,
  # or b/c we fully trust the static typing
  :never,
  # Validate the sig when the file is using the Sorbet Compiler.
  # Behaves like :never when interpreted.
  :compiled,
].freeze

Class Method Summary collapse

Class Method Details

._toggle_checking_tests(checked) ⇒ Object



63
64
65
# File 'lib/types/private/runtime_levels.rb', line 63

def self._toggle_checking_tests(checked)
  @check_tests = checked
end

.check_tests?Boolean

Returns:



26
27
28
29
30
31
32
33
# File 'lib/types/private/runtime_levels.rb', line 26

def self.check_tests?
  # Assume that this code path means that some `sig.checked(:tests)`
  # has been wrapped (or not wrapped) already, which is a trapdoor
  # for toggling `@check_tests`.
  @wrapped_tests_with_validation = true

  @check_tests
end

.default_checked_levelObject



47
48
49
50
# File 'lib/types/private/runtime_levels.rb', line 47

def self.default_checked_level
  @has_read_default_checked_level = true
  @default_checked_level
end

.default_checked_level=(default_checked_level) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/types/private/runtime_levels.rb', line 52

def self.default_checked_level=(default_checked_level)
  if @has_read_default_checked_level
    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."
  end
  if !LEVELS.include?(default_checked_level)
    raise "Invalid `checked` level '#{default_checked_level}'. Use one of: #{LEVELS}."
  end

  @default_checked_level = default_checked_level
end

.enable_checking_in_testsObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/types/private/runtime_levels.rb', line 35

def self.enable_checking_in_tests
  if !@check_tests && @wrapped_tests_with_validation
    all_checked_tests_sigs = T::Private::Methods.all_checked_tests_sigs
    locations = all_checked_tests_sigs.map {|sig| sig.method.source_location.join(':')}.join("\n- ")
    raise "Toggle `:tests`-level runtime type checking earlier. " \
      "There are already some methods wrapped with `sig.checked(:tests)`:\n" \
      "- #{locations}"
  end

  _toggle_checking_tests(true)
end