Skip to content

Commit

Permalink
Add Union type aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
Thoralf-M committed Sep 29, 2023
1 parent 45e6c7c commit 14d352b
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 59 deletions.
12 changes: 6 additions & 6 deletions bindings/python/iota_sdk/types/address.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
# SPDX-License-Identifier: Apache-2.0

from enum import IntEnum

from dataclasses import dataclass, field
from typing import Any, Dict, List, Union


from typing import Any, Dict, List, TypeAlias, Union
from iota_sdk.types.common import HexStr, json


Expand Down Expand Up @@ -81,7 +78,10 @@ class AddressWithUnspentOutputs():
output_ids: bool


def deserialize_address(d: Dict[str, Any]) -> Union[Ed25519Address, AccountAddress, NFTAddress]:
AddressUnion: TypeAlias = Union[Ed25519Address, AccountAddress, NFTAddress]


def deserialize_address(d: Dict[str, Any]) -> AddressUnion:
"""
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 +99,7 @@ def deserialize_address(d: Dict[str, Any]) -> Union[Ed25519Address, AccountAddre


def deserialize_addresses(
dicts: List[Dict[str, Any]]) -> List[Union[Ed25519Address, AccountAddress, NFTAddress]]:
dicts: List[Dict[str, Any]]) -> List[AddressUnion]:
"""
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
11 changes: 7 additions & 4 deletions bindings/python/iota_sdk/types/context_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from __future__ import annotations
from dataclasses import dataclass, field
from enum import IntEnum
from typing import Any, Dict, List, Union
from typing import Any, Dict, List, TypeAlias, Union
from iota_sdk.types.common import HexStr, json


Expand Down Expand Up @@ -73,8 +73,11 @@ class RewardContextInput(ContextInput):
init=False)


def deserialize_context_input(d: Dict[str, Any]) -> Union[CommitmentContextInput,
BlockIssuanceCreditContextInput, RewardContextInput]:
ContextInputUnion: TypeAlias = Union[CommitmentContextInput,
BlockIssuanceCreditContextInput, RewardContextInput]


def deserialize_context_input(d: Dict[str, Any]) -> ContextInputUnion:
"""
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 @@ -92,7 +95,7 @@ def deserialize_context_input(d: Dict[str, Any]) -> Union[CommitmentContextInput


def deserialize_context_inputs(
dicts: List[Dict[str, Any]]) -> List[Union[CommitmentContextInput, BlockIssuanceCreditContextInput, RewardContextInput]]:
dicts: List[Dict[str, Any]]) -> List[ContextInputUnion]:
"""
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
14 changes: 5 additions & 9 deletions bindings/python/iota_sdk/types/essence.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@

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

from dataclasses import dataclass, field

from iota_sdk.types.common import HexStr, json, SlotIndex
from iota_sdk.types.mana import ManaAllotment
# TODO: Add missing output types in #1174
# pylint: disable=no-name-in-module
from iota_sdk.types.output import BasicOutput, AccountOutput, FoundryOutput, NftOutput, DelegationOutput
from iota_sdk.types.input import UtxoInput
from iota_sdk.types.context_input import CommitmentContextInput, BlockIssuanceCreditContextInput, RewardContextInput
from iota_sdk.types.context_input import ContextInputUnion
from iota_sdk.types.output import OutputUnion

# Required to prevent circular import
if TYPE_CHECKING:
Expand Down Expand Up @@ -57,10 +55,8 @@ class RegularTransactionEssence(TransactionEssence):
creation_slot: SlotIndex
inputs: List[UtxoInput]
inputs_commitment: HexStr
outputs: List[Union[BasicOutput, AccountOutput,
FoundryOutput, NftOutput, DelegationOutput]]
context_inputs: Optional[List[Union[CommitmentContextInput,
BlockIssuanceCreditContextInput, RewardContextInput]]] = None
outputs: List[OutputUnion]
context_inputs: Optional[List[ContextInputUnion]] = None
allotments: Optional[List[ManaAllotment]] = None
payload: Optional[Payload] = None
type: int = field(
Expand Down
18 changes: 10 additions & 8 deletions bindings/python/iota_sdk/types/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
# SPDX-License-Identifier: Apache-2.0

from enum import IntEnum
from typing import Dict, List, Union, Any
from typing import Dict, List, TypeAlias, Union, Any
from dataclasses import dataclass, field
from dataclasses_json import config
from iota_sdk.types.address import Ed25519Address, AccountAddress, NFTAddress, deserialize_address
from iota_sdk.types.address import AddressUnion, deserialize_address
from iota_sdk.types.common import EpochIndex, HexStr, json, SlotIndex


Expand Down Expand Up @@ -43,7 +43,7 @@ class SenderFeature(Feature):
Attributes:
address: A given sender address.
"""
address: Union[Ed25519Address, AccountAddress, NFTAddress] = field(
address: AddressUnion = field(
metadata=config(
decoder=deserialize_address
))
Expand All @@ -60,7 +60,7 @@ class IssuerFeature(Feature):
Attributes:
address: A given issuer address.
"""
address: Union[Ed25519Address, AccountAddress, NFTAddress] = field(
address: AddressUnion = field(
metadata=config(
decoder=deserialize_address
))
Expand Down Expand Up @@ -132,8 +132,11 @@ class StakingFeature(Feature):
init=False)


def deserialize_feature(d: Dict[str, Any]) -> Union[SenderFeature, IssuerFeature,
MetadataFeature, TagFeature, BlockIssuerFeature, StakingFeature]:
FeatureUnion: TypeAlias = Union[SenderFeature, IssuerFeature,
MetadataFeature, TagFeature, BlockIssuerFeature, StakingFeature]


def deserialize_feature(d: Dict[str, Any]) -> FeatureUnion:
"""
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 @@ -156,8 +159,7 @@ def deserialize_feature(d: Dict[str, Any]) -> Union[SenderFeature, IssuerFeature
raise Exception(f'invalid feature type: {feature_type}')


def deserialize_features(dicts: List[Dict[str, Any]]) -> List[Union[SenderFeature,
IssuerFeature, MetadataFeature, TagFeature, BlockIssuerFeature, StakingFeature]]:
def deserialize_features(dicts: List[Dict[str, Any]]) -> List[FeatureUnion]:
"""
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
12 changes: 7 additions & 5 deletions bindings/python/iota_sdk/types/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from __future__ import annotations
from enum import IntEnum
from typing import Dict, Optional, List, Union, Any
from typing import Dict, Optional, List, TypeAlias, Union, Any
from dataclasses import dataclass, field
from dataclasses_json import config
from iota_sdk.types.common import HexStr, json
Expand Down Expand Up @@ -227,8 +227,11 @@ class DelegationOutput(Output):
OutputType.Delegation), init=False)


