Skip to content
This repository was archived by the owner on Jul 5, 2023. It is now read-only.

Commit 85a538f

Browse files
author
Harry Doan
authored
[deprecation] add deprecation warning for fetch_typed, etc. (#318)
I think TypedParams provide a much neater API to do all conversion at once. It also helps converting boolean string or integer string to native ruby type. Plus, we found through profiling that BooleanString and IntegerString incurs some cost in type-checking String, even when they are not used directly. This deprecation will help us getting rid of them as well. Slated to remove them by v0.7, but issue a warning for now. Test plan: spec shows the warning now! Pull Request Branch: manhhung741/deprecation_add_deprecation_warning_for_f Pull Request Link: #318
1 parent c127909 commit 85a538f

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

lib/sorbet-rails/custom_types/boolean_string.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# typed: false
2+
require('sorbet-rails/deprecation.rb')
3+
24
module BooleanStringImpl
35
def is_a?(type)
46
return super unless type == BooleanString
@@ -17,6 +19,10 @@ def instance_of?(type)
1719

1820
def _is_a_boolean_string?
1921
return @cached_is_a unless @cached_is_a.nil?
22+
SorbetRails::TypeAssertDeprecation.deprecation_warning(
23+
:BooleanString,
24+
'Use TypedParam with T::Boolean type instead.'
25+
)
2026
@cached_is_a = (self =~ /^(true|false)$/i) == 0
2127
end
2228
end
@@ -27,6 +33,10 @@ class String
2733

2834
class BooleanString < String
2935
def self.===(other)
36+
SorbetRails::TypeAssertDeprecation.deprecation_warning(
37+
:BooleanString,
38+
'Use TypedParam with T::Boolean type instead.'
39+
)
3040
other.is_a?(BooleanString)
3141
end
3242
end

lib/sorbet-rails/custom_types/integer_string.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# typed: false
2+
require('sorbet-rails/deprecation.rb')
3+
24
module IntegerStringImpl
35
def is_a?(type)
46
return super unless type == IntegerString
@@ -17,6 +19,10 @@ def instance_of?(type)
1719

1820
def _is_a_integer_string?
1921
return @cached_is_a unless @cached_is_a.nil?
22+
SorbetRails::TypeAssertDeprecation.deprecation_warning(
23+
:IntegerString,
24+
'Use TypedParams with Integer type instead.'
25+
)
2026
Integer(self, 10)
2127
@cached_is_a = true
2228
rescue ArgumentError => err
@@ -30,6 +36,10 @@ class String
3036

3137
class IntegerString < String
3238
def self.===(other)
39+
SorbetRails::TypeAssertDeprecation.deprecation_warning(
40+
:IntegerString,
41+
'Use TypedParams with Integer type instead.'
42+
)
3343
other.is_a?(IntegerString)
3444
end
3545
end

lib/sorbet-rails/deprecation.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require 'active_support/deprecation'
2+
3+
module SorbetRails
4+
TypeAssertDeprecation = ActiveSupport::Deprecation.new('0.7', 'SorbetRails')
5+
end

lib/sorbet-rails/rails_mixins/custom_params_methods.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# typed: false
22
require 'sorbet-runtime'
3+
require('sorbet-rails/deprecation.rb')
34

45
module SorbetRails::CustomParamsMethods
56
include Kernel
@@ -16,6 +17,11 @@ module SorbetRails::CustomParamsMethods
1617
returns(T.type_parameter(:U))
1718
}
1819
def require_typed(key, ta)
20+
SorbetRails::TypeAssertDeprecation.deprecation_warning(
21+
:require_typed,
22+
'Use TypedParams with a T::Struct represents the type of each parameters.'
23+
)
24+
1925
val = require(key)
2026
ta.assert(val)
2127
rescue TypeError
@@ -35,6 +41,11 @@ def require_typed(key, ta)
3541
returns(T.type_parameter(:U))
3642
}
3743
def fetch_typed(key, ta, *args)
44+
SorbetRails::TypeAssertDeprecation.deprecation_warning(
45+
:fetch_typed,
46+
'Use TypedParams with a T::Struct represents the type of each parameters.'
47+
)
48+
3849
val = fetch(key, *args)
3950
ta.assert(val)
4051
rescue TypeError

0 commit comments

Comments
 (0)