Skip to content

Commit 07caa46

Browse files
committed
Add pass name to error message
1 parent 97e5406 commit 07caa46

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

hugr-py/src/hugr/passes/_composable_pass.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def then(self, other: ComposablePass) -> ComposablePass:
4444

4545

4646
def impl_pass_run(
47+
pass_: ComposablePass,
4748
*,
4849
hugr: Hugr,
4950
inplace: bool,
@@ -55,6 +56,7 @@ def impl_pass_run(
5556
5657
At least one of the `inplace_call` or `copy_call` arguments must be provided.
5758
59+
:param pass_: The pass being run. Used for error messages.
5860
:param hugr: The Hugr to apply the pass to.
5961
:param inplace: Whether to apply the pass inplace.
6062
:param inplace_call: The method to apply the pass inplace.
@@ -79,7 +81,7 @@ def impl_pass_run(
7981
pass_result.original_dirty = False
8082
return pass_result
8183
else:
82-
msg = "Pass must implement at least an inplace or copy run method"
84+
msg = f"{pass_.name} needs to implement at least an inplace or copy run method"
8385
raise ValueError(msg)
8486

8587

@@ -106,6 +108,7 @@ def apply(hugr: Hugr) -> PassResult:
106108
return pass_result
107109

108110
return impl_pass_run(
111+
self,
109112
hugr=hugr,
110113
inplace=inplace,
111114
inplace_call=apply,

hugr-py/tests/test_passes.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from copy import deepcopy
22

3+
import pytest
4+
35
from hugr.hugr.base import Hugr
46
from hugr.passes._composable_pass import (
57
ComposablePass,
@@ -13,6 +15,7 @@ def test_composable_pass() -> None:
1315
class MyDummyInlinePass(ComposablePass):
1416
def run(self, hugr: Hugr, inplace: bool = True) -> PassResult:
1517
return impl_pass_run(
18+
self,
1619
hugr=hugr,
1720
inplace=inplace,
1821
inplace_call=lambda hugr: PassResult.for_pass(
@@ -28,6 +31,7 @@ def run(self, hugr: Hugr, inplace: bool = True) -> PassResult:
2831
class MyDummyCopyPass(ComposablePass):
2932
def run(self, hugr: Hugr, inplace: bool = True) -> PassResult:
3033
return impl_pass_run(
34+
self,
3135
hugr=hugr,
3236
inplace=inplace,
3337
copy_call=lambda hugr: PassResult.for_pass(
@@ -82,3 +86,20 @@ def run(self, hugr: Hugr, inplace: bool = True) -> PassResult:
8286
("MyDummyCopyPass", None),
8387
]
8488
assert copy_result.hugr is not hugr
89+
90+
91+
def test_invalid_composable_pass() -> None:
92+
class MyDummyInvalidPass(ComposablePass):
93+
def run(self, hugr: Hugr, inplace: bool = True) -> PassResult:
94+
return impl_pass_run(
95+
self,
96+
hugr=hugr,
97+
inplace=inplace,
98+
)
99+
100+
dummy_invalid = MyDummyInvalidPass()
101+
with pytest.raises(
102+
ValueError,
103+
match="MyDummyInvalidPass needs to implement at least an inplace or copy run method", # noqa: E501
104+
):
105+
dummy_invalid.run(Hugr())

0 commit comments

Comments
 (0)