From 15d6eda00bb9f813b790e4697b42fd8ac1cf2ecc Mon Sep 17 00:00:00 2001 From: Ayham970 <173636158+Ayham970@users.noreply.github.com> Date: Tue, 24 Nov 2026 09:06:17 +0400 Subject: [PATCH] Add pathlib-compatible junction detection Assisted-by: OpenAI Codex --- newsfragments/+is-junction.feature.rst | 1 + path/__init__.py | 12 +++++++++++ tests/test_path.py | 29 ++++++++++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 newsfragments/+is-junction.feature.rst diff --git a/newsfragments/+is-junction.feature.rst b/newsfragments/+is-junction.feature.rst new file mode 100644 index 0000000..021b573 --- /dev/null +++ b/newsfragments/+is-junction.feature.rst @@ -0,0 +1 @@ +Add ``Path.is_junction()`` on Python 3.12 and later for pathlib compatibility. diff --git a/path/__init__.py b/path/__init__.py index 731e88e..602f947 100644 --- a/path/__init__.py +++ b/path/__init__.py @@ -1027,6 +1027,18 @@ def islink(self) -> bool: """.. seealso:: :func:`os.path.islink`""" return self.module.islink(self) + if sys.version_info >= (3, 12): + + def is_junction(self) -> bool: + """Return whether this path is a Windows junction. + + Available on Python 3.12 and later. Returns False on + platforms that do not support junctions. + + .. seealso:: :func:`os.path.isjunction` + """ + return self.module.isjunction(self) + def ismount(self) -> bool: """ >>> Path('.').ismount() diff --git a/tests/test_path.py b/tests/test_path.py index 55d733c..56eb95e 100644 --- a/tests/test_path.py +++ b/tests/test_path.py @@ -310,6 +310,35 @@ def test_group(self, tmpdir): assert isinstance(file.group(), str) +@pytest.mark.skipif( + sys.version_info < (3, 12), reason='junction API requires Python 3.12' +) +class TestJunctions: + def test_non_junctions(self, tmp_path): + root = Path(tmp_path) + file = (root / 'file').touch() + assert not root.is_junction() + assert not file.is_junction() + assert not (root / 'missing').is_junction() + assert not Path('invalid\0path').is_junction() + + @pytest.mark.skipif(sys.platform != 'win32', reason='Windows junctions') + def test_junction(self, tmp_path): + winapi = pytest.importorskip('_winapi') + root = Path(tmp_path) + target = (root / 'target').mkdir() + junction = root / 'junction' + winapi.CreateJunction(str(target), str(junction)) + try: + assert junction.is_junction() + assert not target.is_junction() + target.rmdir() + assert junction.is_junction() + assert not junction.exists() + finally: + junction.rmdir() + + class TestReadWriteText: def test_read_write(self, tmpdir): file = path.Path(tmpdir) / 'filename'