thoughtbot / thoughtbot/factory_bot
Fix shape variations with attribute assigner
Nobody has claimed this yet.
- Dominant language
- Ruby
- Stars
- 8.2k
- Forks
- 2.6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
👋 Hello, I'm seeing the following warnings in my test suite when using Factory Bot:
/factory_bot-6.4.6/lib/factory_bot/attribute_assigner.rb:16: warning: The class User reached 8 shape variations, instance variables accesses will be slower and memory usage increased.
It is recommended to define instance variables in a consistent order, for instance by eagerly defining them all in the #initialize method.
Reproduction Steps
You should be able to reproduce this locally by using export RUBYOPT="-W:deprecated -W:performance" when running the entire Factory Bot test suite.
Expected behavior
I would expect not to see performance warnings when using Factory Bot.
Actual behavior
Here's what I'm seeing:
/factory_bot-6.4.6/lib/factory_bot/attribute_assigner.rb:16: warning: The class User reached 8 shape variations, instance variables accesses will be slower and memory usage increased.
It is recommended to define instance variables in a consistent order, for instance by eagerly defining them all in the #initialize method.
Suggestion
A quick fix would be to ensure all of your lazy initialized instance variables are defined as nil in your constructor. Example:
def initialize
# Original impelmentation...
@method_tracking_evaluator = nil
...etc...
end
System configuration
factory_bot version: 6.4.6
rails version: 7.1.3.4
ruby version: 3.3.1
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in lib/factory_bot/attribute_assigner.rb at line 16 and reproduce the warning by running the full Factory Bot test suite with RUBYOPT="-W:deprecated -W:performance". The work is done when Factory Bot no longer emits the reported shape-variation warning under that configuration.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- ruby
- Domain
- testing
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 68/100