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(optimize): support kwargs in 'register_strategy' decorator #583

Merged
merged 1 commit into from
Oct 3, 2024
Merged
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
6 changes: 4 additions & 2 deletions src/taskgraph/optimize/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@
registry = {}


def register_strategy(name, args=()):
def register_strategy(name, args=(), kwargs=None):
kwargs = kwargs or {}

def wrap(cls):
if name not in registry:
registry[name] = cls(*args)
registry[name] = cls(*args, **kwargs)
if not hasattr(registry[name], "description"):
registry[name].description = name
return cls
Expand Down
15 changes: 14 additions & 1 deletion test/test_optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@

from taskgraph.graph import Graph
from taskgraph.optimize import base as optimize_mod
from taskgraph.optimize.base import All, Any, Not, OptimizationStrategy
from taskgraph.optimize.base import (
All,
Any,
Not,
OptimizationStrategy,
register_strategy,
)
from taskgraph.task import Task
from taskgraph.taskgraph import TaskGraph

Expand Down Expand Up @@ -467,3 +473,10 @@ def test_get_subgraph_removed_dep():
graph = make_triangle()
with pytest.raises(Exception):
optimize_mod.get_subgraph(graph, {"t2"}, set(), {})


def test_register_strategy(mocker):
m = mocker.Mock()
func = register_strategy("foo", args=("one", "two"), kwargs={"n": 1})
func(m)
m.assert_called_with("one", "two", n=1)
Loading