Skip to content

Via inheritance

As example

from platonic.sqs.queue import SQSReceiver, SQSSender


class Spell:
    """Magical spell."""

    def __init__(self, text: str) -> None:
        """Initialize."""
        self.text = text


class SpellSender(SQSSender[Spell]):
    """Spell sender."""

    def serialize_value(self, spell: Spell) -> str:
        """Serialize a spell."""
        return spell.text


class SpellReceiver(SQSReceiver[Spell]):
    """Spell receiver."""

    def deserialize_value(self, raw_value: str) -> Spell:
        """Deserialize a spell."""
        return Spell(text=raw_value)

Pro

  • Easy to implement
  • mypy compatible

Contra

  • PyCharm is unhappy:

    Signature of method 'SpellSender.serialize_value()' does not match signature of base method in class 'Sender'

  • Subclassing is required
  • Type conversion is tightly coupled with the data structure you're using it with, and cannot be reused

Warning

That is why this method is DISCOURAGED. Use it at your own risk. Click Next to see an alternative.