Skip to content

fix: Use type() instead of __class__ for FreeCAD C++ binding objects - #75

Open
NeelChotai wants to merge 1 commit into
spkane:mainfrom
NeelChotai:neel/fix-cpp-types
Open

fix: Use type() instead of __class__ for FreeCAD C++ binding objects#75
NeelChotai wants to merge 1 commit into
spkane:mainfrom
NeelChotai:neel/fix-cpp-types

Conversation

@NeelChotai

@NeelChotai NeelChotai commented Apr 12, 2026

Copy link
Copy Markdown

FreeCAD C++ Python bindings can return a dict from obj.__class__ instead of the actual class type, causing AttributeError: 'dict' object has no attribute '__name__'. I specifically ran in to this on inspect_object.

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.
@coderabbitai

coderabbitai Bot commented Apr 12, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

Refactoring type-checking patterns across three bridge modules: replacing .__class__.__module__ and .__class__.__name__ attribute access with direct type() function calls for cleaner, more idiomatic Python.

Changes

Cohort / File(s) Summary
Type-checking refactoring
src/freecad_mcp/bridge/embedded.py, src/freecad_mcp/bridge/socket.py, src/freecad_mcp/bridge/xmlrpc.py
Replaced val.__class__.__module__ with type(val).__module__ in property sanitization and active_wb.__class__.__name__ with type(active_wb).__name__ in workbench detection. Socket bridge also updates view-type validation to use type(view).__name__.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately captures the main change: replacing class with type() across multiple bridge files for FreeCAD C++ binding objects.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between d9a3711 and c84eb6d.

📒 Files selected for processing (3)
  • src/freecad_mcp/bridge/embedded.py
  • src/freecad_mcp/bridge/socket.py
  • src/freecad_mcp/bridge/xmlrpc.py

Comment on lines +254 to 255
if type(val).__module__ != 'builtins':
val = str(val)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

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.

rich-weight added a commit to rich-weight/freecad-robust-mcp that referenced this pull request Aug 25, 2026
… 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
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.

1 participant