Skip to content

Commit

Permalink
Rename python union types (#1392)
Browse files Browse the repository at this point in the history
* based

* Use unions everywhere

* remove base classes

* remove unused imports

* another unused import

* hopefully fix circular dep

* imports order 😤
  • Loading branch information
DaughterOfMars authored Oct 4, 2023
1 parent 9e8ef00 commit d480f57
Show file tree
Hide file tree
Showing 17 changed files with 110 additions and 192 deletions.
20 changes: 6 additions & 14 deletions bindings/python/iota_sdk/types/address.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,7 @@ class AddressType(IntEnum):

@json
@dataclass
class Address():
"""Base class for addresses.
"""
type: int


@json
@dataclass
class Ed25519Address(Address):
class Ed25519Address:
"""Represents an Ed25519 address.
Attributes:
pub_key_hash: The hex encoded Ed25519 public key hash.
Expand All @@ -44,7 +36,7 @@ class Ed25519Address(Address):

@json
@dataclass
class AccountAddress(Address):
class AccountAddress:
"""Represents an Account address.
Attributes:
account_id: The hex encoded account id.
Expand All @@ -58,7 +50,7 @@ class AccountAddress(Address):

@json
@dataclass
class NFTAddress(Address):
class NFTAddress:
"""Represents an NFT address.
Attributes:
nft_id: The hex encoded NFT id.
Expand All @@ -78,10 +70,10 @@ class AddressWithUnspentOutputs():
output_ids: bool


AddressUnion: TypeAlias = Union[Ed25519Address, AccountAddress, NFTAddress]
Address: TypeAlias = Union[Ed25519Address, AccountAddress, NFTAddress]


def deserialize_address(d: Dict[str, Any]) -> AddressUnion:
def deserialize_address(d: Dict[str, Any]) -> Address:
"""
Takes a dictionary as input and returns an instance of a specific class based on the value of the 'type' key in the dictionary.
Expand All @@ -99,7 +91,7 @@ def deserialize_address(d: Dict[str, Any]) -> AddressUnion:


def deserialize_addresses(
dicts: List[Dict[str, Any]]) -> List[AddressUnion]:
dicts: List[Dict[str, Any]]) -> List[Address]:
"""
Takes a list of dictionaries as input and returns a list with specific instances of a classes based on the value of the 'type' key in the dictionary.
Expand Down
8 changes: 4 additions & 4 deletions bindings/python/iota_sdk/types/block/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
from __future__ import annotations
from dataclasses import dataclass, field
from typing import List, Optional
from iota_sdk.types.block.block import Block, BlockType
from iota_sdk.types.block.block import BlockType
from iota_sdk.types.common import HexStr, json
from iota_sdk.types.payload import PayloadUnion
from iota_sdk.types.payload import Payload


@json
@dataclass
class BasicBlock(Block):
class BasicBlock:
"""A `BasicBlock` is the most common type of block used to issue various kinds of payloads such as transactions
at the cost of burning Mana.
Expand All @@ -26,7 +26,7 @@ class BasicBlock(Block):
weak_parents: List[HexStr]
shallow_like_parents: List[HexStr]
max_burned_mana: str
payload: Optional[PayloadUnion] = None
payload: Optional[Payload] = None
type: int = field(
default_factory=lambda: BlockType.Basic,
init=False)
10 changes: 0 additions & 10 deletions bindings/python/iota_sdk/types/block/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

from __future__ import annotations
from enum import IntEnum
from dataclasses import dataclass
from iota_sdk.types.common import json


class BlockType(IntEnum):
Expand All @@ -16,11 +14,3 @@ class BlockType(IntEnum):
"""
Basic = 0
Validation = 1


@json
@dataclass
class Block:
"""Base class for blocks.
"""
type: int
4 changes: 2 additions & 2 deletions bindings/python/iota_sdk/types/block/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
from __future__ import annotations
from dataclasses import dataclass, field
from typing import List
from iota_sdk.types.block.block import Block, BlockType
from iota_sdk.types.block.block import BlockType
from iota_sdk.types.common import HexStr, json


@json
@dataclass
class ValidationBlock(Block):
class ValidationBlock:
"""A Validation Block is a special type of block used by validators to secure the network. It is recognized by the
Congestion Control of the IOTA 2.0 protocol and can be issued without burning Mana within the constraints of the
allowed validator throughput. It is allowed to reference more parent blocks than a normal Basic Block.
Expand Down
17 changes: 9 additions & 8 deletions bindings/python/iota_sdk/types/block/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@
from __future__ import annotations
from dataclasses import dataclass
from typing import TypeAlias, Union
from iota_sdk.types.block.basic import BasicBlock
from iota_sdk.types.block.validation import ValidationBlock
from iota_sdk.utils import Utils
from iota_sdk.types.common import HexStr, json, SlotIndex
from iota_sdk.types.node_info import ProtocolParameters
from iota_sdk.types.signature import SignatureUnion
from iota_sdk.utils import Utils

