Skip to content

Accept os.PathLike wherever a str path is accepted - #2064

Open
HaozheZhang6 wants to merge 5 commits into
CadQuery:masterfrom
HaozheZhang6:feat/pathlike-support
Open

Accept os.PathLike wherever a str path is accepted#2064
HaozheZhang6 wants to merge 5 commits into
CadQuery:masterfrom
HaozheZhang6:feat/pathlike-support

Conversation

@HaozheZhang6

Copy link
Copy Markdown
Contributor

Closes #2016.

Passing a pathlib.Path to any of the IO entry points fails today:

exporters.export(box, Path("out.step"))   # AttributeError: 'PosixPath' object has no attribute 'split'
box.val().exportStep(Path("out.step"))    # TypeError: Write(): incompatible function arguments
importers.importStep(Path("in.step"))     # TypeError: ReadFile(): incompatible function arguments
Assembly(box).export(Path("out.step"))    # AttributeError: 'PosixPath' object has no attribute 'split'

Annotations are widened to str | os.PathLike[str] (PathLike in cadquery/types.py), and the value is normalized with os.fspath() where it reaches a string operation, OCCT or VTK. Functions that only forward the path are left to the callee to normalize.

Covered: exporters.export, exportSVG, exportDXF, exportVTP, exportAssembly, exportStepMeta, exportCAF, exportVTKJS, exportVRML, exportGLTF, importShape, importStep, importDXF, importBrep, importBin, the assembly importers, Shape.export*/import*, Workplane.export/exportSvg, Sketch.export/importDXF, Assembly.save/export/load/importStep.

Shape.exportBrep/importBrep/exportBin/importBin also take a BytesIO, so those go through a small _fspath helper that passes file objects through untouched.

Tests: test_export_pathlike (every format exporters.export supports, plus the Shape/Workplane/Sketch methods), TestImporters.testPathLike, and test_pathlike in the assembly tests. All three fail on master with the AttributeError above.

Every exporter and importer entry point took str only, so passing a
pathlib.Path raised AttributeError ('PosixPath' object has no attribute
'split') or a TypeError from the OCP/VTK call underneath.

Widen the annotations to str | os.PathLike[str] and normalize with
os.fspath() where the value reaches a string operation, OCCT, or VTK.
@HaozheZhang6
HaozheZhang6 force-pushed the feat/pathlike-support branch from ed7d6a4 to 488639c Compare July 14, 2026 05:48
@adam-urbanczyk

Copy link
Copy Markdown
Member

@HaozheZhang6 will you fix the mypy errors or do you need help?

@HaozheZhang6

HaozheZhang6 commented Aug 1, 2026

Copy link
Copy Markdown
Contributor Author

Fixed. I missed _importDXF, widened it too, mypy is clean now. Also merged master: the merge dropped the import os in exporters/__init__.py, and exportDXFProjection was importing PathLike from os instead of the alias, so both use the alias now. Added it to the PathLike test.

@codecov

codecov Bot commented Aug 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.71%. Comparing base (b47f7b7) to head (ac7a2be).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #2064   +/-   ##
=======================================
  Coverage   95.70%   95.71%           
=======================================
  Files          30       30           
  Lines        9479     9498   +19     
  Branches     1410     1410           
=======================================
+ Hits         9072     9091   +19     
  Misses        253      253           
  Partials      154      154           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

Make Python API support os.PathLike[str] object beside str path

2 participants