to_compound: return an empty compound for an empty list instead of raising (#600) - #601
Open
youdie006 wants to merge 1 commit into
Open
to_compound: return an empty compound for an empty list instead of raising (#600)#601youdie006 wants to merge 1 commit into
youdie006 wants to merge 1 commit into
Conversation
…ising to_compound() type-checked list inputs with isinstance(obj[0], ...) before handling the empty-list case, so to_compound([]) raised IndexError: list index out of range. This reached the object-tree render path (to_compound(node.obj).moved(...)) for an empty selection or intermediate result and crashed it. Handle an empty list explicitly, producing an empty compound (consistent with an empty Workplane). Add a regression test. Fixes CadQuery#600
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 #600.
Problem
to_compound()type-checks list inputs by indexingobj[0]before it handles the empty-list case:So
to_compound([])raisesIndexError: list index out of range. This is on a live render path:cq_editor/widgets/object_tree.pycallsto_compound(node.obj).moved(world), so an empty selection or an empty intermediate result crashes the object-tree/assembly render.Fix
Handle an empty list explicitly and return an empty compound, which is consistent with how an empty
Workplane(whosevals()is empty) already flows throughcq.Compound.makeCompound([]). I confirmedcq.Compound.makeCompound([])returns an emptyCompoundrather than raising.(The issue left the choice open between an empty compound and a raised error; empty compound keeps
to_compoundtotal over its declared input types and matches the empty-Workplanebehavior. Happy to switch to aValueErrorif you'd prefer.)Test
Added
test_to_compound_empty_listintests/test_cq_utils.py. Red-green verified withpytest tests/test_cq_utils.py: before the change the test raisesIndexError; after,to_compound([])returns an emptycq.Compound. The existingtest_cq_utils.pytest still passes.Disclosure: I used AI assistance (Claude) while preparing this change. I reproduced the crash, ran the tests (red-green), and take responsibility for the contribution.