Skip to content

Conversation

@CalMacCQ
Copy link
Contributor

@CalMacCQ CalMacCQ commented Nov 18, 2025

Implementing the revisions to ComposablePass and ComposedPass as discussed with Agustin.

I still find the _apply and _apply_inplace rather unintuitive (although now I realise that it will work). Looking at the default implementations in the ComposablePass protocol these look circular but as you're always overrididing at least one of the _apply or _apply_inplace methods whenever you implement the protocol.

Would be interested to know if there are suggestions to improve this.

@CalMacCQ CalMacCQ requested a review from a team as a code owner November 18, 2025 11:49
@CalMacCQ CalMacCQ requested a review from acl-cqc November 18, 2025 11:49
@CalMacCQ CalMacCQ marked this pull request as draft November 18, 2025 11:50
passes: list[ComposablePass]

def __call__(self, hugr: Hugr):
def __call__(self, hugr: Hugr, inplace: bool = True) -> Hugr:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Make sure to overrite _apply and _apply_inline, in case some implementor calls them directly

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@CalMacCQ CalMacCQ changed the title feat: Make ComposablePass return a Hugr feat: Make ComposablePass.__call__ return a Hugr Nov 18, 2025
@codecov
Copy link

codecov bot commented Nov 19, 2025

Codecov Report

❌ Patch coverage is 14.70588% with 29 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.48%. Comparing base (e610678) to head (6d26c81).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
hugr-py/src/hugr/passes/_composable_pass.py 14.28% 24 Missing ⚠️
hugr-py/src/hugr/hugr/base.py 16.66% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2697      +/-   ##
==========================================
- Coverage   83.54%   83.48%   -0.06%     
==========================================
  Files         266      266              
  Lines       51714    51650      -64     
  Branches    47180    47086      -94     
==========================================
- Hits        43202    43121      -81     
- Misses       6134     6153      +19     
+ Partials     2378     2376       -2     
Flag Coverage Δ
python 90.99% <14.70%> (-0.54%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@CalMacCQ CalMacCQ marked this pull request as ready for review November 19, 2025 13:57
@CalMacCQ CalMacCQ requested review from ss2165 and removed request for acl-cqc November 19, 2025 14:01
Copy link
Contributor

@acl-cqc acl-cqc left a comment

Choose a reason for hiding this comment

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

Thanks, Callum - think the interface is ok, I don't have a great solution to the override-at-least-one problem, but a couple of things....

"""Call all of the passes in sequence."""
def _apply(self, hugr: Hugr) -> Hugr:
for comp_pass in self.passes:
res = comp_pass(hugr, inplace=False)
Copy link
Contributor

Choose a reason for hiding this comment

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

This returns the result of the last pass only! You should probably initialize via res = hugr outside the loop and then

Suggested change
res = comp_pass(hugr, inplace=False)
res = comp_pass(res, inplace=False)

I'm surprised mypy doesn't complain about this, as if self.passes is empty then return res is returning an uninitialized variable

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch! see bfb6f26.

# At least one of the following _apply methods must be ovewritten
def _apply(self, hugr: Hugr) -> Hugr:
hugr = deepcopy(hugr)
self._apply_inplace(hugr)
Copy link
Contributor

Choose a reason for hiding this comment

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

If we were really concerned....you could have an _in_progress:bool on the instance of ComposablePass (not sure but this might mean setattr??), and then check that you didn't get back here with that set (and error if you do)....

I don't see a great solution tho, no

Copy link
Collaborator

@aborgna-q aborgna-q left a comment

Choose a reason for hiding this comment

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

#2703 builds on this, so lets merge and iterate over it

@CalMacCQ CalMacCQ enabled auto-merge November 24, 2025 10:22
@CalMacCQ CalMacCQ added this pull request to the merge queue Nov 24, 2025
Merged via the queue into main with commit dbf8c8e Nov 24, 2025
28 of 29 checks passed
@CalMacCQ CalMacCQ deleted the cm/overwrite_hugr_method branch November 24, 2025 10:31
github-merge-queue bot pushed a commit that referenced this pull request Nov 24, 2025
Includes an idea for simplifying the protocol's `_apply`/`_apply_inline`
from #2697 by providing a helper function instead
(859c811).

---------

Co-authored-by: Callum Macpherson <[email protected]>
Co-authored-by: Callum Macpherson <[email protected]>
Co-authored-by: Alan Lawrence <[email protected]>
aborgna-q added a commit that referenced this pull request Nov 24, 2025
Implementing the revisions to `ComposablePass` and `ComposedPass` as
discussed with Agustin.

I still find the `_apply` and `_apply_inplace` rather unintuitive
(although now I realise that it will work). Looking at the default
implementations in the `ComposablePass` protocol these look circular but
as you're always overrididing at least one of the `_apply` or
`_apply_inplace` methods whenever you implement the protocol.


Would be interested to know if there are suggestions to improve this.

---------

Co-authored-by: Agustín Borgna <[email protected]>
Co-authored-by: Agustín Borgna <[email protected]>
aborgna-q added a commit that referenced this pull request Nov 24, 2025
Includes an idea for simplifying the protocol's `_apply`/`_apply_inline`
from #2697 by providing a helper function instead
(859c811).

---------

Co-authored-by: Callum Macpherson <[email protected]>
Co-authored-by: Callum Macpherson <[email protected]>
Co-authored-by: Alan Lawrence <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants