Applied `return_name` to `type_caster` for `std::fileystem::path`.

The `type_caster` previously named `os.PathLike` as both argument and
return type. This is inaccurate since it also takes `str` and `byte` as
argument and actually returns `Path`. This commit uses the new
`return_name` feature to define argument type as
`Union[os.PathLike, str, bytes]` and return type as `Path`.
An assert was added to the unit test for this.
This commit is contained in:
Tim Ohliger 2024-09-06 14:30:41 +02:00
parent dd829c5994
commit 7e7f9ce67b
2 changed files with 4 additions and 2 deletions

View File

@ -106,7 +106,8 @@ public:
return true;
}
PYBIND11_TYPE_CASTER(T, const_name("os.PathLike"));
PYBIND11_TYPE_CASTER(T, const_name("Union[os.PathLike, str, bytes]"));
static constexpr auto return_name = const_name("Path");
};
#endif // PYBIND11_HAS_FILESYSTEM || defined(PYBIND11_HAS_EXPERIMENTAL_FILESYSTEM)

View File

@ -245,7 +245,7 @@ def test_reference_sensitive_optional():
@pytest.mark.skipif(not hasattr(m, "has_filesystem"), reason="no <filesystem>")
def test_fs_path():
def test_fs_path(doc):
from pathlib import Path
class PseudoStrPath:
@ -261,6 +261,7 @@ def test_fs_path():
assert m.parent_path(b"foo/bar") == Path("foo")
assert m.parent_path(PseudoStrPath()) == Path("foo")
assert m.parent_path(PseudoBytesPath()) == Path("foo")
assert doc(m.parent_path) == "parent_path(arg0: Union[os.PathLike, str, bytes]) -> Path"
@pytest.mark.skipif(not hasattr(m, "load_variant"), reason="no <variant>")