Skip to content

How to define a default implementation for a Protocol property with a setter #1497

@vnmabus

Description

@vnmabus

I hope this question is not too basic. Consider the case in which I want to provide a Protocol with a default implementation that performs some action on the setter of one of its properties (so it is not simply declaring an attribute):

class MyProtocol(Protocol):

    @property
    def my_property(self) -> int:
        return self._my_property

    @my_property.setter
    def my_property(self, value: int) -> None:
        do_something(value)
        self._my_property = value

Here Mypy complains because self._my_property is not part of the Protocol. Is there an accepted way to achieve this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: otherOther topics not covered

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions