Skip to content

Commit

Permalink
Remove deprecated support to call the following methods without passi…
Browse files Browse the repository at this point in the history
…ng a deprecator

  - `deprecate`
  - `deprecate_constant`
  - `ActiveSupport::Deprecation::DeprecatedObjectProxy.new`
  - `ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new`
  - `ActiveSupport::Deprecation::DeprecatedConstantProxy.new`
  - `assert_deprecated`
  - `assert_not_deprecated`
  - `collect_deprecations`
  • Loading branch information
rafaelfranca committed Apr 30, 2024
1 parent 157a6a5 commit c594b14
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 51 deletions.
13 changes: 13 additions & 0 deletions activesupport/CHANGELOG.md
@@ -1,3 +1,16 @@
* Remove deprecated support to call the following methods without passing a deprecator:

- `deprecate`
- `deprecate_constant`
- `ActiveSupport::Deprecation::DeprecatedObjectProxy.new`
- `ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new`
- `ActiveSupport::Deprecation::DeprecatedConstantProxy.new`
- `assert_deprecated`
- `assert_not_deprecated`
- `collect_deprecations`

*Rafael Mendonça França*

* Remove deprecated `ActiveSupport::Deprecation` delegation to instance.

*Rafael Mendonça França*
Expand Down
Expand Up @@ -14,15 +14,12 @@ class Module
# Kernel.warn message
# end
# end
def deprecate(*method_names, deprecator: nil, **options)
def deprecate(*method_names, deprecator:, **options)
if deprecator.is_a?(ActiveSupport::Deprecation)
deprecator.deprecate_methods(self, *method_names, **options)
elsif deprecator
# we just need any instance to call deprecate_methods, but the deprecation will be emitted by deprecator
ActiveSupport.deprecator.deprecate_methods(self, *method_names, **options, deprecator: deprecator)
else
ActiveSupport.deprecator.warn("Module.deprecate without a deprecator is deprecated")
ActiveSupport::Deprecation._instance.deprecate_methods(self, *method_names, **options)
end
end
end
Expand Up @@ -39,9 +39,7 @@ def const_missing(missing_const_name)
super
end

def deprecate_constant(const_name, new_constant, message: nil, deprecator: nil)
ActiveSupport.deprecator.warn("DeprecatedConstantAccessor.deprecate_constant without a deprecator is deprecated") unless deprecator
deprecator ||= ActiveSupport::Deprecation._instance
def deprecate_constant(const_name, new_constant, deprecator:, message: nil)
class_variable_set(:@@_deprecated_constants, {}) unless class_variable_defined?(:@@_deprecated_constants)
class_variable_get(:@@_deprecated_constants)[const_name.to_s] = { new: new_constant, message: message, deprecator: deprecator }
end
Expand Down
17 changes: 7 additions & 10 deletions activesupport/lib/active_support/deprecation/proxy_wrappers.rb
Expand Up @@ -3,7 +3,7 @@
module ActiveSupport
class Deprecation
class DeprecationProxy # :nodoc:
def self.new(*args, &block)
def self.new(*args, **kwargs, &block)
object = args.first

return object unless object
Expand Down Expand Up @@ -36,11 +36,10 @@ def method_missing(called, *args, &block)
# (Backtrace)
# # => "#<Object:0x007fb9b34c34b0>"
class DeprecatedObjectProxy < DeprecationProxy
def initialize(object, message, deprecator = nil)
def initialize(object, message, deprecator)
@object = object
@message = message
ActiveSupport.deprecator.warn("DeprecatedObjectProxy without a deprecator is deprecated") unless deprecator
@deprecator = deprecator || ActiveSupport::Deprecation._instance
@deprecator = deprecator
end

private
Expand Down Expand Up @@ -86,12 +85,11 @@ def warn(callstack, called, args)
# example.request.to_s
# # => "special_request"
class DeprecatedInstanceVariableProxy < DeprecationProxy
def initialize(instance, method, var = "@#{method}", deprecator = nil)
def initialize(instance, method, var = "@#{method}", deprecator:)
@instance = instance
@method = method
@var = var
ActiveSupport.deprecator.warn("DeprecatedInstanceVariableProxy without a deprecator is deprecated") unless deprecator
@deprecator = deprecator || ActiveSupport::Deprecation._instance
@deprecator = deprecator
end

