Search code examples
pythonmypy

mypy: Signature of "<func_name>" incompatible with supertype "<base_class>" [override]


I am trying to implement an abstractmethod, while extending its input arguments at the same time.

from abc import ABC, abstractmethod
from typing import Any

import pandas as pd


class Strategies(ABC):
    def __init(self, price: pd.Series | pd.DataFrame) -> None:
        self.price = price

    @abstractmethod
    def compute_signls(self, *args: Any, **kwargs: Any) -> dict[str, pd.DataFrame]:
        """Compute signals"""


class MyStrategy(Strategies):
    def compute_signls(
        self, indicator: pd.DataFrame, *args: Any, **kwargs: Any
    ) -> dict[str, pd.DataFrame]:
        pass

mypy is complaining of the signature implementation:

src\port_bt\extend_base.py: note: In member "compute_signls" of class "MyStrategy":
src\port_bt\extend_base.py:17:5: error: Signature of "compute_signls" incompatible with supertype "Strategies"  [override]
src\port_bt\extend_base.py:17:5: note:      Superclass:
src\port_bt\extend_base.py:17:5: note:          def compute_signls(self, *args: Any, **kwargs: Any) -> Dict[str, Any]
src\port_bt\extend_base.py:17:5: note:      Subclass:
src\port_bt\extend_base.py:17:5: note:          def compute_signls(self, indicator: Any, *args: Any, **kwargs: Any) -> Dict[str, Any
]
Found 1 error in 1 file (checked 1 source file)

I actually don't understand why this concrete implementation is wrong?


Solution

  • It violates the Liskov substitution principle:
    https://en.wikipedia.org/wiki/Liskov_substitution_principle

    Your code works probably fine as it is. But indicator should be an argument passed to __init__ of MyStrategy