-
Notifications
You must be signed in to change notification settings - Fork 960
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Please don't implement .nil?
#568
Comments
This was carried over by mself when I refactored the Your points are valid, but we should bump a significant version number( 15->16 here I think) since this is going to break lots of folks code. |
I think I'm in favor of this. It very likely could break a lot of people's use but feels like the right thing. I believe I added nil? (if it was me) way back in the day when things were a bit more wild west. |
@jnunemaker In favor as well, but I think we should add a deprecation warning first. |
Resolves: > [DEPRECATION] HTTParty will no longer override `response#nil?`. > This functionality will be removed in future versions. Please, add explicit > check `response.body.nil? || response.body.empty?`. For more info refer > to: jnunemaker/httparty#568 > /lib/learn_to_rank/ranker.rb:63:in `ranker_error'
This resolves a deprecation warning from the HTTParty gem. > HTTParty will no longer override `response#nil?`. This functionality will be > removed in future versions. Please, add explicit check `response.body.nil? || response.body.empty?`. > For more info refer to: jnunemaker/httparty#568.
Resolves: > [DEPRECATION] HTTParty will no longer override `response#nil?`. > This functionality will be removed in future versions. Please, add explicit > check `response.body.nil? || response.body.empty?`. For more info refer > to: jnunemaker/httparty#568 > /lib/learn_to_rank/ranker.rb:63:in `ranker_error'
We'll now check separately for response and the response body being nil or empty as per the deprecated source in https://github.com/jnunemaker/httparty/blob/689e64bb2db0ad454b89519ecd5e2fcfee74c90c/lib/httparty/response.rb#L83 Resolves: > [DEPRECATION] HTTParty will no longer override `response#nil?`. > This functionality will be removed in future versions. Please, add explicit > check `response.body.nil? || response.body.empty?`. For more info refer > to: jnunemaker/httparty#568 > /lib/learn_to_rank/ranker.rb:63:in `ranker_error'
[DEPRECATION] HTTParty will no longer override `response#nil?`. This functionality will be removed in future versions. Please, add explicit check `response.body.nil? || response.body.empty?`. For more info refer to: jnunemaker/httparty#568 Solution from: forem/forem#5297
…ide `response#nil?`. This functionality will be removed in future versions. Please, add explicit check `response.body.nil? || response.body.empty?`. For more info refer to: jnunemaker/httparty#568
…ide `response#nil?`. This functionality will be removed in future versions. Please, add explicit check `response.body.nil? || response.body.empty?`. For more info refer to: jnunemaker/httparty#568
HTTParty now throws a warning when you try to call `HTTParty::Response#nil?`: ``` [DEPRECATION] HTTParty will no longer override `response#nil?`. This functionality will be removed in future versions. Please, add explicit check `response.body.nil? || response.body.empty?`. For more info refer to: jnunemaker/httparty#568 ``` To resolve this, `require_attr` has been altered to now perform a `body.nil?` check when the attribute being checked for nil is an `HTTParty::Response`. This issue was somewhat difficult to reproduce in your test suite due to the extensive amount of mocking going on, as well as the fact that `Gemfile.lock` was committed. This warning wasn't occurring in tests, so I dove in a bit and learned that none of the tests actually pass a real `HTTParty::Response` into `require_attr`, so you'd never see this warning in tests but it definitely happens in the real world. Even if you had been, the fact that `Gemfile.lock` was committed prevented HTTParty from being updated locally, so the tests were not actually running against the version of HTTParty that applications consuming this library have installed. To resolve this, I deleted `Gemfile.lock` from the repo and added it to `.gitignore`. This file is not used when you publish to RubyGems.org, as RubyGems only looks at the `.gemspec` file to figure out dependencies. This is also the case for consuming applications. It's a general good practice to not commit `Gemfile.lock` in RubyGem libraries so that you're always running against the gems that an application would end up installing as dependencies of your gem.
A note for future troubleshooters: In my case it wasn't a call to Old code response = HTTParty.get('https://www.google.com')
page = Nokogiri::HTML(response) # this used to work Fix page = Nokogiri::HTML(response.body) # update to this |
We recently upgraded httparty in our project and now our test output is littered with deprecation warnings like this: ``` [DEPRECATION] HTTParty will no longer override `response#nil?`. This functionality will be removed in future versions. Please, add explicit check `response.body.nil? || response.body.empty?`. For more info refer to: jnunemaker/httparty#568 /Users/magnusvk/.gem/ruby/2.7.2/gems/neverbounce-api-1.2.0/lib/never_bounce/api/feature/require_attr.rb:22:in `block in require_attr' ``` It took me some time to figure out what's going on, but the issue is that `server_ok` in `NeverBounce::API::Session` calls `require_attr` on the HTTParty response and then `require_attr` calls `.nil?` on that HTTParty response object. This actually all seems fine and I don't think the proposed change in HTTParty will break things here as the intended purpose of `require_attr` really just seems to be to check that the response is not `nil`. This change here is equivalent to the future behavior after HTTParty makes its change, and it side-steps the deprecation warning.
Another note for future troubleshooters: logging the response object will trigger the depreciation warning as well:
|
In my experience, when a
The warning's advice is dangerously misleading. Following the advice would cause a |
For anyone looking to suppress these warnings where they can't do anything about them (in my case, in Warning.ignore(
/#{Regexp.quote("HTTParty will no longer override `response#nil?`.")}.+#{Object.const_source_location('ActiveSupport::Cache')[0]}/m,
) |
I would love to see this warning stripped away. Many things, such as loggers and application debuggers, call I.E. it's a warning pretty much completely unavoidable in a lot of circumstances, even if we're not checking or caring about the response body itself. For those looking to suppress it: HTTParty::Response.class_eval do
def warn_about_nil_deprecation
end
end |
Just curious when in practice the |
It's been a minute since I worked at the company that originally used this code, and which caused me to file this, but I can envision a few ways. For instance, if the response is fetched in a
In this case that throws a I am sure there are other ways it can happen. |
As I mentioned above, a |
Could we get a Adding a |
Get rid off the DEPRECATED warning in related to that functionality.. ==> graylog: [DEPRECATION] HTTParty will no longer override `response#nil?`. This functionality will be removed in future versions. Please, add explicit check `response.body.nil? || response.body.empty?`. For more info refer to: jnunemaker/httparty#568 ==> graylog: /tmp/vagrant-puppet/environments/local/local-modules/graylog_api/lib/puppet/provider/graylog_api.rb:109:in `request' ==> graylog: [DEPRECATION] HTTParty will no longer override `response#nil?`. This functionality will be removed in future versions. Please, add explicit check `response.body.nil? || response.body.empty?`. For more info refer to: jnunemaker/httparty#568
Ran into this today:
Had to do this because we were assuming that code like this would cover the
nil
case:Took me a bit of digging to find out that HTTParty::Response implements
nil?
:https://github.com/jnunemaker/httparty/blob/master/lib/httparty/response.rb#L58
I don't think this is a good idea. It is widely expected behavior in ruby that
nil?
isn't overwritten, sincenil
is a singleton, so these are almost universally treated as equivalent:This is important because if they're equivalent, you can use "truthiness" to check if the object itself is
nil
, as opposed to if the response's body isnil
, which is a different thing entirely. The definition ofnil?
above even includes an empty string in the response body, which is, again, not at all the same as when the response itself isnil
.This method really should be
.blank?
or some such.The text was updated successfully, but these errors were encountered: