Skip to content

fix: Raise error when using an enum class in places where a variant is expected#1575

Open
nicolaassolini-qntm wants to merge 2 commits intomainfrom
na/1572-assertionerror-wrong-type-with-enum
Open

fix: Raise error when using an enum class in places where a variant is expected#1575
nicolaassolini-qntm wants to merge 2 commits intomainfrom
na/1572-assertionerror-wrong-type-with-enum

Conversation

@nicolaassolini-qntm
Copy link
Contributor

closes #1572

When an enum class is used as a value (e.g., x = MyEnum), the checker now raises an ExpectedError (Added a new EnumHelp class to provide a help message in error diagnostics)

To spot the error I moved the logic in the function ExprSynthesizer.visit_Name in expr_checker.py to a helper ExprSynthesizer._check_name_id. In this way, when visiting a Name, _check_name_id raise an error if it finds an Enum Def (we are in x = MyEnum case), while calling _check_name_id from visit_Attribute does not cause the error, since x = MyEnum.Variant is valid.

Added 3 new tests (use_enum_class1.py, use_enum_class2.py, use_enum_class3.py) to test these cases.

@nicolaassolini-qntm nicolaassolini-qntm requested a review from a team as a code owner March 19, 2026 13:37
@github-actions
Copy link
Contributor

github-actions bot commented Mar 19, 2026

🐰 Bencher Report

Branchna/1572-assertionerror-wrong-type-with-enum
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,114.31 µs
(-0.96%)Baseline: 712,939.31 µs
748,586.28 µs
(94.33%)
tests/benchmarks/test_big_array.py::test_big_array_compile📈 view plot
🚷 view threshold
1,955,456.23 µs
(+1.68%)Baseline: 1,923,097.72 µs
2,019,252.61 µs
(96.84%)
tests/benchmarks/test_big_array.py::test_big_array_executable📈 view plot
🚷 view threshold
8,117,235.99 µs
(+0.21%)Baseline: 8,100,285.18 µs
8,505,299.44 µs
(95.44%)
tests/benchmarks/test_ctrl_flow.py::test_many_ctrl_flow_check📈 view plot
🚷 view threshold
49,846.70 µs
(-57.70%)Baseline: 117,830.27 µs
123,721.79 µs
(40.29%)
tests/benchmarks/test_ctrl_flow.py::test_many_ctrl_flow_compile📈 view plot
🚷 view threshold
106,530.92 µs
(-2.39%)Baseline: 109,138.08 µs
114,594.98 µs
(92.96%)
tests/benchmarks/test_ctrl_flow.py::test_many_ctrl_flow_executable📈 view plot
🚷 view threshold
600,549.01 µs
(-0.21%)Baseline: 601,829.59 µs
631,921.07 µs
(95.04%)
tests/benchmarks/test_prelude.py::test_import_guppy📈 view plot
🚷 view threshold
51.87 µs
(-3.23%)Baseline: 53.60 µs
56.28 µs
(92.16%)
🐰 View full continuous benchmarking report in Bencher

@github-actions
Copy link
Contributor

github-actions bot commented Mar 19, 2026

🐰 Bencher Report

Branchna/1572-assertionerror-wrong-type-with-enum
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.79 x 1e3
(0.00%)Baseline: 141.79 x 1e3
143.21 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.57 x 1e3
(0.00%)Baseline: 17.57 x 1e3
17.75 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 96.55172% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 93.55%. Comparing base (2201152) to head (bbe2f8a).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...ls/src/guppylang_internals/checker/expr_checker.py 96.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1575      +/-   ##
==========================================
+ Coverage   93.53%   93.55%   +0.01%     
==========================================
  Files         130      130              
  Lines       12291    12298       +7     
==========================================
+ Hits        11496    11505       +9     
+ Misses        795      793       -2     

☔ 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 fix: assertion error wrong type with enum fix: Raise error when using an enum class in places where a variant is expected Mar 19, 2026
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.

[Bug]: AssertionError when returning a wrong type with Enum

2 participants