From 78aed1316b161f2d8c1d8a7ded010491857141a8 Mon Sep 17 00:00:00 2001 From: Ben Hoyt Date: Fri, 16 Jun 2023 11:44:39 +1200 Subject: [PATCH] Avoid error in FileInfo repr due to permissions being None FileInfo.__repr__ was raising a TypeError due to the expectation that permissions was never None, and the ":o" format string: TypeError: unsupported format string passed to NoneType.__format__ At first I was surprised that Pyright didn't catch this, because clearly dict.get can return None, but we were defining a _FileKwargs TypedDict with a non-optional "permissions" field, so fix that too. Fixes #955 --- ops/testing.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ops/testing.py b/ops/testing.py index 70bb5c4f4..b7509acc2 100755 --- a/ops/testing.py +++ b/ops/testing.py @@ -66,7 +66,7 @@ _StringOrPath = Union[str, pathlib.PurePosixPath, pathlib.Path] _FileOrDir = Union['_File', '_Directory'] _FileKwargs = TypedDict('_FileKwargs', { - 'permissions': int, + 'permissions': Optional[int], 'last_modified': datetime.datetime, 'user_id': Optional[int], 'user': Optional[str], @@ -2476,7 +2476,7 @@ def get_pebble_file_type(file: '_FileOrDir') -> pebble.FileType: name=file.name, type=get_pebble_file_type(file), size=file.size if isinstance(file, _File) else None, - permissions=file.kwargs.get('permissions'), + permissions=file.kwargs.get('permissions') or 0, last_modified=file.last_modified, user_id=file.kwargs.get('user_id'), user=file.kwargs.get('user'),