diff --git a/scalecodec/types.py b/scalecodec/types.py index c341e45..09e0a58 100644 --- a/scalecodec/types.py +++ b/scalecodec/types.py @@ -135,10 +135,6 @@ def example_value(self, _recursion_level: int = 0, max_recursion: int = TYPE_DEC class Bool(ScalePrimitive): - @classmethod - def new(cls): - return ScaleType(type_def=cls()) - def decode(self, data: ScaleBytes) -> bool: bool_data = data.get_next_bytes(1) @@ -898,8 +894,10 @@ def _encode(self, value: Union[str, bytes]) -> ScaleBytes: def serialize(self, value: bytes) -> str: return f'0x{value.hex()}' - def deserialize(self, value: str) -> bytes: - return bytes.fromhex(value[2:]) + def deserialize(self, value: Union[str, bytes]) -> bytes: + if type(value) is str: + value = bytes.fromhex(value[2:]) + return value def example_value(self, _recursion_level: int = 0, max_recursion: int = TYPE_DECOMP_MAX_RECURSIVE): return f'0x{str(self.byte_count).zfill(2) * self.byte_count}' diff --git a/test/__init__.py b/test/__init__.py index c2390ec..ddfffe6 100644 --- a/test/__init__.py +++ b/test/__init__.py @@ -1,6 +1,6 @@ # Python SCALE Codec Library # -# Copyright 2018-2020 Stichting Polkascan (Polkascan Foundation). +# Copyright 2018-2024 Stichting Polkascan (Polkascan Foundation). # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -13,13 +13,4 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -import json -import os - -def load_json_file(file_path: str) -> dict: - - with open(os.path.abspath(file_path), 'r') as fp: - data = fp.read() - - return json.loads(data) diff --git a/test/test_enum.py b/test/test_enum.py index 4e88ef7..6dfc31b 100644 --- a/test/test_enum.py +++ b/test/test_enum.py @@ -22,7 +22,7 @@ class TestEnum(unittest.TestCase): - def test_enum(self): + def test_enum_encode_decode(self): scale_obj = Enum(Bool=Bool(), Number=U32, None_=None).new() value = {'Bool': True} @@ -45,6 +45,21 @@ def test_enum(self): self.assertEqual(value, scale_obj.value) + def test_enum_deserialize(self): + scale_obj = Enum(Bool=Bool(), Number=U32, None_=None).new() + + scale_obj.deserialize({'Bool': True}) + self.assertEqual(('Bool', Bool().new(value=True)), scale_obj.value_object) + + scale_obj.deserialize({'Number': 1}) + self.assertEqual(('Number', U32.new(value=1)), scale_obj.value_object) + + scale_obj.deserialize({'None': None}) + self.assertEqual(('None', None), scale_obj.value_object) + + scale_obj.deserialize('None') + self.assertEqual(('None', None), scale_obj.value_object) + if __name__ == '__main__': unittest.main()