pybind11 icon indicating copy to clipboard operation
pybind11 copied to clipboard

Implement noconvert for std::filesystem::path

Open gentlegiantJGC opened this issue 1 month ago • 2 comments

Description

The current cast implementation for std::filesystem::path is to always cast. If the argument is marked as noconvert() it will convert regardless. This change rejects the argument if convert is false and the argument is not an instance of pathlib.Path.

Suggested changelog entry:

  • Implement noconvert() for std::filesystem::path.

gentlegiantJGC avatar Nov 05 '25 16:11 gentlegiantJGC

We should be checking for __fspath__, not isinstance Path.

henryiii avatar Nov 10 '25 21:11 henryiii

We should be checking for __fspath__, not isinstance Path.

I am confused about the intended implementation of noconvert

Should a noconvert argument accept any object that can be losslessly converted?

If that is the case the current implementation is correct but the type hint is incorrect.

Currently the type hints look like this

m.def("path_convert", [](std::filesystem::path) { }, py::arg("path"));
m.def("path_noconvert", [](std::filesystem::path) { }, py::arg("path").noconvert());
def path_convert(path: os.PathLike | str | bytes) -> None: ...
def path_noconvert(path: pathlib.Path) -> None: ...

I was modifying the type caster to match the type hint thinking that the type hint was correct.

Should the noconvert type hint be os.PathLike?

If that is the case I think io_name is insufficient to represent this.

gentlegiantJGC avatar Nov 11 '25 09:11 gentlegiantJGC