Skip to content

to_compound: return an empty compound for an empty list instead of raising (#600) - #601

Open
youdie006 wants to merge 1 commit into
CadQuery:masterfrom
youdie006:fix/600-to-compound-empty-list
Open

to_compound: return an empty compound for an empty list instead of raising (#600)#601
youdie006 wants to merge 1 commit into
CadQuery:masterfrom
youdie006:fix/600-to-compound-empty-list

Conversation

@youdie006

Copy link
Copy Markdown

Fixes #600.

Problem

to_compound() type-checks list inputs by indexing obj[0] before it handles the empty-list case:

elif isinstance(obj, list) and isinstance(obj[0], cq.Workplane):

So to_compound([]) raises IndexError: list index out of range. This is on a live render path: cq_editor/widgets/object_tree.py calls to_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 (whose vals() is empty) already flows through cq.Compound.makeCompound([]). I confirmed cq.Compound.makeCompound([]) returns an empty Compound rather than raising.

elif isinstance(obj, list) and not obj:
    pass  # an empty list produces an empty compound

(The issue left the choice open between an empty compound and a raised error; empty compound keeps to_compound total over its declared input types and matches the empty-Workplane behavior. Happy to switch to a ValueError if you'd prefer.)

Test

Added test_to_compound_empty_list in tests/test_cq_utils.py. Red-green verified with pytest tests/test_cq_utils.py: before the change the test raises IndexError; after, to_compound([]) returns an empty cq.Compound. The existing test_cq_utils.py test 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.

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

to_compound raises IndexError for an empty list

1 participant