Skip to content

feat: Register modifiers as Python functions#1557

Closed
nicolaassolini-qntm wants to merge 13 commits intomainfrom
na/1290-modifiers-as-python-functions
Closed

feat: Register modifiers as Python functions#1557
nicolaassolini-qntm wants to merge 13 commits intomainfrom
na/1290-modifiers-as-python-functions

Conversation

@nicolaassolini-qntm
Copy link
Contributor

@nicolaassolini-qntm nicolaassolini-qntm commented Mar 11, 2026

See #1290 as reference

Quite a hacky solution:

I have defined a new decorator guppy.modifier that registers the class as a RawModifierDef.
This definition is basically an empty definition (as we see in guppylang/src/guppylang/std/modifier.py and it is used to detect wrong modifier usage:


I also added a check here:
def _check_modifier_defined(ctx: Context, modifier_name: str, node: ast.expr) -> None:
"""Check that a modifier is properly registered as a ParsedModifierDef."""
to be sure that the modifier are imported and not overwritten (WIP)

I'm wondering about doing a complete refactor of the modifier, basically defining it as a special guppy function (this may solve other issues here, such as #1288

See modifier_playground.ipynb for an example.

P.S. ignore my TODO comment, is still a draft

@nicolaassolini-qntm nicolaassolini-qntm linked an issue Mar 11, 2026 that may be closed by this pull request
@github-actions
Copy link
Contributor

github-actions bot commented Mar 11, 2026

🐰 Bencher Report

Branchna/1290-modifiers-as-python-functions
TestbedLinux
Click to view all benchmark results
BenchmarkLatencyBenchmark Result
microseconds (µs)
(Result Δ%)
Upper Boundary
microseconds (µs)
(Limit %)
tests/benchmarks/test_big_array.py::test_big_array_check📈 view plot
🚷 view threshold
706,072.98 µs
(-1.25%)Baseline: 714,975.22 µs
750,723.98 µs
(94.05%)
tests/benchmarks/test_big_array.py::test_big_array_compile📈 view plot
🚷 view threshold
1,915,471.43 µs
(+3.68%)Baseline: 1,847,400.78 µs
1,939,770.82 µs
(98.75%)
tests/benchmarks/test_big_array.py::test_big_array_executable📈 view plot
🚷 view threshold
8,193,068.55 µs
(+3.67%)Baseline: 7,902,825.78 µs
8,297,967.07 µs
(98.74%)
tests/benchmarks/test_ctrl_flow.py::test_many_ctrl_flow_check📈 view plot
🚷 view threshold
49,638.86 µs
(-0.09%)Baseline: 49,683.69 µs
52,167.88 µs
(95.15%)
tests/benchmarks/test_ctrl_flow.py::test_many_ctrl_flow_compile📈 view plot
🚷 view threshold
106,267.90 µs
(-53.06%)Baseline: 226,400.21 µs
237,720.23 µs
(44.70%)
tests/benchmarks/test_ctrl_flow.py::test_many_ctrl_flow_executable📈 view plot
🚷 view threshold
594,480.06 µs
(-0.47%)Baseline: 597,261.20 µs
627,124.26 µs
(94.79%)
tests/benchmarks/test_prelude.py::test_import_guppy📈 view plot
🚷 view threshold
50.53 µs
(-7.67%)Baseline: 54.73 µs
57.47 µs
(87.93%)
🐰 View full continuous benchmarking report in Bencher

@github-actions
Copy link
Contributor

github-actions bot commented Mar 11, 2026

🐰 Bencher Report

Branchna/1290-modifiers-as-python-functions
TestbedLinux
Click to view all benchmark results
Benchmarkhugr_bytesBenchmark Result
bytes x 1e3
(Result Δ%)
Upper Boundary
bytes x 1e3
(Limit %)
hugr_nodesBenchmark Result
nodes
(Result Δ%)
Upper Boundary
nodes
(Limit %)
tests/benchmarks/test_big_array.py::test_big_array_compile📈 view plot
🚷 view threshold
141.73 x 1e3
(0.00%)Baseline: 141.73 x 1e3
143.15 x 1e3
(99.01%)
📈 view plot
🚷 view threshold
6,620.00
(0.00%)Baseline: 6,620.00
6,686.20
(99.01%)
tests/benchmarks/test_ctrl_flow.py::test_many_ctrl_flow_compile📈 view plot
🚷 view threshold
17.52 x 1e3
(0.00%)Baseline: 17.52 x 1e3
17.69 x 1e3
(99.01%)
📈 view plot
🚷 view threshold
581.00
(0.00%)Baseline: 581.00
586.81
(99.01%)
🐰 View full continuous benchmarking report in Bencher

@codecov-commenter
Copy link

Codecov Report

❌ Patch coverage is 76.92308% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.33%. Comparing base (aa4c94f) to head (b1894b9).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
guppylang/src/guppylang/decorator.py 25.00% 6 Missing ⚠️
.../src/guppylang_internals/checker/errors/generic.py 87.50% 1 Missing ⚠️
...ls/src/guppylang_internals/checker/expr_checker.py 75.00% 1 Missing ⚠️
...als/src/guppylang_internals/definition/modifier.py 93.33% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1557      +/-   ##
==========================================
- Coverage   93.39%   93.33%   -0.06%     
==========================================
  Files         128      129       +1     
  Lines       11970    12006      +36     
==========================================
+ Hits        11179    11206      +27     
- Misses        791      800       +9     

☔ 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.

@maximilianruesch maximilianruesch changed the title draft feat: Register modifiers as Python functions Mar 12, 2026
@nicolaassolini-qntm
Copy link
Contributor Author

I also wonder if a similar solution may be useful to catch other reserved keywords

@nicolaassolini-qntm
Copy link
Contributor Author

Issue #1290 closed in #1571.
In this pr may contain some hints for #1569

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