[ruby-core:99740] [Ruby master Feature#17122] Add category to Warning#warn
From:
akr@...
Date:
2020-08-28 02:54:49 UTC
List:
ruby-core #99740
Issue #17122 has been updated by akr (Akira Tanaka).
I feel the original proposal, adding a category keyword argument, is natural.
Although it is incompatible, arity check (or parameter check),
checking the arity of Warning.wan and don't give actual arguments more than the arity,
can preserve compatibility.
This fact raises a question.
Ruby has optional argument that
formal argument which corresponding actual argument may not be exist.
But Ruby don't have "ignorable" argument that
actual argument which corresponding formal argument may not be exist.
Ignorable arguments is useful for compatibility of extension of callbacks.
The arity check is a hack to implement it.
I feel arity check is not so problematic here.
It just complement a feature Ruby lacks.
----------------------------------------
Feature #17122: Add category to Warning#warn
https://bugs.ruby-lang.org/issues/17122#change-87240
* Author: eileencodes (Eileen Uchitelle)
* Status: Open
* Priority: Normal
----------------------------------------
Deprecation warnings and other warnings in Ruby have a category (:deprecated, etc) but those categories aren't exposed or accessible. In the most recent Ruby 2.7 upgrade at GitHub we monkey patched `Warning#warn` to be able to turn warnings into exceptions. However, there was no way to tell which warnings were deprecations and which were other types of warnings.
I want to expose the `category` on the `Warning` module so that I'm able to monkey patch `Warning#warn` and treat deprecation warnings differently from other warnings without using a regex the strings.
Here's an example program demonstrating what I'd like to get from Ruby by implementing this feature:
```ruby
module Warning
def self.warn(msg, category: nil)
if category == :deprecated
raise msg
else
super
end
end
end
def ivar
Object.new.instance_variable_get(:@ivar)
end
# Doesn't raise, but warns with verbose set
ivar
# Raises an error
Object.new.tainted?
```
The PR I worked on with @tenderlove is here: https://github.com/ruby/ruby/pull/3418
It moves the `Warning` module to be written in Ruby, updates `rb_warning_s_warn` to pass kwargs, and adds a `category` to `Warning#warn`.
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>