Skip to content

feat!: Reject early bad calls to OpDef.instantiate - #3200

Open
croyzor wants to merge 2 commits into
mainfrom
feat/instantiate-cases
Open

feat!: Reject early bad calls to OpDef.instantiate#3200
croyzor wants to merge 2 commits into
mainfrom
feat/instantiate-cases

Conversation

@croyzor

@croyzor croyzor commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

OpDef.instantiate requires a concrete signature to be provided in order to add a polymorphic operation to the graph. Otherwise, types in the operation's signature will be left out, meaning the resulting hugr wont validate, as in #3189.

Here, I try to raise this error earlier, by checking if the concrete signature is necessary, as well as raising errors for an incorrect number of type args.


Aside

The signature of instantiate:

instantiate(args: Sequence[TypeArg] | None, sig: FunctionType | None)

should really be

instantiate(concrete: tuple[Sequence[TypeArg], FunctionType] | None)

since the FunctionType needs to be provided whenever any type args are given. But this isn't usually done in python, so I thought I'd get pushback for the ugly API


BREAKING CHANGE: Raise errors in bad calls to OpDef.instantiate

@croyzor
croyzor requested a review from aborgna-q August 11, 2026 09:15
@croyzor
croyzor requested a review from a team as a code owner August 11, 2026 09:15
@codecov

codecov Bot commented Aug 11, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.45%. Comparing base (928d7b2) to head (9caec74).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3200   +/-   ##
=======================================
  Coverage   81.45%   81.45%           
=======================================
  Files         243      243           
  Lines       46953    46967   +14     
  Branches    40702    40702           
=======================================
+ Hits        38245    38259   +14     
  Misses       6706     6706           
  Partials     2002     2002           
Flag Coverage Δ
python 89.40% <100.00%> (+0.02%) ⬆️

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

☔ View full report in Codecov by Harness.
📢 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.

Comment thread hugr-py/src/hugr/ext.py
Comment on lines 264 to 266
concrete_signature: Concrete function type of the operation, only required
if the operation is polymorphic.
"""

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Add a Raises section to the docstring

Comment thread hugr-py/src/hugr/ext.py Outdated
f"Too many args: Op {self.name} takes {len(params)} args, "
f"but was given {num_args}"
)
raise TypeError(msg)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

These should rather be ValueErrors, the types are correct.

Comment thread hugr-py/src/hugr/ext.py Outdated
params = self.signature.poly_func.params
if num_args > len(params):
msg = (
f"Too many args: Op {self.name} takes {len(params)} args, "

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Use self.qualified_name(), so the op is clearly disambiguated.

Comment thread hugr-py/src/hugr/ext.py

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Do we need to make this a breaking change?

Cases that would error out now produced invalid hugrs before, so we are only breaking invalid uses 🤔

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Before this PR, we are successfully making the hugrs, but with unsolved type variables. I remember that at some point, when parsing the model we wanted to be able to silently solve these variables when possible, so I'm not sure if we can say the hugrs are invalid?

My thinking about the breakage is that people could have programs generating hugrs that they haven't validated, which would now surprisingly break

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.

2 participants