【发布时间】:2021-06-05 13:42:37
【问题描述】:
我正在寻找一个 Python linter,它可以根据类型注释检查覆盖函数返回类型。 示例:
from abc import ABC, abstractmethod
from typing import List, Dict
class A(ABC):
""" Some interface """
@abstractmethod
def f(self) -> List[str]:
pass
class B(A):
""" Interface implementation """
def f(self):
return dict() # Identify this case
class C(A):
""" Interface implementation """
def f(self) -> Dict[str, str]:
return dict() # Identify this case
我检查了 flake8、mypy 和 pylint
---- 编辑----
我故意错过了注释。 mypy 和 flake8 可以识别何时有函数返回注释。 我想确定当我定义一些接口(A类)并且可以强制从继承类返回函数类型时的情况
【问题讨论】:
-
您的问题与 mypy 文档 mypy.readthedocs.io/en/stable/… 中的示例完全相同,在这种情况下,Mypy 不会产生错误,因为函数 B.f 是动态类型的,因此您应该明确使用类型提示。
-
这正是我想要确定的。假设 A 类是 API,我想强制返回值类型而不强制注释函数