From 73ae2ee37fc5c011ffd17996f47964a851cab48f Mon Sep 17 00:00:00 2001 From: Bartek Sokorski Date: Sat, 16 Sep 2023 19:10:06 +0200 Subject: [PATCH] Use pathlib instead of os.path module --- pyproject.toml | 2 +- src/poetry/core/masonry/api.py | 8 +- src/poetry/core/masonry/builders/sdist.py | 12 +-- src/poetry/core/masonry/builders/wheel.py | 9 +- src/poetry/core/packages/dependency.py | 4 +- src/poetry/core/packages/utils/link.py | 3 +- src/poetry/core/packages/utils/utils.py | 13 ++- src/poetry/core/utils/helpers.py | 12 +-- tests/integration/test_pep517_backend.py | 2 +- tests/masonry/builders/test_complete.py | 3 +- tests/masonry/builders/test_sdist.py | 2 +- tests/masonry/test_api.py | 102 ++++++++++------------ tests/packages/utils/test_utils_urls.py | 4 +- tests/testutils.py | 5 +- tests/utils/test_helpers.py | 17 ++-- 15 files changed, 93 insertions(+), 105 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 04c1b78c3..2112f50a8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -72,10 +72,10 @@ extend-select = [ "UP", # pyupgrade "I", # isort "PGH", # pygrep + "PTH" # flake8-use-pathlib ] ignore = [ "B904", # use 'raise ... from err' - "B905", # use explicit 'strict=' parameter with 'zip()' "N818" # Exception name should be named with an Error suffix ] diff --git a/src/poetry/core/masonry/api.py b/src/poetry/core/masonry/api.py index 42ece0b96..a712baf91 100644 --- a/src/poetry/core/masonry/api.py +++ b/src/poetry/core/masonry/api.py @@ -39,7 +39,7 @@ def get_requires_for_build_wheel( def prepare_metadata_for_build_wheel( metadata_directory: str, config_settings: dict[str, Any] | None = None ) -> str: - poetry = Factory().create_poetry(Path(".").resolve(), with_groups=False) + poetry = Factory().create_poetry(Path().resolve(), with_groups=False) builder = WheelBuilder(poetry) metadata_path = Path(metadata_directory) dist_info = builder.prepare_metadata(metadata_path) @@ -52,7 +52,7 @@ def build_wheel( metadata_directory: str | None = None, ) -> str: """Builds a wheel, places it in wheel_directory""" - poetry = Factory().create_poetry(Path(".").resolve(), with_groups=False) + poetry = Factory().create_poetry(Path().resolve(), with_groups=False) metadata_path = None if metadata_directory is None else Path(metadata_directory) return WheelBuilder.make_in( @@ -64,7 +64,7 @@ def build_sdist( sdist_directory: str, config_settings: dict[str, Any] | None = None ) -> str: """Builds an sdist, places it in sdist_directory""" - poetry = Factory().create_poetry(Path(".").resolve(), with_groups=False) + poetry = Factory().create_poetry(Path().resolve(), with_groups=False) path = SdistBuilder(poetry).build(Path(sdist_directory)) @@ -76,7 +76,7 @@ def build_editable( config_settings: dict[str, Any] | None = None, metadata_directory: str | None = None, ) -> str: - poetry = Factory().create_poetry(Path(".").resolve(), with_groups=False) + poetry = Factory().create_poetry(Path().resolve(), with_groups=False) metadata_path = None if metadata_directory is None else Path(metadata_directory) return WheelBuilder.make_in( diff --git a/src/poetry/core/masonry/builders/sdist.py b/src/poetry/core/masonry/builders/sdist.py index ae0222cf0..6133759f6 100644 --- a/src/poetry/core/masonry/builders/sdist.py +++ b/src/poetry/core/masonry/builders/sdist.py @@ -142,7 +142,7 @@ def build_setup(self) -> bytes: pkg_root = os.path.relpath(pkg_dir, str(self._path)) if "" in package_dir: package_dir.update( - (p, os.path.join(pkg_root, p.replace(".", "/"))) + (p, (Path(pkg_root) / p.replace(".", "/")).as_posix()) for p in _packages ) else: @@ -261,18 +261,18 @@ def find_packages( subpkg_paths = set() def find_nearest_pkg(rel_path: str) -> tuple[str, str]: - parts = rel_path.split(os.sep) + parts = Path(rel_path).parts for i in reversed(range(1, len(parts))): ancestor = "/".join(parts[:i]) if ancestor in subpkg_paths: - pkg = ".".join([pkg_name] + parts[:i]) + pkg = ".".join([pkg_name, *parts[:i]]) return pkg, "/".join(parts[i:]) # Relative to the top-level package return pkg_name, Path(rel_path).as_posix() - for path, _dirnames, filenames in os.walk(str(base), topdown=True): - if os.path.basename(path) == "__pycache__": + for path, _dirnames, filenames in os.walk(base, topdown=True): + if Path(path).name == "__pycache__": continue from_top_level = os.path.relpath(path, base) @@ -288,7 +288,7 @@ def find_nearest_pkg(rel_path: str) -> tuple[str, str]: ) if is_subpkg: subpkg_paths.add(from_top_level) - parts = from_top_level.split(os.sep) + parts = Path(from_top_level).parts packages.append(".".join([pkg_name, *parts])) else: pkg, from_nearest_pkg = find_nearest_pkg(from_top_level) diff --git a/src/poetry/core/masonry/builders/wheel.py b/src/poetry/core/masonry/builders/wheel.py index ab8bd5f92..7e20a162f 100644 --- a/src/poetry/core/masonry/builders/wheel.py +++ b/src/poetry/core/masonry/builders/wheel.py @@ -104,11 +104,12 @@ def build( if not target_dir.exists(): target_dir.mkdir() - fd, temp_path = tempfile.mkstemp(suffix=".whl") + fd, temp = tempfile.mkstemp(suffix=".whl") - st_mode = os.stat(temp_path).st_mode + temp_path = Path(temp) + st_mode = temp_path.stat().st_mode new_mode = normalize_file_permissions(st_mode) - os.chmod(temp_path, new_mode) + temp_path.chmod(new_mode) with os.fdopen(fd, "w+b") as fd_file, zipfile.ZipFile( fd_file, mode="w", compression=zipfile.ZIP_DEFLATED @@ -137,7 +138,7 @@ def build( wheel_path = target_dir / self.wheel_filename if wheel_path.exists(): wheel_path.unlink() - shutil.move(temp_path, str(wheel_path)) + shutil.move(str(temp_path), str(wheel_path)) logger.info(f"Built {self.wheel_filename}") return wheel_path diff --git a/src/poetry/core/packages/dependency.py b/src/poetry/core/packages/dependency.py index f5f559a85..ac8d6c8b6 100644 --- a/src/poetry/core/packages/dependency.py +++ b/src/poetry/core/packages/dependency.py @@ -401,9 +401,9 @@ def create_from_pep_508( elif req.url: link = Link(req.url) else: - path_str = os.path.normpath(os.path.abspath(name)) + path_str = os.path.normpath(os.path.abspath(name)) # noqa: PTH100 p, extras = strip_extras(path_str) - if os.path.isdir(p) and (os.path.sep in name or name.startswith(".")): + if p.is_dir() and (os.path.sep in name or name.startswith(".")): if not is_python_project(Path(name)): raise ValueError( f"Directory {name!r} is not installable. File 'setup.[py|cfg]' " diff --git a/src/poetry/core/packages/utils/link.py b/src/poetry/core/packages/utils/link.py index e1f2d3823..a222cdaf2 100644 --- a/src/poetry/core/packages/utils/link.py +++ b/src/poetry/core/packages/utils/link.py @@ -6,6 +6,7 @@ import warnings from functools import cached_property +from pathlib import Path from typing import TYPE_CHECKING from poetry.core.packages.utils.utils import path_to_url @@ -133,7 +134,7 @@ def path(self) -> str: return urlparse.unquote(urlparse.urlsplit(self.url)[2]) def splitext(self) -> tuple[str, str]: - return splitext(posixpath.basename(self.path.rstrip("/"))) + return splitext(Path(posixpath.basename(self.path.rstrip("/")))) @cached_property def ext(self) -> str: diff --git a/src/poetry/core/packages/utils/utils.py b/src/poetry/core/packages/utils/utils.py index 97229517b..bf047c371 100644 --- a/src/poetry/core/packages/utils/utils.py +++ b/src/poetry/core/packages/utils/utils.py @@ -1,7 +1,6 @@ from __future__ import annotations import functools -import posixpath import re import sys @@ -107,7 +106,7 @@ def is_url(name: str) -> bool: ] -def strip_extras(path: str) -> tuple[str, str | None]: +def strip_extras(path: str) -> tuple[Path, str | None]: m = re.match(r"^(.+)(\[[^\]]+\])$", path) extras = None if m: @@ -116,7 +115,7 @@ def strip_extras(path: str) -> tuple[str, str | None]: else: path_no_extras = path - return path_no_extras, extras + return Path(path_no_extras), extras @functools.lru_cache(maxsize=None) @@ -137,7 +136,7 @@ def is_python_project(path: Path) -> bool: return supports_pep517 or supports_poetry -def is_archive_file(name: str) -> bool: +def is_archive_file(name: Path) -> bool: """Return True if `name` is a considered as an archive file.""" ext = splitext(name)[1].lower() if ext in ARCHIVE_EXTENSIONS: @@ -145,11 +144,11 @@ def is_archive_file(name: str) -> bool: return False -def splitext(path: str) -> tuple[str, str]: +def splitext(path: Path) -> tuple[str, str]: """Like os.path.splitext, but take off .tar too""" - base, ext = posixpath.splitext(path) + base, ext = path.stem, path.suffix if base.lower().endswith(".tar"): - ext = base[-4:] + ext + ext = f"{base[-4:]}{ext}" base = base[:-4] return base, ext diff --git a/src/poetry/core/utils/helpers.py b/src/poetry/core/utils/helpers.py index 887bb2532..0accc739e 100644 --- a/src/poetry/core/utils/helpers.py +++ b/src/poetry/core/utils/helpers.py @@ -1,6 +1,5 @@ from __future__ import annotations -import os import shutil import stat import sys @@ -41,16 +40,16 @@ def normalize_version(version: str) -> str: @contextmanager -def temporary_directory(*args: Any, **kwargs: Any) -> Iterator[str]: +def temporary_directory(*args: Any, **kwargs: Any) -> Iterator[Path]: if sys.version_info >= (3, 10): # mypy reports an error if ignore_cleanup_errors is # specified literally in the call kwargs["ignore_cleanup_errors"] = True with tempfile.TemporaryDirectory(*args, **kwargs) as name: - yield name + yield Path(name) else: name = tempfile.mkdtemp(*args, **kwargs) - yield name + yield Path(name) robust_rmtree(name) @@ -92,10 +91,11 @@ def parse_requires(requires: str) -> list[str]: def _on_rm_error(func: Any, path: str | Path, exc_info: Any) -> None: - if not os.path.exists(path): + path = Path(path) + if not path.exists(): return - os.chmod(path, stat.S_IWRITE) + path.chmod(stat.S_IWRITE) func(path) diff --git a/tests/integration/test_pep517_backend.py b/tests/integration/test_pep517_backend.py index 031214821..5dacbbded 100644 --- a/tests/integration/test_pep517_backend.py +++ b/tests/integration/test_pep517_backend.py @@ -38,7 +38,7 @@ def test_pip_install( # Append dynamic `build-system` section to `pyproject.toml` in the temporary # project directory. - with open(temp_pep_517_backend_path / "pyproject.toml", "a") as f: + with (temp_pep_517_backend_path / "pyproject.toml").open(mode="a") as f: f.write( BUILD_SYSTEM_TEMPLATE.format(project_path=project_source_root.as_posix()) ) diff --git a/tests/masonry/builders/test_complete.py b/tests/masonry/builders/test_complete.py index 44cbfbd72..8b8b82236 100644 --- a/tests/masonry/builders/test_complete.py +++ b/tests/masonry/builders/test_complete.py @@ -1,7 +1,6 @@ from __future__ import annotations import csv -import os import platform import re import shutil @@ -117,7 +116,7 @@ def test_complete(no_vcs: bool) -> None: assert whl.exists() if sys.platform != "win32": - assert (os.stat(str(whl)).st_mode & 0o777) == 0o644 + assert (whl.stat().st_mode & 0o777) == 0o644 expected_name_list = [ "my_package/__init__.py", diff --git a/tests/masonry/builders/test_sdist.py b/tests/masonry/builders/test_sdist.py index 7a76bc653..51cefcab3 100644 --- a/tests/masonry/builders/test_sdist.py +++ b/tests/masonry/builders/test_sdist.py @@ -309,7 +309,7 @@ def test_setup_py_context() -> None: assert setup.exists() assert project_setup_py == setup - with open(str(setup), "rb") as f: + with setup.open(mode="rb") as f: # we convert to string and replace line endings here for compatibility data = f.read().decode().replace("\r\n", "\n") assert data == builder.build_setup().decode() diff --git a/tests/masonry/test_api.py b/tests/masonry/test_api.py index c0d124df3..ac86de4a4 100644 --- a/tests/masonry/test_api.py +++ b/tests/masonry/test_api.py @@ -21,7 +21,7 @@ @contextmanager def cwd(directory: str | Path) -> Iterator[None]: - prev = os.getcwd() + prev = Path.cwd() os.chdir(str(directory)) try: yield @@ -29,55 +29,51 @@ def cwd(directory: str | Path) -> Iterator[None]: os.chdir(prev) -fixtures = os.path.join(os.path.dirname(__file__), "builders", "fixtures") +fixtures = Path(__file__).parent / "builders" / "fixtures" def test_get_requires_for_build_wheel() -> None: expected: list[str] = [] - with cwd(os.path.join(fixtures, "complete")): + with cwd(fixtures / "complete"): assert api.get_requires_for_build_wheel() == expected def test_get_requires_for_build_sdist() -> None: expected: list[str] = [] - with cwd(os.path.join(fixtures, "complete")): + with cwd(fixtures / "complete"): assert api.get_requires_for_build_sdist() == expected def test_build_wheel() -> None: - with temporary_directory() as tmp_dir, cwd(os.path.join(fixtures, "complete")): - filename = api.build_wheel(tmp_dir) + with temporary_directory() as tmp_dir, cwd(fixtures / "complete"): + filename = api.build_wheel(str(tmp_dir)) validate_wheel_contents( name="my_package", version="1.2.3", - path=str(os.path.join(tmp_dir, filename)), + path=tmp_dir / filename, files=["entry_points.txt"], ) def test_build_wheel_with_include() -> None: - with temporary_directory() as tmp_dir, cwd(os.path.join(fixtures, "with-include")): - filename = api.build_wheel(tmp_dir) + with temporary_directory() as tmp_dir, cwd(fixtures / "with-include"): + filename = api.build_wheel(str(tmp_dir)) validate_wheel_contents( name="with_include", version="1.2.3", - path=str(os.path.join(tmp_dir, filename)), + path=tmp_dir / filename, files=["entry_points.txt"], ) def test_build_wheel_with_bad_path_dev_dep_succeeds() -> None: - with temporary_directory() as tmp_dir, cwd( - os.path.join(fixtures, "with_bad_path_dev_dep") - ): - api.build_wheel(tmp_dir) + with temporary_directory() as tmp_dir, cwd(fixtures / "with_bad_path_dev_dep"): + api.build_wheel(str(tmp_dir)) def test_build_wheel_with_bad_path_dep_succeeds(caplog: LogCaptureFixture) -> None: - with temporary_directory() as tmp_dir, cwd( - os.path.join(fixtures, "with_bad_path_dep") - ): - api.build_wheel(tmp_dir) + with temporary_directory() as tmp_dir, cwd(fixtures / "with_bad_path_dep"): + api.build_wheel(str(tmp_dir)) assert len(caplog.records) == 1 record = caplog.records[0] assert record.levelname == "WARNING" @@ -85,47 +81,43 @@ def test_build_wheel_with_bad_path_dep_succeeds(caplog: LogCaptureFixture) -> No def test_build_wheel_extended() -> None: - with temporary_directory() as tmp_dir, cwd(os.path.join(fixtures, "extended")): - filename = api.build_wheel(tmp_dir) + with temporary_directory() as tmp_dir, cwd(fixtures / "extended"): + filename = api.build_wheel(str(tmp_dir)) whl = Path(tmp_dir) / filename assert whl.exists() - validate_wheel_contents(name="extended", version="0.1", path=whl.as_posix()) + validate_wheel_contents(name="extended", version="0.1", path=whl) def test_build_sdist() -> None: - with temporary_directory() as tmp_dir, cwd(os.path.join(fixtures, "complete")): - filename = api.build_sdist(tmp_dir) + with temporary_directory() as tmp_dir, cwd(fixtures / "complete"): + filename = api.build_sdist(str(tmp_dir)) validate_sdist_contents( name="my-package", version="1.2.3", - path=str(os.path.join(tmp_dir, filename)), + path=tmp_dir / filename, files=["LICENSE"], ) def test_build_sdist_with_include() -> None: - with temporary_directory() as tmp_dir, cwd(os.path.join(fixtures, "with-include")): - filename = api.build_sdist(tmp_dir) + with temporary_directory() as tmp_dir, cwd(fixtures / "with-include"): + filename = api.build_sdist(str(tmp_dir)) validate_sdist_contents( name="with-include", version="1.2.3", - path=str(os.path.join(tmp_dir, filename)), + path=tmp_dir / filename, files=["LICENSE"], ) def test_build_sdist_with_bad_path_dev_dep_succeeds() -> None: - with temporary_directory() as tmp_dir, cwd( - os.path.join(fixtures, "with_bad_path_dev_dep") - ): - api.build_sdist(tmp_dir) + with temporary_directory() as tmp_dir, cwd(fixtures / "with_bad_path_dev_dep"): + api.build_sdist(str(tmp_dir)) def test_build_sdist_with_bad_path_dep_succeeds(caplog: LogCaptureFixture) -> None: - with temporary_directory() as tmp_dir, cwd( - os.path.join(fixtures, "with_bad_path_dep") - ): - api.build_sdist(tmp_dir) + with temporary_directory() as tmp_dir, cwd(fixtures / "with_bad_path_dep"): + api.build_sdist(str(tmp_dir)) assert len(caplog.records) == 1 record = caplog.records[0] assert record.levelname == "WARNING" @@ -183,8 +175,8 @@ def test_prepare_metadata_for_build_wheel() -> None: ========== """ - with temporary_directory() as tmp_dir, cwd(os.path.join(fixtures, "complete")): - dirname = api.prepare_metadata_for_build_wheel(tmp_dir) + with temporary_directory() as tmp_dir, cwd(fixtures / "complete"): + dirname = api.prepare_metadata_for_build_wheel(str(tmp_dir)) assert dirname == "my_package-1.2.3.dist-info" @@ -205,19 +197,15 @@ def test_prepare_metadata_for_build_wheel() -> None: def test_prepare_metadata_for_build_wheel_with_bad_path_dev_dep_succeeds() -> None: - with temporary_directory() as tmp_dir, cwd( - os.path.join(fixtures, "with_bad_path_dev_dep") - ): - api.prepare_metadata_for_build_wheel(tmp_dir) + with temporary_directory() as tmp_dir, cwd(fixtures / "with_bad_path_dev_dep"): + api.prepare_metadata_for_build_wheel(str(tmp_dir)) def test_prepare_metadata_for_build_wheel_with_bad_path_dep_succeeds( caplog: LogCaptureFixture, ) -> None: - with temporary_directory() as tmp_dir, cwd( - os.path.join(fixtures, "with_bad_path_dep") - ): - api.prepare_metadata_for_build_wheel(tmp_dir) + with temporary_directory() as tmp_dir, cwd(fixtures / "with_bad_path_dep"): + api.prepare_metadata_for_build_wheel(str(tmp_dir)) assert len(caplog.records) == 1 record = caplog.records[0] assert record.levelname == "WARNING" @@ -225,16 +213,16 @@ def test_prepare_metadata_for_build_wheel_with_bad_path_dep_succeeds( def test_build_editable_wheel() -> None: - pkg_dir = Path(fixtures) / "complete" + pkg_dir = fixtures / "complete" with temporary_directory() as tmp_dir, cwd(pkg_dir): - filename = api.build_editable(tmp_dir) + filename = api.build_editable(str(tmp_dir)) wheel_pth = Path(tmp_dir) / filename validate_wheel_contents( name="my_package", version="1.2.3", - path=str(wheel_pth), + path=wheel_pth, ) with zipfile.ZipFile(wheel_pth) as z: @@ -245,23 +233,23 @@ def test_build_editable_wheel() -> None: def test_build_wheel_with_metadata_directory() -> None: - pkg_dir = Path(fixtures) / "complete" + pkg_dir = fixtures / "complete" with temporary_directory() as metadata_tmp_dir, cwd(pkg_dir): - metadata_directory = api.prepare_metadata_for_build_wheel(metadata_tmp_dir) + metadata_directory = api.prepare_metadata_for_build_wheel(str(metadata_tmp_dir)) with temporary_directory() as wheel_tmp_dir: dist_info_path = Path(metadata_tmp_dir) / metadata_directory (dist_info_path / "CUSTOM").touch() filename = api.build_wheel( - wheel_tmp_dir, metadata_directory=str(dist_info_path) + str(wheel_tmp_dir), metadata_directory=str(dist_info_path) ) wheel_pth = Path(wheel_tmp_dir) / filename validate_wheel_contents( name="my_package", version="1.2.3", - path=str(wheel_pth), + path=wheel_pth, files=["entry_points.txt"], ) @@ -272,23 +260,25 @@ def test_build_wheel_with_metadata_directory() -> None: def test_build_editable_wheel_with_metadata_directory() -> None: - pkg_dir = Path(fixtures) / "complete" + pkg_dir = fixtures / "complete" with temporary_directory() as metadata_tmp_dir, cwd(pkg_dir): - metadata_directory = api.prepare_metadata_for_build_editable(metadata_tmp_dir) + metadata_directory = api.prepare_metadata_for_build_editable( + str(metadata_tmp_dir) + ) with temporary_directory() as wheel_tmp_dir: dist_info_path = Path(metadata_tmp_dir) / metadata_directory (dist_info_path / "CUSTOM").touch() filename = api.build_editable( - wheel_tmp_dir, metadata_directory=str(dist_info_path) + str(wheel_tmp_dir), metadata_directory=str(dist_info_path) ) wheel_pth = Path(wheel_tmp_dir) / filename validate_wheel_contents( name="my_package", version="1.2.3", - path=str(wheel_pth), + path=wheel_pth, files=["entry_points.txt"], ) diff --git a/tests/packages/utils/test_utils_urls.py b/tests/packages/utils/test_utils_urls.py index 6480c78a8..0f2814ed1 100644 --- a/tests/packages/utils/test_utils_urls.py +++ b/tests/packages/utils/test_utils_urls.py @@ -16,7 +16,7 @@ @pytest.mark.skipif("sys.platform == 'win32'") def test_path_to_url_unix() -> None: assert path_to_url("/tmp/file") == "file:///tmp/file" - path = Path(".") / "file" + path = Path() / "file" assert path_to_url("file") == "file://" + path.absolute().as_posix() @@ -25,7 +25,7 @@ def test_path_to_url_win() -> None: assert path_to_url("c:/tmp/file") == "file:///c:/tmp/file" assert path_to_url("c:\\tmp\\file") == "file:///c:/tmp/file" assert path_to_url(r"\\unc\as\path") == "file://unc/as/path" - path = Path(".") / "file" + path = Path() / "file" assert path_to_url("file") == "file:///" + path.absolute().as_posix() diff --git a/tests/testutils.py b/tests/testutils.py index 93f3b6e12..02effca1e 100644 --- a/tests/testutils.py +++ b/tests/testutils.py @@ -65,20 +65,19 @@ def subprocess_run(*args: str, **kwargs: Any) -> subprocess.CompletedProcess[str def validate_wheel_contents( - name: str, version: str, path: str, files: list[str] | None = None + name: str, version: str, path: Path, files: list[str] | None = None ) -> None: dist_info = f"{name}-{version}.dist-info" files = files or [] with zipfile.ZipFile(path) as z: namelist = z.namelist() - # we use concatenation here for PY2 compat for filename in ["WHEEL", "METADATA", "RECORD", *files]: assert f"{dist_info}/{filename}" in namelist def validate_sdist_contents( - name: str, version: str, path: str, files: list[str] + name: str, version: str, path: Path, files: list[str] ) -> None: escaped_name = name.replace("-", "_") with tarfile.open(path) as tar: diff --git a/tests/utils/test_helpers.py b/tests/utils/test_helpers.py index f0845bd44..fef39de68 100644 --- a/tests/utils/test_helpers.py +++ b/tests/utils/test_helpers.py @@ -1,6 +1,5 @@ from __future__ import annotations -import os import sys import tempfile @@ -102,13 +101,13 @@ def test_utils_helpers_combine_unicode() -> None: def test_utils_helpers_temporary_directory_readonly_file() -> None: with temporary_directory() as temp_dir: - readonly_filename = os.path.join(temp_dir, "file.txt") - with open(readonly_filename, "w+") as readonly_file: + readonly_filename = temp_dir / "file.txt" + with readonly_filename.open(mode="w+") as readonly_file: readonly_file.write("Poetry rocks!") - os.chmod(str(readonly_filename), S_IREAD) + readonly_filename.chmod(S_IREAD) - assert not os.path.exists(temp_dir) - assert not os.path.exists(readonly_filename) + assert not temp_dir.exists() + assert not readonly_filename.exists() @pytest.mark.parametrize( @@ -135,7 +134,7 @@ def test_temporary_directory_python_3_10_or_newer(mocker: MockerFixture) -> None mocker.patch.object(sys, "version_info", (3, 10)) with temporary_directory() as tmp: - assert tmp + assert str(tmp) assert not mocked_rmtree.called assert not mocked_mkdtemp.called @@ -147,11 +146,11 @@ def test_temporary_directory_python_3_9_or_older(mocker: MockerFixture) -> None: mocked_temp_dir = mocker.patch("tempfile.TemporaryDirectory") mocked_mkdtemp = mocker.patch("tempfile.mkdtemp") - mocked_mkdtemp.return_value = "hello from test" + mocked_mkdtemp.return_value = Path("hello from test") mocker.patch.object(sys, "version_info", (3, 9)) with temporary_directory() as tmp: - assert tmp == "hello from test" + assert tmp == Path("hello from test") assert mocked_rmtree.called assert mocked_mkdtemp.called