Merged
Conversation
…arsing and checking (#1419) * Added a new module `guppylang_internals/definition/enum.py` containing `RawEnumDef`, `ParsedEnumDef`, and `CheckedEnumDef`(WIP) * Added a `guppylang_internals/definition/util.py` module with common function and classes used by `enum.py`, `struct.py` and `function.py` * Improved error diagnostics for unexpected statements in enum and struct definitions by adding contextual help messages BREAKING CHANGE: Refactoring and code organization: * Moved shared parsing logic for classes (`parse_py_class`, `extract_generic_params`, `try_parse_generic_base`, `extract_generic_params`, `params_from_ast`) into a new utility module, `definition/util.py`, and updated both `enum.py`, `struct.py` and `function.py` to use these utilities. * Moved error classes and parsing functions from `struct.py` and `function.py` (`RepeatedTypeParamError`, `DuplicateFieldError`, `RedundantParamsError`) * Defined UncheckedField and CheckedField instead of UncheckedStructField and StructField --------- Co-authored-by: tatiana-s <tatiana.sedelnikov@quantinuum.com> Co-authored-by: Mark Koch <48097969+mark-koch@users.noreply.github.com>
# PR 1: Minimal Enum Infrastructure ## Summary This PR implements the minimal foundation for enum support with recursion checking. **Implements**: Issue #1423 (check_not_recursive for enum) **Related**: Issue #1333 (Enums tracking), Issue #1422 (Future cleanup) ## Changes ### New Files - `guppylang-internals/src/guppylang_internals/definition/enum.py` (~300 lines) - `ParsedEnumDef`, `CheckedEnumDef` classes - `UncheckedEnumVariant`, `EnumVariant` classes - `check_not_recursive()` function ### Modified Files - `guppylang-internals/src/guppylang_internals/tys/ty.py` - Added `EnumType` class (~50 lines) - Updated `ParametrizedType` TypeAlias - Added forward references ### Tests - `tests/error/enum_errors/test_recursive_minimal.py` - Manual test for direct recursion detection ## What This Enables ✅ Basic enum type definitions ✅ Recursion checking (prevents infinite loops) ✅ Type system integration ✅ Foundation for future enum features ## What's NOT Included (Future PRs) ❌ `@guppy.enum` decorator (PR 3) ❌ Enhanced error messages (PR 2) ❌ Auto-generated constructors (PR 3) ❌ DefId refactoring (PR 4) ## Testing ```bash # Run minimal test python tests/error/enum_errors/test_recursive_minimal.py # Check for syntax errors python -m py_compile guppylang-internals/src/guppylang_internals/definition/enum.py ``` ## Next Steps After this PR: - PR 2 will add better error handling - PR 3 will add the decorator and full API - PR 4 will replace the hacky implementation with DefId approach ``` --------- Co-authored-by: nicolaassolini-qntm <nicola.assolini@quantinuum.com> Co-authored-by: tatiana-s <tatiana.sedelnikov@quantinuum.com> Co-authored-by: Mark Koch <48097969+mark-koch@users.noreply.github.com> Co-authored-by: Craig Roy <craig.roy@quantinuum.com> Co-authored-by: Craig Roy <croyzor@users.noreply.github.com>
…n enums and structs, added two error tests (#1470) BREAKING CHANGES removed default value for `class_type`, field of `DuplicateFieldError` in `guppylang_internals/definition/util.py` --------- Co-authored-by: tatiana-s <tatiana.sedelnikov@quantinuum.com>
…AndDrop after rebase
…1505) BREAKING CHANGES: renamed `guppylang-internals/src/guppylang_internals/tys/printing.py:_visit_OpaqueType_StructType` in `visit_OpaqueType_StructType_EnumType` Co-authored-by: tatiana-s <tatiana.sedelnikov@quantinuum.com>
Collaborator
|
This PR contains breaking changes to the public Python API. Breaking changes summary |
Contributor
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1518 +/- ##
==========================================
+ Coverage 93.43% 93.53% +0.10%
==========================================
Files 128 130 +2
Lines 12056 12291 +235
==========================================
+ Hits 11264 11496 +232
- Misses 792 795 +3 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
84bd91d to
94c4d1f
Compare
Contributor
|
| Branch | feat/guppy-enum |
| Testbed | Linux |
Click to view all benchmark results
| Benchmark | hugr_bytes | Benchmark Result bytes x 1e3 (Result Δ%) | Upper Boundary bytes x 1e3 (Limit %) | hugr_nodes | Benchmark Result nodes (Result Δ%) | Upper Boundary nodes (Limit %) |
|---|---|---|---|---|---|---|
| tests/benchmarks/test_big_array.py::test_big_array_compile | 📈 view plot 🚷 view threshold | 141.79 x 1e3(-0.01%)Baseline: 141.81 x 1e3 | 143.23 x 1e3 (99.00%) | 📈 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.57 x 1e3(-0.09%)Baseline: 17.59 x 1e3 | 17.76 x 1e3 (98.93%) | 📈 view plot 🚷 view threshold | 581.00(0.00%)Baseline: 581.00 | 586.81 (99.01%) |
tatiana-s
approved these changes
Mar 17, 2026
Contributor
tatiana-s
left a comment
There was a problem hiding this comment.
Since this has mostly been reviewed already, looks good to me besides a few smaller comments - regarding the PR title, I think something like "feat!: Add enum type" seems better for the release summary as the rest of the current title is a bit redundant
tatiana-s
reviewed
Mar 17, 2026
This was referenced Mar 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request introduces enums in guppy, with all the compilation pipeline required, as well as a refactor to avoid code duplication.
Replaced references to
StructFieldwith a new, more generalCheckedFieldtype across the codebase, allowing for more flexible handling of both struct fields and enum variants in diagnostics and error messages.Improved error messages for attribute access, especially for enums, by distinguishing between "field or method" and "variant or method" in the
AttributeNotFoundError.Added support for the instantiation of Enum and the use of Enum methods.
Added test cases for Enum
BREAKING CHANGES: renamed
guppylang-internals/src/guppylang_internals/tys/printing.py:_visit_OpaqueType_StructTypeinvisit_OpaqueType_StructType_EnumTypeBREAKING CHANGES: removed default value for
class_type, field ofDuplicateFieldErroringuppylang_internals/definition/util.pyBREAKING CHANGE: Moved shared parsing logic for classes (
parse_py_class,extract_generic_params,try_parse_generic_base,extract_generic_params,params_from_ast) into a new utility module,definition/util.py, and updated bothenum.py,struct.pyandfunction.pyto use these utilities.BREAKING CHANGE: Moved error classes and parsing functions from
struct.pyandfunction.py(RepeatedTypeParamError,DuplicateFieldError,RedundantParamsError)BREAKING CHANGE: Defined UncheckedField and CheckedField instead of UncheckedStructField and StructField
Not a
guppylangbreaking changeBEGIN_COMMIT_OVERRIDE
feat: Add enum type (#1518)
END_COMMIT_OVERRIDE