BlockUnion: TypeAlias = Union[BasicBlock, ValidationBlock]
from iota_sdk.types.signature import Signature
from iota_sdk.types.block.basic import BasicBlock
from iota_sdk.types.block.validation import ValidationBlock


@json
Expand All @@ -36,10 +34,13 @@ class BlockWrapper:
slot_commitment_id: HexStr
latest_finalized_slot: SlotIndex
issuer_id: HexStr
block: BlockUnion
signature: SignatureUnion
block: Block
signature: Signature

def id(self, params: ProtocolParameters) -> HexStr:
"""Returns the block ID as a hexadecimal string.
"""
return Utils.block_id(self, params)


Block: TypeAlias = Union[BasicBlock, ValidationBlock]
22 changes: 7 additions & 15 deletions bindings/python/iota_sdk/types/context_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,7 @@ class ContextInputType(IntEnum):

@json
@dataclass
class ContextInput():
"""Base class for context inputs.
"""
type: int


@json
@dataclass
class CommitmentContextInput(ContextInput):
class CommitmentContextInput:
"""A Commitment Context Input allows referencing a commitment to a certain slot.
Attributes:
Expand All @@ -42,7 +34,7 @@ class CommitmentContextInput(ContextInput):

@json
@dataclass
class BlockIssuanceCreditContextInput(ContextInput):
class BlockIssuanceCreditContextInput:
"""A Block Issuance Credit (BIC) Context Input provides the VM with context for the value of
the BIC vector of a specific slot.
Expand All @@ -59,7 +51,7 @@ class BlockIssuanceCreditContextInput(ContextInput):

@json
@dataclass
class RewardContextInput(ContextInput):
class RewardContextInput:
"""A Reward Context Input indicates which transaction Input is claiming Mana rewards.
Attributes:
Expand All @@ -73,11 +65,11 @@ class RewardContextInput(ContextInput):
init=False)


ContextInputUnion: TypeAlias = Union[CommitmentContextInput,
BlockIssuanceCreditContextInput, RewardContextInput]
ContextInput: TypeAlias = Union[CommitmentContextInput,
BlockIssuanceCreditContextInput, RewardContextInput]


def deserialize_context_input(d: Dict[str, Any]) -> ContextInputUnion:
def deserialize_context_input(d: Dict[str, Any]) -> ContextInput:
"""
Takes a dictionary as input and returns an instance of a specific class based on the value of the 'type' key in the dictionary.
Expand All @@ -95,7 +87,7 @@ def deserialize_context_input(d: Dict[str, Any]) -> ContextInputUnion:


def deserialize_context_inputs(
dicts: List[Dict[str, Any]]) -> List[ContextInputUnion]:
dicts: List[Dict[str, Any]]) -> List[ContextInput]:
"""
Takes a list of dictionaries as input and returns a list with specific instances of a classes based on the value of the 'type' key in the dictionary.
Expand Down
23 changes: 9 additions & 14 deletions bindings/python/iota_sdk/types/essence.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

from __future__ import annotations
from enum import IntEnum
from typing import TYPE_CHECKING, Optional, List
from typing import TYPE_CHECKING, Optional, List, TypeAlias

from dataclasses import dataclass, field

from iota_sdk.types.common import HexStr, json, SlotIndex
from iota_sdk.types.mana import ManaAllotment
from iota_sdk.types.input import UtxoInput
from iota_sdk.types.context_input import ContextInputUnion
from iota_sdk.types.output import OutputUnion
from iota_sdk.types.context_input import ContextInput
from iota_sdk.types.output import Output

# Required to prevent circular import
if TYPE_CHECKING:
Expand All @@ -29,15 +29,7 @@ class EssenceType(IntEnum):

@json
@dataclass
class TransactionEssence:
"""Base class of Transaction essence
"""
type: int


@json
@dataclass
class RegularTransactionEssence(TransactionEssence):
class RegularTransactionEssence:
"""Describes the essence data making up a transaction by defining its inputs, outputs, and an optional payload.
Attributes:
Expand All @@ -55,10 +47,13 @@ class RegularTransactionEssence(TransactionEssence):
creation_slot: SlotIndex
inputs: List[UtxoInput]
inputs_commitment: HexStr
outputs: List[OutputUnion]
context_inputs: Optional[List[ContextInputUnion]] = None
outputs: List[Output]
context_inputs: Optional[List[ContextInput]] = None
allotments: Optional[List[ManaAllotment]] = None
payload: Optional[Payload] = None
type: int = field(
default_factory=lambda: EssenceType.RegularTransactionEssence,
init=False)


