From 0c02554c8858dd0210e68ccfcefca26b0abf849c Mon Sep 17 00:00:00 2001 From: Leo Date: Fri, 30 Oct 2026 01:08:42 -0700 Subject: [PATCH] Add pathlib-compatible Path.resolve --- newsfragments/214.feature.rst | 1 + path/__init__.py | 9 +++++++ tests/test_path.py | 49 +++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 newsfragments/214.feature.rst diff --git a/newsfragments/214.feature.rst b/newsfragments/214.feature.rst new file mode 100644 index 0000000..dfdbf75 --- /dev/null +++ b/newsfragments/214.feature.rst @@ -0,0 +1 @@ +Add ``Path.resolve()`` with pathlib-compatible symbolic-link resolution and a strict mode. diff --git a/path/__init__.py b/path/__init__.py index 731e88e..1f7c5b9 100644 --- a/path/__init__.py +++ b/path/__init__.py @@ -279,6 +279,15 @@ def realpath(self) -> Self: """.. seealso:: :func:`os.path.realpath`""" return self._next_class(self.module.realpath(self)) + def resolve(self, strict: bool = False) -> Self: + """Return an absolute path with symbolic links resolved. + + Uses the host filesystem, with the same behavior as + :meth:`pathlib.Path.resolve`. If *strict* is true, raise an error + when the path cannot be resolved completely. + """ + return self._next_class(pathlib.Path(self).resolve(strict=strict)) + def expanduser(self) -> Self: """.. seealso:: :func:`os.path.expanduser`""" return self._next_class(self.module.expanduser(self)) diff --git a/tests/test_path.py b/tests/test_path.py index 55d733c..bd5f07e 100644 --- a/tests/test_path.py +++ b/tests/test_path.py @@ -41,6 +41,55 @@ def os_choose(**choices): return choices[os.name] +class TestResolve: + def test_relative_path(self, tmp_path, monkeypatch): + (tmp_path / 'child').mkdir() + monkeypatch.chdir(tmp_path) + resolved = Path('child/..').resolve() + assert resolved == str(tmp_path) + assert type(resolved) is Path + + def test_missing_path_is_allowed_by_default(self, tmp_path): + target = Path(tmp_path / 'missing' / 'file.txt') + assert target.resolve() == target + + def test_strict_missing_path(self, tmp_path): + with pytest.raises(FileNotFoundError): + Path(tmp_path / 'missing').resolve(strict=True) + + def test_strict_existing_path(self, tmp_path): + target = tmp_path / 'file.txt' + target.write_text('content', encoding='utf-8') + assert Path(target).resolve(True) == str(target) + + def test_preserves_subclass(self, tmp_path): + class CustomPath(Path): + pass + + assert type(CustomPath(tmp_path).resolve()) is CustomPath + + def test_tempdir_result_is_a_plain_path(self, tmp_path): + temp = TempDir(dir=tmp_path) + try: + resolved = temp.resolve() + assert type(resolved) is Path + assert resolved == temp + finally: + temp.rmdir() + + def test_symlink(self, tmp_path): + target = tmp_path / 'target' + target.mkdir() + link = tmp_path / 'link' + try: + link.symlink_to(target, target_is_directory=True) + except OSError as error: + if getattr(error, 'winerror', None) == 1314: + pytest.skip('Creating symlinks requires Windows permission') + raise + assert Path(link).resolve() == str(target) + + class TestBasics: def test_relpath(self): root = Path(os_choose(nt='C:\\', posix='/'))