Skip to content
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
5 changes: 4 additions & 1 deletion kink/inject.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@ def _resolve_kwargs(args, kwargs) -> dict:
if len(passed_kwargs) == len(parameters_name):
return passed_kwargs

resolved_kwargs = _resolve_function_kwargs(binding, parameters_name, parameters, container)
# do not resolve for passed kwargs and args
parameters_name_to_resolve = tuple(parameters_name - passed_kwargs.keys())

resolved_kwargs = _resolve_function_kwargs(binding, parameters_name_to_resolve, parameters, container)

all_kwargs = {**resolved_kwargs, **passed_kwargs}

Expand Down
37 changes: 37 additions & 0 deletions tests/test_issue_passed_parameters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from unittest.mock import MagicMock

from kink import di, inject

def test_do_not_resolve_passed_kwargs() -> None:
di["name"] = "Bob"
@inject
class ExpensiveObject:
def __init__(self, name: str):
self.name = name
raise Exception("Constructing expensive object")

@inject
def greet(namer: ExpensiveObject, greeting: str = "Hello %s") -> str:
Copy link
Contributor

Choose a reason for hiding this comment

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

@aikidojohn, the fact that this works in such scenarios is a side effect I allowed in the initial design, but it was not my intention for the library to be used in this way. I find it concerning when functions are called without parameters, despite their signature indicating otherwise.

return greeting % namer.name

mock_expensive_object = MagicMock(spec=ExpensiveObject)
mock_expensive_object.name = "Bill"

assert greet(namer=mock_expensive_object) == "Hello Bill"

def test_do_not_resolve_passed_args() -> None:
di["name"] = "Bob"
@inject
class ExpensiveObject:
def __init__(self, name: str):
self.name = name
raise Exception("Constructing expensive object")

@inject
def greet(namer: ExpensiveObject, greeting: str = "Hello %s") -> str:
return greeting % namer.name

mock_expensive_object = MagicMock(spec=ExpensiveObject)
mock_expensive_object.name = "Bill"

assert greet(mock_expensive_object) == "Hello Bill"