From 089bbcb939fe6e92515a6693a68107a2ae0127ce Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Sun, 27 Dec 2020 17:15:49 -0800 Subject: [PATCH] Remove unnecessary type override NamedTemporaryFileResult The object returned by NamedTemporaryFile delegates all functions calls to the underlying file so can avoid the type override by relying on IO methods. --- ...05edb4f-502c-4213-b8b8-c9173718e8ab.trivial.rst | 0 src/pip/_internal/operations/install/wheel.py | 4 ++-- src/pip/_internal/utils/filesystem.py | 14 ++++---------- 3 files changed, 6 insertions(+), 12 deletions(-) create mode 100644 news/205edb4f-502c-4213-b8b8-c9173718e8ab.trivial.rst diff --git a/news/205edb4f-502c-4213-b8b8-c9173718e8ab.trivial.rst b/news/205edb4f-502c-4213-b8b8-c9173718e8ab.trivial.rst new file mode 100644 index 000000000..e69de29bb diff --git a/src/pip/_internal/operations/install/wheel.py b/src/pip/_internal/operations/install/wheel.py index b6da06f55..37dcb618c 100644 --- a/src/pip/_internal/operations/install/wheel.py +++ b/src/pip/_internal/operations/install/wheel.py @@ -46,6 +46,7 @@ else: from typing import ( IO, Any, + BinaryIO, Callable, Dict, Iterable, @@ -65,7 +66,6 @@ else: from pip._vendor.pkg_resources import Distribution from pip._internal.models.scheme import Scheme - from pip._internal.utils.filesystem import NamedTemporaryFileResult RecordPath = NewType('RecordPath', str) InstalledCSVRow = Tuple[RecordPath, str, Union[int, str]] @@ -742,7 +742,7 @@ def _install_wheel( @contextlib.contextmanager def _generate_file(path, **kwargs): - # type: (str, **Any) -> Iterator[NamedTemporaryFileResult] + # type: (str, **Any) -> Iterator[BinaryIO] with adjacent_tmp_file(path, **kwargs) as f: yield f os.chmod(f.name, generated_file_mode) diff --git a/src/pip/_internal/utils/filesystem.py b/src/pip/_internal/utils/filesystem.py index dfa2802f7..1af8c10ea 100644 --- a/src/pip/_internal/utils/filesystem.py +++ b/src/pip/_internal/utils/filesystem.py @@ -19,12 +19,6 @@ from pip._internal.utils.typing import MYPY_CHECK_RUNNING, cast if MYPY_CHECK_RUNNING: from typing import Any, BinaryIO, Iterator, List, Union - class NamedTemporaryFileResult(BinaryIO): - @property - def file(self): - # type: () -> BinaryIO - pass - def check_path_owner(path): # type: (str) -> bool @@ -86,7 +80,7 @@ def is_socket(path): @contextmanager def adjacent_tmp_file(path, **kwargs): - # type: (str, **Any) -> Iterator[NamedTemporaryFileResult] + # type: (str, **Any) -> Iterator[BinaryIO] """Return a file-like object pointing to a tmp file next to path. The file is created securely and is ensured to be written to disk @@ -102,12 +96,12 @@ def adjacent_tmp_file(path, **kwargs): suffix='.tmp', **kwargs ) as f: - result = cast('NamedTemporaryFileResult', f) + result = cast('BinaryIO', f) try: yield result finally: - result.file.flush() - os.fsync(result.file.fileno()) + result.flush() + os.fsync(result.fileno()) _replace_retry = retry(stop_max_delay=1000, wait_fixed=250)