From bb62dded29796080b26fec40ffe722a27a839e9c Mon Sep 17 00:00:00 2001 From: Jussi Kukkonen Date: Thu, 30 Jan 2025 18:20:39 +0200 Subject: [PATCH 1/2] Add type check mypy warns about this but we know that encode_canonical() cannot return None if we don't set output_function argument. ruff does not like assert so I added a "noqa" and a comment Signed-off-by: Jussi Kukkonen --- tuf/api/serialization/json.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tuf/api/serialization/json.py b/tuf/api/serialization/json.py index a031ef8255..dcff79e029 100644 --- a/tuf/api/serialization/json.py +++ b/tuf/api/serialization/json.py @@ -98,7 +98,10 @@ def serialize(self, signed_obj: Signed) -> bytes: """ try: signed_dict = signed_obj.to_dict() - canonical_bytes = encode_canonical(signed_dict).encode("utf-8") + canon_str = encode_canonical(signed_dict) + # encode_canonical cannot return None if output_function is not set + assert canon_str is not None # noqa: S101 + canonical_bytes = canon_str.encode("utf-8") except Exception as e: raise SerializationError from e From 1a1312e1afd01304b9d844aafa55a0874e9984fc Mon Sep 17 00:00:00 2001 From: Jussi Kukkonen Date: Fri, 31 Jan 2025 13:43:03 +0200 Subject: [PATCH 2/2] dsse: Improve type checking mypy rightly complains our types do not match (this only happen if you enable type checks for securesystemslib): * I think the annotation is actually wrong: Envelope does not know the contained type at this point. * Likely SimpleEnvelope should not be generic: it does not relly know what it contains I decided not to break the API here and just made the type cast explicit (even though we don't really know that the cast is correct): this silences mypy but has no other consequences. Signed-off-by: Jussi Kukkonen --- tuf/api/dsse.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tuf/api/dsse.py b/tuf/api/dsse.py index d027d14013..493fefd1d0 100644 --- a/tuf/api/dsse.py +++ b/tuf/api/dsse.py @@ -81,7 +81,7 @@ def from_bytes(cls, data: bytes) -> SimpleEnvelope[T]: except Exception as e: raise DeserializationError from e - return envelope + return cast(SimpleEnvelope[T], envelope) def to_bytes(self) -> bytes: """Return envelope as JSON bytes.