TransactionEssence: TypeAlias = RegularTransactionEssence
34 changes: 13 additions & 21 deletions bindings/python/iota_sdk/types/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import Dict, List, TypeAlias, Union, Any
from dataclasses import dataclass, field
from dataclasses_json import config
from iota_sdk.types.address import AddressUnion, deserialize_address
from iota_sdk.types.address import Address, deserialize_address
from iota_sdk.types.common import EpochIndex, HexStr, json, SlotIndex


Expand All @@ -30,20 +30,12 @@ class FeatureType(IntEnum):

@json
@dataclass
class Feature():
"""Base class of a feature.
"""
type: int


@json
@dataclass
class SenderFeature(Feature):
class SenderFeature:
"""Identifies the validated sender of an output.
Attributes:
address: A given sender address.
"""
address: AddressUnion = field(
address: Address = field(
metadata=config(
decoder=deserialize_address
))
Expand All @@ -55,12 +47,12 @@ class SenderFeature(Feature):

@json
@dataclass
class IssuerFeature(Feature):
class IssuerFeature:
"""Identifies the validated issuer of the UTXO state machine.
Attributes:
address: A given issuer address.
"""
address: AddressUnion = field(
address: Address = field(
metadata=config(
decoder=deserialize_address
))
Expand All @@ -72,7 +64,7 @@ class IssuerFeature(Feature):

@json
@dataclass
class MetadataFeature(Feature):
class MetadataFeature:
"""Defines metadata, arbitrary binary data, that will be stored in the output.
Attributes:
data: Some hex encoded metadata.
Expand All @@ -86,7 +78,7 @@ class MetadataFeature(Feature):

@json
@dataclass
class TagFeature(Feature):
class TagFeature:
"""Makes it possible to tag outputs with an index, so they can be retrieved through an indexer API.
Attributes:
tag: A hex encoded tag used to index the output.
Expand All @@ -97,7 +89,7 @@ class TagFeature(Feature):

@json
@dataclass
class BlockIssuerFeature(Feature):
class BlockIssuerFeature:
"""Contains the public keys to verify block signatures and allows for unbonding the issuer deposit.
Attributes:
expiry_slot: The slot index at which the Block Issuer Feature expires and can be removed.
Expand All @@ -114,7 +106,7 @@ class BlockIssuerFeature(Feature):

@json
@dataclass
class StakingFeature(Feature):
class StakingFeature:
"""Stakes IOTA coins to become eligible for committee selection, validate the network and receive Mana rewards.
Attributes:
staked_amount: The amount of IOTA coins that are locked and staked in the containing account.
Expand All @@ -132,11 +124,11 @@ class StakingFeature(Feature):
init=False)


FeatureUnion: TypeAlias = Union[SenderFeature, IssuerFeature,
MetadataFeature, TagFeature, BlockIssuerFeature, StakingFeature]
Feature: TypeAlias = Union[SenderFeature, IssuerFeature,
MetadataFeature, TagFeature, BlockIssuerFeature, StakingFeature]


def deserialize_feature(d: Dict[str, Any]) -> FeatureUnion:
def deserialize_feature(d: Dict[str, Any]) -> Feature:
"""
Takes a dictionary as input and returns an instance of a specific class based on the value of the 'type' key in the dictionary.
Expand All @@ -159,7 +151,7 @@ def deserialize_feature(d: Dict[str, Any]) -> FeatureUnion:
raise Exception(f'invalid feature type: {feature_type}')


def deserialize_features(dicts: List[Dict[str, Any]]) -> List[FeatureUnion]:
def deserialize_features(dicts: List[Dict[str, Any]]) -> List[Feature]:
"""
Takes a list of dictionaries as input and returns a list with specific instances of a classes based on the value of the 'type' key in the dictionary.
Expand Down
Loading

0 comments on commit d480f57

Please sign in to comment.