Skip to content

Commit bfd4e35

Browse files
authored
Fix missing type and description when saved to json file (#457)
* Fix missing type and description when saved to json file * Fix types
1 parent 332d8f4 commit bfd4e35

File tree

5 files changed

+27
-17
lines changed

5 files changed

+27
-17
lines changed

pycardano/address.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ def save(
416416
path: str,
417417
key_type: Optional[str] = None,
418418
description: Optional[str] = None,
419+
**kwargs,
419420
):
420421
"""
421422
Save the Address object to a file.
@@ -427,6 +428,7 @@ def save(
427428
path (str): The file path to save the object to.
428429
key_type (str, optional): Not used in this context, but can be included for consistency.
429430
description (str, optional): Not used in this context, but can be included for consistency.
431+
**kwargs: Additional keyword arguments (not used here).
430432
431433
Raises:
432434
IOError: If the file already exists and is not empty.

pycardano/key.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def to_primitive(self) -> bytes:
7373
def from_primitive(cls: Type["Key"], value: bytes) -> Key:
7474
return cls(value)
7575

76-
def to_json(self, **kwargs) -> str:
76+
def to_json(self, **kwargs) -> str: # type: ignore
7777
"""Serialize the key to JSON.
7878
7979
The json output has three fields: "type", "description", and "cborHex".
@@ -90,7 +90,7 @@ def to_json(self, **kwargs) -> str:
9090
)
9191

9292
@classmethod
93-
def from_json(cls: Type[Key], data: str, validate_type=False) -> Key:
93+
def from_json(cls: Type[Key], data: str, validate_type=False) -> Key: # type: ignore
9494
"""Restore a key from a JSON string.
9595
9696
Args:

pycardano/plutus.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ def _dfs(obj):
652652

653653
return _dfs(self)
654654

655-
def to_json(self, **kwargs) -> str:
655+
def to_json(self, **kwargs) -> str: # type: ignore
656656
"""Convert to a json string
657657
658658
Args:
@@ -847,7 +847,7 @@ def _dfs(obj):
847847

848848
return _dfs(RawPlutusData.to_primitive(self))
849849

850-
def to_json(self, **kwargs) -> str:
850+
def to_json(self, **kwargs) -> str: # type: ignore
851851
"""Convert to a json string
852852
853853
Args:

pycardano/serialization.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -561,29 +561,35 @@ def json_description(self) -> str:
561561
"""
562562
return self.__class__.__doc__ or "Generated with PyCardano"
563563

564-
def to_json(self, **kwargs) -> str:
564+
def to_json(
565+
self,
566+
key_type: Optional[str] = None,
567+
description: Optional[str] = None,
568+
**kwargs,
569+
) -> str:
565570
"""
566571
Convert the CBORSerializable object to a JSON string containing type, description, and CBOR hex.
567572
568573
This method returns a JSON representation of the object, including its type, description, and CBOR hex encoding.
569574
570575
Args:
571-
**kwargs: Additional keyword arguments that can include:
572-
- key_type (str): The type to use in the JSON output. Defaults to the class name.
573-
- description (str): The description to use in the JSON output. Defaults to the class docstring.
576+
key_type (str): The type to use in the JSON output. Defaults to the class name.
577+
description (str): The description to use in the JSON output. Defaults to the class docstring.
578+
**kwargs: Extra key word arguments to be passed to `json.dumps()`
574579
575580
Returns:
576581
str: The JSON string representation of the object.
577582
"""
578-
key_type = kwargs.pop("key_type", self.json_type)
579-
description = kwargs.pop("description", self.json_description)
583+
if "indent" not in kwargs:
584+
kwargs["indent"] = 2
585+
580586
return json.dumps(
581587
{
582-
"type": key_type,
583-
"description": description,
588+
"type": key_type or self.json_type,
589+
"description": description or self.json_description,
584590
"cborHex": self.to_cbor_hex(),
585591
},
586-
indent=2,
592+
**kwargs,
587593
)
588594

589595
@classmethod
@@ -616,6 +622,7 @@ def save(
616622
path: str,
617623
key_type: Optional[str] = None,
618624
description: Optional[str] = None,
625+
**kwargs,
619626
):
620627
"""
621628
Save the CBORSerializable object to a file in JSON format.
@@ -625,16 +632,17 @@ def save(
625632
626633
Args:
627634
path (str): The file path to save the object to.
628-
key_type (str, optional): The type to use in the JSON output.
629-
description (str, optional): The description to use in the JSON output.
635+
key_type (str, optional): The type to use in the JSON output. Defaults to the class name.
636+
description (str, optional): The description to use in the JSON output. Defaults to the class docstring.
637+
**kwargs: Extra key word arguments to be passed to `json.dumps()`
630638
631639
Raises:
632640
IOError: If the file already exists and is not empty.
633641
"""
634642
if os.path.isfile(path) and os.stat(path).st_size > 0:
635643
raise IOError(f"File {path} already exists!")
636644
with open(path, "w") as f:
637-
f.write(self.to_json(key_type=key_type, description=description))
645+
f.write(self.to_json(key_type=key_type, description=description, **kwargs))
638646

639647
@classmethod
640648
def load(cls, path: str):

pycardano/transaction.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ class Transaction(ArrayCBORSerializable):
698698
def json_type(self) -> str:
699699
return (
700700
"Unwitnessed Tx ConwayEra"
701-
if self.transaction_witness_set.is_empty()
701+
if self.transaction_witness_set.vkey_witnesses is None
702702
else "Signed Tx ConwayEra"
703703
)
704704

0 commit comments

Comments
 (0)