Skip to content

Fix ruby 2.7 kwargs warning#1415

Merged
composerinteralia merged 1 commit intothoughtbot:masterfrom
HParker:fix-ruby-2.7-kwargs-warning
Jun 24, 2020
Merged

Fix ruby 2.7 kwargs warning#1415
composerinteralia merged 1 commit intothoughtbot:masterfrom
HParker:fix-ruby-2.7-kwargs-warning

Conversation

@HParker
Copy link
Contributor

@HParker HParker commented Jun 23, 2020

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.

@HParker HParker force-pushed the fix-ruby-2.7-kwargs-warning branch from 6d851c5 to fea439a Compare June 23, 2020 17:33
@HParker HParker changed the title Ruby 2.7 deprecated passing kwargs when the method expects a hash or … @HParker @leequarella Fix ruby 2.7 kwargs warning Jun 23, 2020
@HParker HParker changed the title @HParker @leequarella Fix ruby 2.7 kwargs warning Fix ruby 2.7 kwargs warning Jun 23, 2020
@HParker HParker force-pushed the fix-ruby-2.7-kwargs-warning branch from fea439a to 243e6c9 Compare June 23, 2020 18:01
@composerinteralia
Copy link
Collaborator

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/")

@HParker HParker force-pushed the fix-ruby-2.7-kwargs-warning branch from 243e6c9 to f68581a Compare June 24, 2020 16:10
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>
@HParker HParker force-pushed the fix-ruby-2.7-kwargs-warning branch from f68581a to e3ee350 Compare June 24, 2020 16:30
@HParker
Copy link
Contributor Author

HParker commented Jun 24, 2020

Hey @composerinteralia thanks for the comments!

I do see,

/home/travis/build/thoughtbot/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
/home/travis/build/thoughtbot/factory_bot/spec/acceptance/initialize_with_spec.rb:220: warning: The called method `initialize' is defined here

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 delegate method that is causing the warning in that build.

@composerinteralia composerinteralia merged commit 5c071d4 into thoughtbot:master Jun 24, 2020
@composerinteralia
Copy link
Collaborator

Thanks again! I don't like this PR and I am grateful for you opening it, if that makes sense 😄.

@HParker
Copy link
Contributor Author

HParker commented Jun 24, 2020

@composerinteralia agreed! I really wish there was another way to do this. Thanks again for factory_bot! ❤️

machupicchubeta added a commit to machupicchubeta/kirico that referenced this pull request Mar 6, 2021
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants