Skip to content

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

Open
HaozheZhang6 wants to merge 1 commit into
CadQuery:masterfrom
HaozheZhang6:feat/pathlike-support
Open

Accept os.PathLike wherever a str path is accepted#2064
HaozheZhang6 wants to merge 1 commit 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.
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

1 participant