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

Fix the issue where passing keyword argumentss was not effective #475

Open
wants to merge 4 commits 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/pluggy/_callers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from ._result import Result
from ._warnings import PluggyTeardownRaisedWarning


# Need to distinguish between old- and new-style hook wrappers.
# Wrapping with a tuple is the fastest type-safe way I found to do it.
Teardown = Union[
Expand Down Expand Up @@ -70,6 +69,11 @@ def _multicall(
for hook_impl in reversed(hook_impls):
try:
args = [caller_kwargs[argname] for argname in hook_impl.argnames]
kwargs = {
Copy link
Member

Choose a reason for hiding this comment

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

note that adding this bit undoes a specifically chosen optimization (that we might want to reevaluate later)

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for pointing this out. I understand what you mean. However, the current design has a major limitation that prevents the passing of keyword arguments.

Could you please elaborate on the specific optimization that is being undone by my addition? I would like to understand its purpose and how it was beneficial to the project.

Copy link
Member

Choose a reason for hiding this comment

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

initially pluggy used kwarg passing
i'm not quite sure how long ago, but eventually we determined that with extracting only positional arguments, things where much faster (i vaguely recall 2x improvement back in the time)

if we now add kwargs back, that effect is likely undone

as recent python versions have optimizations for calling, i believe a new measurement makes sense

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for sharing this information. Could you please elaborate on the performance evaluation method used? I would like to measure the performance based on the new version of Python to have a more accurate comparison. It would be helpful if you could provide more details on the testing environment .

Choose a reason for hiding this comment

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

Hi Ronny,

I also got the same requirement as well.

Any plan to run a new measurement? It is appreciated if anything I can help.

k: v
for k, v in caller_kwargs.items()
if k in hook_impl.kwargnames
}
except KeyError:
for argname in hook_impl.argnames:
if argname not in caller_kwargs:
Expand All @@ -82,7 +86,7 @@ def _multicall(
try:
# If this cast is not valid, a type error is raised below,
# which is the desired response.
res = hook_impl.function(*args)
res = hook_impl.function(*args, **kwargs)
wrapper_gen = cast(Generator[None, Result[object], None], res)
next(wrapper_gen) # first yield
teardowns.append((wrapper_gen, hook_impl))
Expand All @@ -92,14 +96,14 @@ def _multicall(
try:
# If this cast is not valid, a type error is raised below,
# which is the desired response.
res = hook_impl.function(*args)
res = hook_impl.function(*args, **kwargs)
function_gen = cast(Generator[None, object, object], res)
next(function_gen) # first yield
teardowns.append(function_gen)
except StopIteration:
_raise_wrapfail(function_gen, "did not yield")
else:
res = hook_impl.function(*args)
res = hook_impl.function(*args, **kwargs)
if res is not None:
results.append(res)
if firstresult: # halt further impl calls
Expand Down
11 changes: 9 additions & 2 deletions testing/test_multicall.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,15 @@ def test_keyword_args_with_defaultargs() -> None:
def f(x, z=1):
return x + z

reslist = MC([f], dict(x=23, y=24))
assert reslist == [24]
@hookimpl
def f2(x, y=1):
return x + y

reslist = MC([f, f2], dict(x=23, y=24))
assert reslist == [23 + 24, 23 + 1]

reslist = MC([f2], dict(x=23))
assert reslist == [23 + 1]


def test_tags_call_error() -> None:
Expand Down