Fix StopIteration when computing mass of a compound containing an empty compound (#2078) - #2079
Draft
youdie006 wants to merge 1 commit into
Draft
Conversation
…ty compound A bare empty compound reports a mass of 0.0, but a non-empty compound whose first child is an empty compound raised StopIteration instead. Shape._mass_calc_function picks the mass function from the first non-compound descendant of a compound. It guarded the top-level empty case with `if obj:` but then descended with a bare next(iter(child)); Shape.__iter__ yields nothing for an empty compound, so next() raised. This is self-inconsistent: a bare empty compound already resolves to 0.0, so nesting it inside another compound must not flip that into a crash. Use the two-argument next(..., None) sentinel so both the top-level-empty case (unchanged) and the nested-empty case resolve to SOLID -> 0.0. Non-empty compounds still descend to their first non-compound child exactly as before. The same helper backs Volume(), Area() and centerOfMass(), so all three are fixed. Fixes CadQuery#2078.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2079 +/- ##
==========================================
- Coverage 95.72% 95.71% -0.01%
==========================================
Files 30 30
Lines 9533 9531 -2
Branches 1421 1420 -1
==========================================
- Hits 9125 9123 -2
Misses 253 253
Partials 155 155 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
adam-urbanczyk
marked this pull request as draft
August 19, 2026 17:54
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.
Fixes #2078.
Repro
A bare empty compound reports a mass of
0.0, but a non-empty compound whose first child is an empty compound raisesStopIterationinstead.Root cause
Shape._mass_calc_functionpicks the mass function from the first non-compound descendant of a compound. It guarded the top-level empty case withif obj:but then descended with a barenext(iter(child)):Shape.__iter__yields nothing for an empty compound, sonext()raises. This is self-inconsistent: a bare empty compound already resolves to0.0, so nesting it inside another compound must not flip that into a crash.Fix
Use the two-argument
next(..., None)sentinel so both the top-level-empty case (unchanged) and the nested-empty case resolve toSOLID -> 0.0. Non-empty compounds still descend to their first non-compound child exactly as before.The same helper backs
Volume(),Area(), andcenterOfMass(), so all three are fixed (verified: nested-emptycenterOfMass()now returns(0, 0, 0)).Test
Adds
test_nested_empty_compound_masstotests/test_shapes.py, asserting the bare-empty control plusVolume()/Area()of a nested-empty compound are0.0. Fails withStopIterationbefore the fix, passes after.Thanks to @johnbeard for the clear report and minimal repro.
AI-assisted: this fix was prepared with the help of an AI coding assistant and reviewed by me.