From 30592d13bfeb3c667dd98e9b5607f93bf1811c75 Mon Sep 17 00:00:00 2001 From: Janosh Riebesell Date: Mon, 29 Jul 2024 16:43:23 -0400 Subject: [PATCH 1/3] zpath convert pathlib.Path to safely use str.removesuffix --- src/monty/os/path.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/monty/os/path.py b/src/monty/os/path.py index e0b452441..fa293a90f 100644 --- a/src/monty/os/path.py +++ b/src/monty/os/path.py @@ -5,6 +5,7 @@ from __future__ import annotations import os +from pathlib import Path from typing import TYPE_CHECKING from monty.fnmatch import WildCard @@ -14,7 +15,7 @@ from typing import Callable, Literal, Optional, Union -def zpath(filename: str) -> str: +def zpath(filename: str | Path) -> str: """ Returns an existing (zipped or unzipped) file path given the unzipped version. If no path exists, returns the filename unmodified. @@ -23,10 +24,10 @@ def zpath(filename: str) -> str: filename: filename without zip extension Returns: - filename with a zip extension (unless an unzipped version - exists). If filename is not found, the same filename is returned - unchanged. + str: filename with a zip extension (unless an unzipped version exists). + If filename is not found, the same filename is returned unchanged. """ + filename = str(filename) # ensure we work with strings exts = ("", ".gz", ".GZ", ".bz2", ".BZ2", ".z", ".Z") for ext in exts: filename = filename.removesuffix(ext) From ecce7ec5f54e5f0c2a33bbbf811f8080785fe1a7 Mon Sep 17 00:00:00 2001 From: Janosh Riebesell Date: Mon, 29 Jul 2024 16:55:34 -0400 Subject: [PATCH 2/3] better zpath test coverage --- tests/test_os.py | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/tests/test_os.py b/tests/test_os.py index c6ca8d7ca..c4119c768 100644 --- a/tests/test_os.py +++ b/tests/test_os.py @@ -4,6 +4,7 @@ from pathlib import Path import pytest + from monty.os import cd, makedirs_p from monty.os.path import find_exts, zpath @@ -12,10 +13,11 @@ class TestPath: - def test_zpath(self, tmp_path: Path): + def test_zpath_str(self, tmp_path: Path): tmp_gz = tmp_path / "tmp.gz" tmp_gz.touch() ret_path = zpath(str(tmp_gz)) + assert isinstance(ret_path, str) assert ret_path == str(tmp_gz) tmp_not_bz2 = tmp_path / "tmp_not_bz2" @@ -23,6 +25,39 @@ def test_zpath(self, tmp_path: Path): ret_path = zpath(f"{tmp_not_bz2}.bz2") assert ret_path == str(tmp_not_bz2) + assert isinstance(ret_path, str) + + def test_zpath_path(self, tmp_path: Path): + # Test with Path input + tmp_gz = tmp_path / "tmp.gz" + tmp_gz.touch() + ret_path = zpath(tmp_gz) + assert ret_path == str(tmp_gz) + assert isinstance(ret_path, str) + + def test_zpath_multiple_extensions(self, tmp_path: Path): + exts = ["", ".gz", ".GZ", ".bz2", ".BZ2", ".z", ".Z"] + for ext in exts: + tmp_file = tmp_path / f"tmp{ext}" + # create files with all supported compression extensions + tmp_file.touch() + + # zpath should return the file without compression extension + ret_path = zpath(tmp_path / "tmp") + assert ret_path == str(tmp_path / "tmp") + assert isinstance(ret_path, str) + + (tmp_path / "tmp").unlink() # Remove the uncompressed file + ret_path = zpath(tmp_path / "tmp") + assert ret_path == str(tmp_path / "tmp.gz") # should find .gz first now + + def test_zpath_nonexistent_file(self, tmp_path: Path): + # should return path as is for non-existent file + nonexistent = tmp_path / "nonexistent.txt" + ret_path = zpath(nonexistent) + assert ret_path == str(nonexistent) + ret_path = zpath(f"{nonexistent}.bz2") + assert ret_path == str(nonexistent) def test_find_exts(self): assert len(find_exts(MODULE_DIR, "py")) >= 18 From 8163a11c72da37c7123cf699558ea835e66c1696 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 29 Jul 2024 20:58:02 +0000 Subject: [PATCH 3/3] pre-commit auto-fixes --- tests/test_os.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_os.py b/tests/test_os.py index c4119c768..c38510c01 100644 --- a/tests/test_os.py +++ b/tests/test_os.py @@ -4,7 +4,6 @@ from pathlib import Path import pytest - from monty.os import cd, makedirs_p from monty.os.path import find_exts, zpath