Skip to content

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

Closed

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?

Activity

  1. locked and limited conversation to collaborators on Oct 25, 2023
  2. converted this issue into a discussion #1498 on Oct 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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