From 91b820168f17084e82b2e13a1e4f03aeab03eeac Mon Sep 17 00:00:00 2001 From: The mediapy Authors Date: Tue, 7 Nov 2023 11:11:07 -0800 Subject: [PATCH] Resolve unsoundness caught by pytype --strict-none-binding. PiperOrigin-RevId: 580240467 --- mediapy/__init__.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/mediapy/__init__.py b/mediapy/__init__.py index 6915e4a..1ca828b 100644 --- a/mediapy/__init__.py +++ b/mediapy/__init__.py @@ -1388,8 +1388,8 @@ def read(self) -> _NDArray | None: array with 3 color channels, except for format 'gray' which is 2D. """ assert self._proc, 'Error: reading from an already closed context.' - assert self._proc.stdout - data = self._proc.stdout.read(self._num_bytes_per_image) + assert (stdout := self._proc.stdout) is not None + data = stdout.read(self._num_bytes_per_image) if not data: # Due to either end-of-file or subprocess error. self.close() # Raises exception if subprocess had error. return None # To indicate end-of-file. @@ -1644,20 +1644,24 @@ def add_image(self, image: _NDArray) -> None: if self.input_format == 'yuv': # Convert from per-pixel YUV to planar YUV. image = np.moveaxis(image, 2, 0) data = image.tobytes() - assert self._proc.stdin - if self._proc.stdin.write(data) != len(data): + assert (stdin := self._proc.stdin) is not None + if stdin.write(data) != len(data): self._proc.wait() - assert self._proc.stderr - s = self._proc.stderr.read().decode() + stderr = self._proc.stderr + assert stderr is not None + s = stderr.read().decode() raise RuntimeError(f"Error writing '{self.path}': {s}") def close(self) -> None: """Finishes writing the video. (Called automatically at end of context.)""" if self._popen: assert self._proc and self._proc.stdin and self._proc.stderr - self._proc.stdin.close() + assert (stdin := self._proc.stdin) is not None + stdin.close() if self._proc.wait(): - s = self._proc.stderr.read().decode() + stderr = self._proc.stderr + assert stderr is not None + s = stderr.read().decode() raise RuntimeError(f"Error writing '{self.path}': {s}") self._popen.__exit__(None, None, None) self._popen = None