Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
eeb4c50
Agustin's suggested Protocol class
CalMacCQ Oct 27, 2025
c5cf1d7
minor fixes
CalMacCQ Oct 27, 2025
8a3d6ba
more methods
CalMacCQ Oct 27, 2025
7b58c05
import Self from typing_extensions
CalMacCQ Oct 27, 2025
33e325b
Merge branch 'main' into cm/composable_pass
CalMacCQ Oct 27, 2025
5e31a3d
fix import
CalMacCQ Oct 27, 2025
c52de85
remove to_dict and from_dict methods as well as supported Ops
CalMacCQ Oct 27, 2025
6d7afc2
remove unused import
CalMacCQ Oct 27, 2025
89fabf0
node -> entrypoint
CalMacCQ Oct 27, 2025
91879cd
Merge branch 'main' into cm/composable_pass
CalMacCQ Oct 27, 2025
4878d5f
rename and update __init__.py
CalMacCQ Oct 27, 2025
7045270
Merge branch 'main' into cm/composable_pass
CalMacCQ Oct 27, 2025
d20aaaa
Merge branch 'main' into cm/composable_pass
CalMacCQ Oct 27, 2025
954730f
make a passes module
CalMacCQ Oct 28, 2025
448b5cf
move passes into hugr root
CalMacCQ Oct 28, 2025
17d1dc8
remove from build
CalMacCQ Oct 28, 2025
b6fa9a5
add __init__.py for passes
CalMacCQ Oct 28, 2025
cc9c151
undo init changes
CalMacCQ Oct 28, 2025
2d10e87
update __all__
CalMacCQ Oct 28, 2025
45bd86a
fix __all__ again
CalMacCQ Oct 28, 2025
6ca653e
minimal methods for ComposablePass
CalMacCQ Nov 12, 2025
9171dba
add ComposedPass dataclass
CalMacCQ Nov 12, 2025
c6fbfc0
fix to comp_pass
CalMacCQ Nov 12, 2025
fcf00c0
docstrings
CalMacCQ Nov 12, 2025
3ae525c
formatting stuff
CalMacCQ Nov 12, 2025
78e7522
Merge branch 'main' into cm/composable_pass
CalMacCQ Nov 12, 2025
af225a3
more ruff check
CalMacCQ Nov 12, 2025
92964a8
provide a default implementation of then() method
CalMacCQ Nov 12, 2025
f139d7c
fix default impl
CalMacCQ Nov 12, 2025
df0ab15
fixup then impl
CalMacCQ Nov 12, 2025
ac63dd5
fixup mypy and ruff
CalMacCQ Nov 12, 2025
9a157b8
yet another fix
CalMacCQ Nov 12, 2025
3db5bdd
more ruff nonsense
CalMacCQ Nov 12, 2025
67d7d38
rm self
CalMacCQ Nov 12, 2025
975ddbb
fixes
CalMacCQ Nov 12, 2025
0165ae7
Update hugr-py/src/hugr/passes/composable_pass.py
CalMacCQ Nov 12, 2025
2def5fc
fmt
CalMacCQ Nov 12, 2025
9673dc2
make name a property
CalMacCQ Nov 12, 2025
01b7126
add a dummy test
CalMacCQ Nov 12, 2025
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
1 change: 1 addition & 0 deletions hugr-py/src/hugr/passes/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""A hugr-py passes module for hugr transformations."""
51 changes: 51 additions & 0 deletions hugr-py/src/hugr/passes/composable_pass.py
Copy link
Collaborator

Choose a reason for hiding this comment

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

C.I. failure to do with coverage checks or something?

Yeah, it's complaining that there are no tests here.

You can see the codecov report linked from the message.

We could add a test with a dummy no-op pass

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in 01b7126

Some maybe the isinstance checks are not needed. I also made ComposablePass runtime checkable.

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""A Protocol for a composable pass."""

from __future__ import annotations

from dataclasses import dataclass
from typing import TYPE_CHECKING, Protocol, runtime_checkable

if TYPE_CHECKING:
from hugr.hugr.base import Hugr


@runtime_checkable
class ComposablePass(Protocol):
"""A Protocol which represents a composable Hugr transformation."""

def __call__(self, hugr: Hugr) -> None:
"""Call the pass to transform a HUGR."""
...

@property
def name(self) -> str:
"""Returns the name of the pass."""
return self.__class__.__name__

def then(self, other: ComposablePass) -> ComposablePass:
"""Perform another composable pass after this pass."""
# Provide a default implementation for composing passes.
pass_list = []
if isinstance(self, ComposedPass):
pass_list.extend(self.passes)
else:
pass_list.append(self)

if isinstance(other, ComposedPass):
pass_list.extend(other.passes)
else:
pass_list.append(other)

return ComposedPass(pass_list)


@dataclass
class ComposedPass(ComposablePass):
"""A sequence of composable passes."""

passes: list[ComposablePass]

def __call__(self, hugr: Hugr):
"""Call all of the passes in sequence."""
for comp_pass in self.passes:
comp_pass(hugr)
26 changes: 26 additions & 0 deletions hugr-py/tests/test_passes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from hugr.hugr.base import Hugr
from hugr.passes.composable_pass import ComposablePass, ComposedPass


def test_composable_pass() -> None:
class MyDummyPass(ComposablePass):
def __call__(self, hugr: Hugr) -> None:
return self(hugr)

def then(self, other: ComposablePass) -> ComposablePass:
return ComposedPass([self, other])

Comment on lines +10 to +12
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let it use the default implementation (we want to test that rather than test code)

Suggested change
def then(self, other: ComposablePass) -> ComposablePass:
return ComposedPass([self, other])

Copy link
Member

Choose a reason for hiding this comment

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

This suggestion wasn't implemented?

Copy link
Collaborator

Choose a reason for hiding this comment

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

@property
def name(self) -> str:
return "Dummy"

dummy = MyDummyPass()

composed = dummy.then(dummy)

my_composed_pass = ComposedPass([dummy, dummy])

assert my_composed_pass.passes == [dummy, dummy]
assert isinstance(my_composed_pass, ComposablePass)
assert isinstance(composed, ComposablePass)
assert dummy.name == "Dummy"
Loading