diff --git a/src/wrapt/proxies.py b/src/wrapt/proxies.py index 2c0c6da2..bd75ba44 100644 --- a/src/wrapt/proxies.py +++ b/src/wrapt/proxies.py @@ -270,6 +270,9 @@ def __new__(cls, callback=None, *args, interface=..., **kwargs): if "__anext__" in interface_attrs and "__anext__" not in class_attrs: namespace["__anext__"] = __wrapper_anext__ + if "__fspath__" in interface_attrs and "__fspath__" not in class_attrs: + namespace["__fspath__"] = __wrapper_fspath__ + if ( "__length_hint__" in interface_attrs and "__length_hint__" not in class_attrs diff --git a/tests/core/test_lazy_object_proxy.py b/tests/core/test_lazy_object_proxy.py index 4597f812..ba0937a8 100644 --- a/tests/core/test_lazy_object_proxy.py +++ b/tests/core/test_lazy_object_proxy.py @@ -1,11 +1,30 @@ +import os import sys import unittest +from pathlib import Path import wrapt class TestLazyObjectProxy(unittest.TestCase): + def test_pathlike_interface_is_lazy(self): + for interface in (os.PathLike, Path): + with self.subTest(interface=interface): + status = {"created": 0} + + def factory(): + status["created"] += 1 + return Path("example.txt") + + proxy = wrapt.LazyObjectProxy(factory, interface=interface) + self.assertEqual(status["created"], 0) + self.assertTrue(hasattr(type(proxy), "__fspath__")) + self.assertEqual(os.fspath(proxy), "example.txt") + self.assertEqual(status["created"], 1) + self.assertEqual(os.fspath(proxy), "example.txt") + self.assertEqual(status["created"], 1) + def test_lazy_object_proxy(self): status = {"created": 0}