From 51700c735bc2915c222d21ae66b69ea1e244d124 Mon Sep 17 00:00:00 2001 From: Pietro Pasotti Date: Mon, 13 Jun 2022 09:30:09 +0200 Subject: [PATCH] some pr comments --- ops/pebble.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/ops/pebble.py b/ops/pebble.py index 512005736..27c57c68a 100644 --- a/ops/pebble.py +++ b/ops/pebble.py @@ -97,7 +97,8 @@ class _Writable(Protocol): # but we can't import that type def write(self, buf: Union[bytes, str, bytearray]) -> int: ... # noqa - _AnyStrFileLikeIO = typing.Union[_FileLikeIO[bytes], _FileLikeIO[str]] + _AnyStrFileLikeIO = Union[_FileLikeIO[bytes], _FileLikeIO[str]] + _TextOrBinaryIO = Union[TextIO, BinaryIO] _IOSource = Union[str, bytes, _AnyStrFileLikeIO] _PartType = Literal['response', 'files'] _Ctype = Literal['multipart/form-data', 'application/json'] @@ -2184,8 +2185,8 @@ def exec( group_id: Optional[int] = None, group: Optional[str] = None, stdin: Optional['_IOSource'] = None, - stdout: Optional[Union[TextIO, BinaryIO]] = None, - stderr: Optional[Union[TextIO, BinaryIO]] = None, + stdout: Optional['_TextOrBinaryIO'] = None, + stderr: Optional['_TextOrBinaryIO'] = None, encoding: Optional[str] = 'utf-8', combine_stderr: bool = False ) -> ExecProcess: @@ -2584,24 +2585,24 @@ def _prepare_tempfile(self, filename: str): def _get_open_tempfile(self): return self._files[self.current_filename] - def get_response(self) -> '_FilesResponse': + def get_response(self) -> Optional['_FilesResponse']: """Return the deserialized JSON object from the multipart "response" field.""" - assert self._response, 'response unset' return self._response def filenames(self): """Return a list of filenames from the "files" parts of the response.""" return list(self._files.keys()) - def get_file(self, path: str, encoding: Optional[str]) -> Union[TextIO, BinaryIO]: + def get_file(self, path: str, encoding: Optional[str]) -> '_TextOrBinaryIO': """Return an open file object containing the data.""" mode = 'r' if encoding else 'rb' # We're using text-based file I/O purely for file encoding purposes, not for # newline normalization. newline='' serves the line endings as-is. newline = '' if encoding else None - return open(self._files[path].name, mode, # type: ignore - encoding=encoding, newline=newline) - + file_io = open(self._files[path].name, mode, + encoding=encoding, newline=newline) + # open() returns IO[Any] + return typing.cast('_TextOrBinaryIO', file_io) class _MultipartParser: def __init__(