Skip to content

Conversation

@gentlegiantJGC
Copy link
Contributor

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 and others added 4 commits November 5, 2025 16:43
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.
@henryiii
Copy link
Collaborator

We should be checking for __fspath__, not isinstance Path.

@gentlegiantJGC
Copy link
Contributor Author

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.

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.

2 participants