From b8b775e86a1fcb2ec4f295a6395106167bfddea6 Mon Sep 17 00:00:00 2001 From: Jerry Date: Sat, 30 Aug 2025 12:05:02 -0700 Subject: [PATCH 1/2] Fix missing type and description when saved to json file --- pycardano/serialization.py | 17 +++++++---------- pycardano/transaction.py | 2 +- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/pycardano/serialization.py b/pycardano/serialization.py index 63e13586..4dd7c1e5 100644 --- a/pycardano/serialization.py +++ b/pycardano/serialization.py @@ -561,26 +561,23 @@ def json_description(self) -> str: """ return self.__class__.__doc__ or "Generated with PyCardano" - def to_json(self, **kwargs) -> str: + def to_json(self, key_type: Optional[str] = None, description: Optional[str] = None) -> str: """ Convert the CBORSerializable object to a JSON string containing type, description, and CBOR hex. This method returns a JSON representation of the object, including its type, description, and CBOR hex encoding. Args: - **kwargs: Additional keyword arguments that can include: - - key_type (str): The type to use in the JSON output. Defaults to the class name. - - description (str): The description to use in the JSON output. Defaults to the class docstring. + key_type (str): The type to use in the JSON output. Defaults to the class name. + description (str): The description to use in the JSON output. Defaults to the class docstring. Returns: str: The JSON string representation of the object. """ - key_type = kwargs.pop("key_type", self.json_type) - description = kwargs.pop("description", self.json_description) return json.dumps( { - "type": key_type, - "description": description, + "type": key_type or self.json_type, + "description": description or self.json_description, "cborHex": self.to_cbor_hex(), }, indent=2, @@ -625,8 +622,8 @@ def save( Args: path (str): The file path to save the object to. - key_type (str, optional): The type to use in the JSON output. - description (str, optional): The description to use in the JSON output. + key_type (str, optional): The type to use in the JSON output. Defaults to the class name. + description (str, optional): The description to use in the JSON output. Defaults to the class docstring. Raises: IOError: If the file already exists and is not empty. diff --git a/pycardano/transaction.py b/pycardano/transaction.py index d9f5515f..a193baf7 100644 --- a/pycardano/transaction.py +++ b/pycardano/transaction.py @@ -698,7 +698,7 @@ class Transaction(ArrayCBORSerializable): def json_type(self) -> str: return ( "Unwitnessed Tx ConwayEra" - if self.transaction_witness_set.is_empty() + if self.transaction_witness_set.vkey_witnesses is None else "Signed Tx ConwayEra" ) From 30819785d4ad2dc62c195247d4911f35a98f594c Mon Sep 17 00:00:00 2001 From: Jerry Date: Sat, 30 Aug 2025 12:30:21 -0700 Subject: [PATCH 2/2] Fix types --- pycardano/address.py | 2 ++ pycardano/key.py | 4 ++-- pycardano/plutus.py | 4 ++-- pycardano/serialization.py | 17 ++++++++++++++--- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/pycardano/address.py b/pycardano/address.py index ba7a6dd7..61547edc 100644 --- a/pycardano/address.py +++ b/pycardano/address.py @@ -416,6 +416,7 @@ def save( path: str, key_type: Optional[str] = None, description: Optional[str] = None, + **kwargs, ): """ Save the Address object to a file. @@ -427,6 +428,7 @@ def save( path (str): The file path to save the object to. key_type (str, optional): Not used in this context, but can be included for consistency. description (str, optional): Not used in this context, but can be included for consistency. + **kwargs: Additional keyword arguments (not used here). Raises: IOError: If the file already exists and is not empty. diff --git a/pycardano/key.py b/pycardano/key.py index e8bce690..d2d728d8 100644 --- a/pycardano/key.py +++ b/pycardano/key.py @@ -73,7 +73,7 @@ def to_primitive(self) -> bytes: def from_primitive(cls: Type["Key"], value: bytes) -> Key: return cls(value) - def to_json(self, **kwargs) -> str: + def to_json(self, **kwargs) -> str: # type: ignore """Serialize the key to JSON. The json output has three fields: "type", "description", and "cborHex". @@ -90,7 +90,7 @@ def to_json(self, **kwargs) -> str: ) @classmethod - def from_json(cls: Type[Key], data: str, validate_type=False) -> Key: + def from_json(cls: Type[Key], data: str, validate_type=False) -> Key: # type: ignore """Restore a key from a JSON string. Args: diff --git a/pycardano/plutus.py b/pycardano/plutus.py index 435c8f4f..83c66705 100644 --- a/pycardano/plutus.py +++ b/pycardano/plutus.py @@ -652,7 +652,7 @@ def _dfs(obj): return _dfs(self) - def to_json(self, **kwargs) -> str: + def to_json(self, **kwargs) -> str: # type: ignore """Convert to a json string Args: @@ -847,7 +847,7 @@ def _dfs(obj): return _dfs(RawPlutusData.to_primitive(self)) - def to_json(self, **kwargs) -> str: + def to_json(self, **kwargs) -> str: # type: ignore """Convert to a json string Args: diff --git a/pycardano/serialization.py b/pycardano/serialization.py index 4dd7c1e5..22f51a92 100644 --- a/pycardano/serialization.py +++ b/pycardano/serialization.py @@ -561,7 +561,12 @@ def json_description(self) -> str: """ return self.__class__.__doc__ or "Generated with PyCardano" - def to_json(self, key_type: Optional[str] = None, description: Optional[str] = None) -> str: + def to_json( + self, + key_type: Optional[str] = None, + description: Optional[str] = None, + **kwargs, + ) -> str: """ Convert the CBORSerializable object to a JSON string containing type, description, and CBOR hex. @@ -570,17 +575,21 @@ def to_json(self, key_type: Optional[str] = None, description: Optional[str] = N Args: key_type (str): The type to use in the JSON output. Defaults to the class name. description (str): The description to use in the JSON output. Defaults to the class docstring. + **kwargs: Extra key word arguments to be passed to `json.dumps()` Returns: str: The JSON string representation of the object. """ + if "indent" not in kwargs: + kwargs["indent"] = 2 + return json.dumps( { "type": key_type or self.json_type, "description": description or self.json_description, "cborHex": self.to_cbor_hex(), }, - indent=2, + **kwargs, ) @classmethod @@ -613,6 +622,7 @@ def save( path: str, key_type: Optional[str] = None, description: Optional[str] = None, + **kwargs, ): """ Save the CBORSerializable object to a file in JSON format. @@ -624,6 +634,7 @@ def save( path (str): The file path to save the object to. key_type (str, optional): The type to use in the JSON output. Defaults to the class name. description (str, optional): The description to use in the JSON output. Defaults to the class docstring. + **kwargs: Extra key word arguments to be passed to `json.dumps()` Raises: IOError: If the file already exists and is not empty. @@ -631,7 +642,7 @@ def save( if os.path.isfile(path) and os.stat(path).st_size > 0: raise IOError(f"File {path} already exists!") with open(path, "w") as f: - f.write(self.to_json(key_type=key_type, description=description)) + f.write(self.to_json(key_type=key_type, description=description, **kwargs)) @classmethod def load(cls, path: str):