Fix ruby 2.7 kwargs warning#1415
Conversation
6d851c5 to
fea439a
Compare
fea439a to
243e6c9
Compare
|
Thanks for opening this PR! Could explain a bit more about what code is causing the deprecation warnings? I ask because I am seeing more factory_bot-specific warnings on the 2.7 Travis builds with this change than I did before this change. My expectation would be that for each change made in this PR there would be at least one test that outputs a warning before the change, and no longer outputs the warning after this change. Here is what I am currently seeing:
(I searched the logs from each build for "/home/travis/build/thoughtbot/factory_bot/lib/") |
243e6c9 to
f68581a
Compare
Ruby 2.7 deprecated passing kwargs when the method expects a hash or passing a hash when the method expects kwargs. In factory_bot, this creates the warning: ``` /Users/hparker/code/factory_bot/lib/factory_bot/decorator/new_constructor.rb:9: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call /Users/hparker/code/factory_bot/spec/acceptance/initialize_with_spec.rb:220: warning: The called method `initialize' is defined here ``` We can fix this warning by updating the syntax. We need to include `**kwargs` in the `method_missing` calls when we are on ruby 2.7 or later. In decorator.rb, adding `**kwargs` alone doesn't work since adding `**kwargs` can change what arguments remain in the `args`. In this case we have to class eval the method if we are running ruby 2.7. This way the syntax is valid in previous versions and we can use the `...` operator which allows us to avoid changing the arguments passed on in method missing. Co-authored-by: Lee Quarella <leequarella@gmail.com>
f68581a to
e3ee350
Compare
|
Hey @composerinteralia thanks for the comments! I do see, on the 6.0 build in CI, but it does not appear locally for me. It looks like the warning is not happening in the rails master build which makes me think that it is rails 6.0's
|
|
Thanks again! I don't like this PR and I am grateful for you opening it, if that makes sense 😄. |
|
@composerinteralia agreed! I really wish there was another way to do this. Thanks again for factory_bot! ❤️ |
…guments, using the latest version of it `bundle update factory_bot` It seems that `initialize_with` of `factory_bot` raised `ArgumentError` related to keyword arguments of Ruby `3.0.0`. It seems that the latest version `factory_bot` `6.1.0` revised the error. CHANGELOG: - https://github.com/thoughtbot/factory_bot/blob/v6.1.0/NEWS.md Error was (Some excerpts): ``` Failures: 1) Kirico::Form when the form is for general use validations Failure/Error: def initialize(fd:, company_count: Kirico::CompanyCount.new, company:, records: []) @fd = fd @company_count = company_count @Company = company @records = records end ArgumentError: wrong number of arguments (given 1, expected 0; required keywords: fd, company) # ./lib/kirico/models/form.rb:21:in `initialize' # ./spec/factories/form.rb:6:in `block (3 levels) in <top (required)>' # ./spec/kirico/models/form_spec.rb:7:in `block (3 levels) in <top (required)>' # ./spec/kirico/models/form_spec.rb:14:in `block (4 levels) in <top (required)>' # ./spec/kirico/models/form_spec.rb:15:in `block (4 levels) in <top (required)>' ``` Refs. - thoughtbot/factory_bot@2eacfe8 - thoughtbot/factory_bot#1425 - thoughtbot/factory_bot#1423 - thoughtbot/factory_bot@5c071d4 - thoughtbot/factory_bot#1415
Ruby 2.7 deprecated passing kwargs when the method expects a hash or passing a hash when the method expects kwargs. In factory_bot, this creates the warning:
We can fix this warning by updating the syntax. We need to include
**kwargsin themethod_missingcalls when we are on ruby 2.7 or later.In decorator.rb, adding
**kwargsalone doesn't work since adding**kwargscan change what arguments remain in theargs.In this case we have to class eval the method if we are running ruby 2.7. This way the syntax is valid in previous versions and we can use the
...operator which allows us to avoid changing the arguments passed on in method missing.