具有更具体参数的python抽象方法

2024-09-27 09:34:19 发布

您现在位置:Python中文网/ 问答频道 /正文

在以更明确的方式实现abstractmethod时,我收到一个类型警告,为什么这是错误的

我有以下接口

from typing import Any
from abc import abstractmethod, ABCMeta

class SaveDataInterface(metaclass=ABCMeta):
    @abstractmethod
    def save_data(self, data: Any, *args, **kwargs):
        ...

在实现以下FileSaver类时,mypy抛出一个错误
error: Signature of "save_data" incompatible with supertype "SaveDataInterface"

class FileSaver(SaveDataInterface):
    def save_data(self, data: str, file_path: str, *args, **kwargs):
        with open(file_path, 'w') as file:
            file.write(data)

在我看来FileSaver.save_data并没有以某种方式破坏abstractmethod{}功能,有没有其他方法来实现更显式的函数签名


Tags: fromimportselfdatasavedef错误方式

热门问题