def deserialize_output(d: Dict[str, Any]) -> Union[BasicOutput, AccountOutput,
FoundryOutput, NftOutput, DelegationOutput]:
OutputUnion: TypeAlias = Union[BasicOutput, AccountOutput,
FoundryOutput, NftOutput, DelegationOutput]


def deserialize_output(d: Dict[str, Any]) -> OutputUnion:
"""
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 @@ -249,8 +252,7 @@ def deserialize_output(d: Dict[str, Any]) -> Union[BasicOutput, AccountOutput,
raise Exception(f'invalid output type: {output_type}')


def deserialize_outputs(dicts: List[Dict[str, Any]]) -> List[Union[BasicOutput,
AccountOutput, FoundryOutput, NftOutput, DelegationOutput]]:
def deserialize_outputs(dicts: List[Dict[str, Any]]) -> List[OutputUnion]:
"""
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
10 changes: 5 additions & 5 deletions bindings/python/iota_sdk/types/output_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

from __future__ import annotations
from dataclasses import dataclass
from typing import Optional, Union
from iota_sdk.types.address import Ed25519Address, AccountAddress, NFTAddress
from typing import Optional
from iota_sdk.types.address import AddressUnion
from iota_sdk.types.common import HexStr, json
from iota_sdk.types.output import BasicOutput, AccountOutput, FoundryOutput, NftOutput
from iota_sdk.types.output import OutputUnion
from iota_sdk.types.output_metadata import OutputMetadata
from iota_sdk.types.signature import Bip44

Expand All @@ -28,9 +28,9 @@ class OutputData():
"""
output_id: HexStr
metadata: OutputMetadata
output: Union[AccountOutput, FoundryOutput, NftOutput, BasicOutput]
output: OutputUnion
is_spent: bool
address: Union[Ed25519Address, AccountAddress, NFTAddress]
address: AddressUnion
network_id: str
remainder: bool
chain: Optional[Bip44] = None
11 changes: 6 additions & 5 deletions bindings/python/iota_sdk/types/payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@

from __future__ import annotations
from enum import IntEnum
from typing import Any, Dict, List, Union

from typing import Any, Dict, List, TypeAlias, Union
from dataclasses import dataclass, field

from iota_sdk.types.common import HexStr, json
from iota_sdk.types.essence import RegularTransactionEssence
from iota_sdk.types.unlock import SignatureUnlock, ReferenceUnlock
Expand Down Expand Up @@ -65,7 +63,10 @@ class TransactionPayload(Payload):
init=False)


def deserialize_payload(d: Dict[str, Any]) -> Union[TaggedDataPayload, TransactionPayload]:
PayloadUnion: TypeAlias = Union[TaggedDataPayload, TransactionPayload]


def deserialize_payload(d: Dict[str, Any]) -> PayloadUnion:
"""
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 @@ -81,7 +82,7 @@ def deserialize_payload(d: Dict[str, Any]) -> Union[TaggedDataPayload, Transacti


def deserialize_payloads(
dicts: List[Dict[str, Any]]) -> List[Union[TaggedDataPayload, TransactionPayload]]:
dicts: List[Dict[str, Any]]) -> List[PayloadUnion]:
"""
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
12 changes: 6 additions & 6 deletions bindings/python/iota_sdk/types/transaction_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

from __future__ import annotations
from dataclasses import dataclass
from typing import Optional, List, Union
from iota_sdk.types.address import Ed25519Address, AccountAddress, NFTAddress
from iota_sdk.types.output import BasicOutput, AccountOutput, FoundryOutput, NftOutput
from typing import Optional, List
from iota_sdk.types.address import AddressUnion
from iota_sdk.types.output import OutputUnion
from iota_sdk.types.output_metadata import OutputMetadata
from iota_sdk.types.essence import RegularTransactionEssence
from iota_sdk.types.payload import TransactionPayload
Expand All @@ -23,7 +23,7 @@ class InputSigningData:
output_metadata: The output metadata.
chain: The BIP44 chain for the address to unlock the output.
"""
output: Union[AccountOutput, FoundryOutput, NftOutput, BasicOutput]
output: OutputUnion
output_metadata: OutputMetadata
chain: Optional[Bip44] = None

Expand All @@ -38,8 +38,8 @@ class RemainderData:
address: The remainder address.
chain: The BIP44 chain for the remainder address.
"""
output: Union[AccountOutput, FoundryOutput, NftOutput, BasicOutput]
address: Union[Ed25519Address, AccountAddress, NFTAddress]
output: OutputUnion
address: AddressUnion
chain: Optional[Bip44] = None


Expand Down
24 changes: 13 additions & 11 deletions bindings/python/iota_sdk/types/unlock_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

from enum import IntEnum
from dataclasses import dataclass, field
from typing import Dict, List, Union, Any
from typing import Dict, List, TypeAlias, Union, Any
from dataclasses_json import config
from iota_sdk.types.address import Ed25519Address, AccountAddress, NFTAddress
from iota_sdk.types.address import AddressUnion, AccountAddress
from iota_sdk.types.common import json
from iota_sdk.types.address import deserialize_address

Expand Down Expand Up @@ -47,7 +47,7 @@ class AddressUnlockCondition(UnlockCondition):
Args:
address: An address unlocked with a private key.
"""
address: Union[Ed25519Address, AccountAddress, NFTAddress] = field(
address: AddressUnion = field(
metadata=config(
decoder=deserialize_address
))
Expand All @@ -66,7 +66,7 @@ class StorageDepositReturnUnlockCondition(UnlockCondition):
return_address: The address to return the amount to.
"""
amount: str
return_address: Union[Ed25519Address, AccountAddress, NFTAddress] = field(
return_address: AddressUnion = field(
metadata=config(
decoder=deserialize_address
))
Expand Down Expand Up @@ -97,7 +97,7 @@ class ExpirationUnlockCondition(UnlockCondition):
return_address: The return address if the output was not claimed in time.
"""
unix_time: int
return_address: Union[Ed25519Address, AccountAddress, NFTAddress] = field(
return_address: AddressUnion = field(
metadata=config(
decoder=deserialize_address
))
Expand All @@ -114,7 +114,7 @@ class StateControllerAddressUnlockCondition(UnlockCondition):
Args:
address: The state controller address that owns the output.
"""
address: Union[Ed25519Address, AccountAddress, NFTAddress] = field(
address: AddressUnion = field(
metadata=config(
decoder=deserialize_address
))
Expand All @@ -129,7 +129,7 @@ class GovernorAddressUnlockCondition(UnlockCondition):
Args:
address: The governor address that owns the output.
"""
address: Union[Ed25519Address, AccountAddress, NFTAddress] = field(
address: AddressUnion = field(
metadata=config(
decoder=deserialize_address
))
Expand All @@ -149,8 +149,11 @@ class ImmutableAccountAddressUnlockCondition(UnlockCondition):
UnlockConditionType.ImmutableAccountAddress), init=False)


def deserialize_unlock_condition(d: Dict[str, Any]) -> Union[AddressUnlockCondition, StorageDepositReturnUnlockCondition, TimelockUnlockCondition,
ExpirationUnlockCondition, StateControllerAddressUnlockCondition, GovernorAddressUnlockCondition, ImmutableAccountAddressUnlockCondition]:
UnlockConditionUnion: TypeAlias = Union[AddressUnlockCondition, StorageDepositReturnUnlockCondition, TimelockUnlockCondition,
ExpirationUnlockCondition, StateControllerAddressUnlockCondition, GovernorAddressUnlockCondition, ImmutableAccountAddressUnlockCondition]


def deserialize_unlock_condition(d: Dict[str, Any]) -> UnlockConditionUnion:
"""
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 @@ -176,8 +179,7 @@ def deserialize_unlock_condition(d: Dict[str, Any]) -> Union[AddressUnlockCondit
raise Exception(f'invalid unlock condition type: {uc_type}')


def deserialize_unlock_conditions(dicts: List[Dict[str, Any]]) -> List[Union[AddressUnlockCondition, StorageDepositReturnUnlockCondition, TimelockUnlockCondition,
ExpirationUnlockCondition, StateControllerAddressUnlockCondition, GovernorAddressUnlockCondition, ImmutableAccountAddressUnlockCondition]]:
def deserialize_unlock_conditions(dicts: List[Dict[str, Any]]) -> List[UnlockConditionUnion]:
"""
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

0 comments on commit 14d352b

Please sign in to comment.