Skip to content
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

Only pass-through kwargs if super class can take them #81

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/dry/auto_inject/strategies/kwargs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def define_initialize_with_splat(super_parameters)
else
super_kwargs = slice_kwargs.(kwargs, super_parameters)

if super_kwargs.any?
if super_kwargs.any? && super_parameters.keyword_names.any?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it allocate a new array every time when asking for keyword_names? If yes, then it would be good to memoize it

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I read it, keyword_names are already memoized.

super(*args, **super_kwargs, &block)
else
super(*args, &block)
Expand Down
33 changes: 33 additions & 0 deletions spec/dry/auto_inject_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,39 @@ def initialize(other)
expect(instance.three).to eq 3
end
end

context "autoinject in class and parent with regular argument in super class" do
let(:super_klass) do
Class.new do
attr_reader :other

def initialize(other)
@other = other
end
end
end

let(:parent_klass) do
Class.new(super_klass) do
include Test::AutoInject.kwargs[:one, :two]
end
end

let(:child_class) do
Class.new(parent_klass) do
include Test::AutoInject.kwargs["namespace.three"]
end
end

it "works" do
instance = child_class.new(:other)

expect(instance.other).to eq :other
expect(instance.one).to eq 1
expect(instance.two).to eq 2
expect(instance.three).to eq 3
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the expected behavior when you actually pass in kwargs to the constructor?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean what is the expected behaviour if the super_klass in this test accepts kwargs? I suspect that is probably still broken as it was before this change. It would be nice to get this working with kwargs too but I can't think of a good way to achieve that without checking every single argument the constructor accepts and attempting to match each one before calling super.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should raise an error when you include auto-injector module and the super constructor is not compatible. WDYT?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. Loud and noisy is good.

end
end
end
end