duffel-api-python icon indicating copy to clipboard operation
duffel-api-python copied to clipboard

Use property getters and setters for validation

Open richcooper95 opened this issue 3 years ago • 0 comments

Describe the bug To ensure that validation of attributes happens every time they're set, we should use @property getters/setters for attributes requiring validation.

To Reproduce n/a

Expected behavior Use @property getters/setters to validate instance attributes - e.g. here:

    def __init__(self, client, order_id):
        """Instantiate an order change request creation."""
        self._client = client
        self._order_id = order_id
        self._slices = []
        OrderChangeRequestCreate._validate_order_id(self._order_id)

    def _validate_order_id(order_id):
        """Set order ID"""
        if type(order_id) is not str:
            raise OrderChangeRequestCreate.InvalidOrderId(order_id)

can be changed to:

    def __init__(self, client, order_id):
        """Instantiate an order change request creation."""
        self.client = client
        self.order_id = order_id
        self.slices = []

    @property
    def order_id(self):
        return self._order_id

    @order_id.setter
    def order_id(self, value):
        if not isinstance(value, str):
            raise OrderChangeRequestCreate.InvalidOrderId(value)

        self._order_id = value

System (please complete the following information): n/a

Additional context Could also fix this issue at the same time!

richcooper95 avatar Feb 02 '22 17:02 richcooper95