private
Expand Down Expand Up @@ -127,13 +125,12 @@ def self.new(*args, **options, &block)
super
end

def initialize(old_const, new_const, deprecator = nil, message: "#{old_const} is deprecated! Use #{new_const} instead.")
def initialize(old_const, new_const, deprecator, message: "#{old_const} is deprecated! Use #{new_const} instead.")
Kernel.require "active_support/inflector/methods"

@old_const = old_const
@new_const = new_const
ActiveSupport.deprecator.warn("DeprecatedConstantProxy without a deprecator is deprecated") unless deprecator
@deprecator = deprecator || ActiveSupport::Deprecation._instance
@deprecator = deprecator
@message = message
end

Expand Down
17 changes: 5 additions & 12 deletions activesupport/lib/active_support/testing/deprecation.rb
Expand Up @@ -29,10 +29,11 @@ module Deprecation
# end
def assert_deprecated(match = nil, deprecator = nil, &block)
match, deprecator = nil, match if match.is_a?(ActiveSupport::Deprecation)

unless deprecator
ActiveSupport.deprecator.warn("assert_deprecated without a deprecator is deprecated")
deprecator = ActiveSupport::Deprecation._instance
raise ArgumentError, "No deprecator given"
end

result, warnings = collect_deprecations(deprecator, &block)
assert !warnings.empty?, "Expected a deprecation warning within the block but received none"
if match
Expand All @@ -51,11 +52,7 @@ def assert_deprecated(match = nil, deprecator = nil, &block)
# assert_not_deprecated(ActiveSupport::Deprecation.new) do
# CustomDeprecator.warn "message" # passes assertion, different deprecator
# end
def assert_not_deprecated(deprecator = nil, &block)
unless deprecator
ActiveSupport.deprecator.warn("assert_not_deprecated without a deprecator is deprecated")
deprecator = ActiveSupport::Deprecation._instance
end
def assert_not_deprecated(deprecator, &block)
result, deprecations = collect_deprecations(deprecator, &block)
assert deprecations.empty?, "Expected no deprecation warning within the block but received #{deprecations.size}: \n #{deprecations * "\n "}"
result
Expand All @@ -69,11 +66,7 @@ def assert_not_deprecated(deprecator = nil, &block)
# ActiveSupport::Deprecation.new.warn "other message"
# :result
# end # => [:result, ["message"]]
def collect_deprecations(deprecator = nil)
unless deprecator
ActiveSupport.deprecator.warn("collect_deprecations without a deprecator is deprecated")
deprecator = ActiveSupport::Deprecation._instance
end
def collect_deprecations(deprecator)
old_behavior = deprecator.behavior
deprecations = []
deprecator.behavior = Proc.new do |message, callstack|
Expand Down
40 changes: 18 additions & 22 deletions activesupport/test/deprecation_test.rb
Expand Up @@ -47,8 +47,8 @@ def setup
end
end

test "assert_deprecated is deprecated without a deprecator" do
assert_deprecated(ActiveSupport.deprecator) do
test "assert_deprecated requires a deprecator" do
assert_raises(ArgumentError) do
assert_deprecated do
ActiveSupport::Deprecation._instance.warn
end
Expand All @@ -61,8 +61,8 @@ def setup
end
end

test "assert_not_deprecated is deprecated without a deprecator" do
assert_deprecated(ActiveSupport.deprecator) do
test "assert_not_deprecated requires a deprecator" do
assert_raises(ArgumentError) do
assert_not_deprecated { }
end
end
Expand All @@ -77,8 +77,8 @@ def setup
assert_match "DEPRECATION WARNING:", result.last.sole
end

test "collect_deprecations is deprecated without a deprecator" do
assert_deprecated(ActiveSupport.deprecator) do
test "collect_deprecations requires a deprecator" do
assert_raises(ArgumentError) do
collect_deprecations { }
end
end
Expand Down Expand Up @@ -116,24 +116,20 @@ def setup
end
end

test "Module::deprecate without a deprecator is deprecated" do
test "Module::deprecate requires a deprecator" do
klass = Class.new(Deprecatee)
_, deprecations = collect_deprecations(ActiveSupport.deprecator) do
assert_raises(ArgumentError) do
klass.deprecate :zero
end
assert_match "Module.deprecate without a deprecator is deprecated", deprecations.sole
assert_deprecated(/zero is deprecated/, ActiveSupport::Deprecation._instance) do
klass.new.zero
end
end

test "DeprecatedObjectProxy" do
deprecated_object = ActiveSupport::Deprecation::DeprecatedObjectProxy.new(Object.new, ":bomb:", @deprecator)
assert_deprecated(/:bomb:/, @deprecator) { deprecated_object.to_s }
end

test "DeprecatedObjectProxy without a deprecator is deprecated" do
assert_deprecated(ActiveSupport.deprecator) do
test "DeprecatedObjectProxy requires a deprecator" do
assert_raises(ArgumentError) do
ActiveSupport::Deprecation::DeprecatedObjectProxy.new(Object.new, ":bomb:")
end
end
Expand Down Expand Up @@ -302,7 +298,7 @@ def setup

test "DeprecatedInstanceVariableProxy" do
instance = Deprecatee.new
instance.fubar = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(instance, :foo_bar, "@fubar", @deprecator)
instance.fubar = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(instance, :foo_bar, "@fubar", deprecator: @deprecator)
instance.foo_bar = "foo bar!"

fubar_size = assert_deprecated("@fubar.size", @deprecator) { instance.fubar.size }
Expand All @@ -314,15 +310,15 @@ def setup

test "DeprecatedInstanceVariableProxy does not warn on inspect" do
instance = Deprecatee.new
instance.fubar = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(instance, :foo_bar, "@fubar", @deprecator)
instance.fubar = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(instance, :foo_bar, "@fubar", deprecator: @deprecator)
instance.foo_bar = "foo bar!"

fubar_inspected = assert_not_deprecated(@deprecator) { instance.fubar.inspect }
assert_equal instance.foo_bar.inspect, fubar_inspected
end

test "DeprecatedInstanceVariableProxy without a deprecator is deprecated" do
assert_deprecated(ActiveSupport.deprecator) do
test "DeprecatedInstanceVariableProxy requires a deprecator" do
assert_raises(ArgumentError) do
ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(Deprecatee.new, :foobar, "@fubar")
end
end
Expand Down Expand Up @@ -354,8 +350,8 @@ def setup
end
end

test "DeprecatedConstantProxy without a deprecator is deprecated" do
assert_deprecated(ActiveSupport.deprecator) do
test "DeprecatedConstantProxy requires a deprecator" do
assert_raise(ArgumentError) do
ActiveSupport::Deprecation::DeprecatedConstantProxy.new("Fuu", "Undeprecated::Foo")
end
end
Expand Down Expand Up @@ -383,9 +379,9 @@ def setup
end
end

test "deprecate_constant is deprecated without a deprecator" do
test "deprecate_constant requires a deprecator" do
legacy = Module.new.include(ActiveSupport::Deprecation::DeprecatedConstantAccessor)
assert_deprecated("DeprecatedConstantAccessor.deprecate_constant without a deprecator is deprecated", ActiveSupport.deprecator) do
assert_raises(ArgumentError) do
legacy.deprecate_constant "OLD", "NEW"
end
end
Expand Down
11 changes: 11 additions & 0 deletions guides/source/7_0_release_notes.md
Expand Up @@ -269,6 +269,17 @@ Please refer to the [Changelog][active-support] for detailed changes.

### Removals

* Remove deprecated support to call the following methods without passing a deprecator:

- `deprecate`
- `deprecate_constant`
- `ActiveSupport::Deprecation::DeprecatedObjectProxy.new`
- `ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new`
- `ActiveSupport::Deprecation::DeprecatedConstantProxy.new`
- `assert_deprecated`
- `assert_not_deprecated`
- `collect_deprecations`

* Remove deprecated `config.active_support.use_sha1_digests`.

* Remove deprecated `URI.parser`.
Expand Down

0 comments on commit c594b14

Please sign in to comment.