I'm using the @property decorator and I assign a value to that property. However, mypy throws an error with my code (which works). Do I do something wrong? How can I fix my code so that mypy doesn't complain?
MVCE
# Core Library
from abc import ABCMeta, abstractmethod
class ListNode(metaclass=ABCMeta):
def __init__(self, value):
"""Every list node should have a value at initialization."""
@property
@abstractmethod
def value(self):
"""Read the value attribute"""
@abstractmethod
def next(self):
"""Read the next attribute"""
class SinglyLinkedListNode(ListNode):
def __init__(self, value):
self.value = value
self.next = None # Optional[SinglyLinkedListNode]
@property
def value(self):
return self._value
@value.setter
def value(self, value):
self._value = value
@property
def next(self):
return self._next
@next.setter
def next(self, next_):
self._next = next_
def reverse(list_node: ListNode) -> ListNode:
"""Reverse a list."""
current = list_node
previous = None
while current is not None:
previous, current.next, current = current, previous, current.next # This throws the error
return previous
Error:
mpu/datastructures/lists.py:47: error: Cannot assign to a method
mpu/datastructures/lists.py:47: error: Incompatible types in assignment (expression has type "ListNode", variable has type "Callable[[], Any]")
mpu/datastructures/lists.py:47: error: Incompatible types in assignment (expression has type "Callable[[], Any]", variable has type "ListNode")