Skip to content

feat: Add __repr__ to NftId, TokenTransfer, and TokenNftTransfer #601

@Om7035

Description

@Om7035

Problem

Currently, the following classes in the src/hiero_sdk_python/tokens/ directory have str methods but are missing __repr__ methods:

  1. NftId (src/hiero_sdk_python/tokens/nft_id.py)
  2. TokenTransfer (src/hiero_sdk_python/tokens/token_transfer.py)
  3. TokenNftTransfer (src/hiero_sdk_python/tokens/token_nft_transfer.py)

According to Python best practices (PEP 257), classes should implement both str and __repr__ methods:

  • str() - Returns a human-readable string representation
  • __repr__() - Returns an unambiguous string representation that ideally could be used to recreate the object

Current State:

  • All three classes have str implemented ✅
  • None of them have __repr__ implemented ❌

Inconsistency:
Other similar classes in the same module already have __repr__ methods:

  • HbarTransfer has both str and __repr__
  • HbarAllowance has both str and __repr__
  • TokenAllowance has both str and __repr__
  • TokenNftAllowance has both str and __repr__

Proposed Solution

Add __repr__ methods to all three classes following the existing pattern used in other token classes.

Implementation Details

1. For NftId class (src/hiero_sdk_python/tokens/nft_id.py)

Add a __repr__ method after the existing str method (after line 135):

def __repr__(self) -> str:
    """
    Returns an unambiguous string representation of the NftId.
    
    Returns:
        str: A string that could be used to recreate the NftId object.
    """
    return f"NftId(token_id={self.token_id!r}, serial_number={self.serial_number!r})"

2. For TokenTransfer class (src/hiero_sdk_python/tokens/token_transfer.py)

Add a __repr__ method after the existing str method (after line 70):

def __repr__(self) -> str:
    """
    Returns an unambiguous string representation of the TokenTransfer.
    
    Returns:
        str: A string that could be used to recreate the TokenTransfer object.
    """
    return (
        f"TokenTransfer("
        f"token_id={self.token_id!r}, "
        f"account_id={self.account_id!r}, "
        f"amount={self.amount!r}, "
        f"expected_decimals={self.expected_decimals!r}, "
        f"is_approved={self.is_approved!r})"
    )

3. For TokenNftTransfer class (src/hiero_sdk_python/tokens/token_nft_transfer.py)

Add a __repr__ method after the existing str method (after line 95):

def __repr__(self) -> str:
    """
    Returns an unambiguous string representation of the TokenNftTransfer.
    
    Returns:
        str: A string that could be used to recreate the TokenNftTransfer object.
    """
    return (
        f"TokenNftTransfer("
        f"token_id={self.token_id!r}, "
        f"sender_id={self.sender_id!r}, "
        f"receiver_id={self.receiver_id!r}, "
        f"serial_number={self.serial_number!r}, "
        f"is_approved={self.is_approved!r})"
    )

Benefits

  1. Consistency - Aligns with other token classes that already have __repr__
  2. Better Debugging - Provides clearer object representation in debuggers and logs
  3. Python Best Practices - Follows PEP 257 guidelines
  4. Developer Experience - Makes it easier to inspect objects during development

Acceptance Criteria

  • Add __repr__ method to NftId class
  • Add __repr__ method to TokenTransfer class
  • Add __repr__ method to TokenNftTransfer class
  • All __repr__ methods include Google-style docstrings
  • Update CHANGELOG.md with the changes
  • All commits are GPG signed (-S) and DCO signed (-s)
  • Code passes pylint with 10/10 rating
  • All tests pass

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions