fix: Use type() instead of __class__ for FreeCAD C++ binding objects - #75
fix: Use type() instead of __class__ for FreeCAD C++ binding objects#75NeelChotai wants to merge 1 commit into
Conversation
FreeCAD C++ Python bindings can return a `dict` from `instead of the actual class type, causing `AttributeError: 'dict' object has no attribute '__name__'`. I specifically ran in to this on `inspect_object` screenshots, and workbench listing.
📝 WalkthroughWalkthroughRefactoring type-checking patterns across three bridge modules: replacing Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes 🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/freecad_mcp/bridge/embedded.py`:
- Around line 254-255: Add a regression test that exercises the fallback branch
in src/freecad_mcp/bridge/embedded.py where type(val).__module__ != 'builtins'
causes val to be converted to str(val): create a test (e.g.,
test_serialize_freecad_cpp_property) that constructs or mocks a FreeCAD
C++-backed object (or a dummy object whose __class__.__module__ is not
'builtins' and whose __class__ / __repr__ behavior is unreliable), call the
serialization routine that triggers the type(val) check, and assert the property
is serialized as the string returned by str(val) (and not by accessing
__class__); ensure the test covers the exact branch by verifying the code path
via the public serialization API used by embedded.py.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: b8f50fac-4665-4ba3-b3f9-40313508acc0
📒 Files selected for processing (3)
src/freecad_mcp/bridge/embedded.pysrc/freecad_mcp/bridge/socket.pysrc/freecad_mcp/bridge/xmlrpc.py
| if type(val).__module__ != 'builtins': | ||
| val = str(val) |
There was a problem hiding this comment.
Add regression coverage for the type(val) fallback path.
This is a bug-fix path, but no regression test is included for objects with unreliable __class__ behavior. Please add coverage for property serialization with FreeCAD C++ bindings to prevent reintroduction.
As per coding guidelines, "All code must have tests - create unit tests, integration tests, edge cases, and regression tests".
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@src/freecad_mcp/bridge/embedded.py` around lines 254 - 255, Add a regression
test that exercises the fallback branch in src/freecad_mcp/bridge/embedded.py
where type(val).__module__ != 'builtins' causes val to be converted to str(val):
create a test (e.g., test_serialize_freecad_cpp_property) that constructs or
mocks a FreeCAD C++-backed object (or a dummy object whose __class__.__module__
is not 'builtins' and whose __class__ / __repr__ behavior is unreliable), call
the serialization routine that triggers the type(val) check, and assert the
property is serialized as the string returned by str(val) (and not by accessing
__class__); ensure the test covers the exact branch by verifying the code path
via the public serialization API used by embedded.py.
… fix Our fixes: - .env in the working directory no longer crashes startup (spkane#105) - PartDesign features that silently do nothing are now caught (spkane#99) - empty-body Shape access no longer crashes pad onto a fresh body - mcp pinned below 2.x so a fresh pip install imports Backported from unmerged upstream PRs, original authorship preserved: - spkane#75 type() instead of __class__ for FreeCAD C++ bindings - spkane#101 count external geometry by subelement - spkane#102 flush paint events before screenshot capture Verified end to end against FreeCAD 1.1.3 in embedded mode: MCP tool -> EmbeddedBridge -> FreeCAD, 152 tools served over stdio. Claude-Session: https://claude.ai/code/session_01Y72E9EtDrSDkA9n1Fnu21r
FreeCAD C++ Python bindings can return a
dictfromobj.__class__instead of the actual class type, causingAttributeError: 'dict' object has no attribute '__name__'. I specifically ran in to this oninspect_object.