如何记录变量的类型,它可以是实现 Python 中接口的任何类型的对象?

How to document a type of the variable that can be an object of any kind that implements an interface in Python?

我有如下结构的代码:

接口ISomething(即基础抽象class):

class ISomething:
    # some methods that should be implemented in classes
    # that implement this interface

实现 ISomething 接口的各种 classes:

class SomethingA(ISomething):
    # implementation of the interface

class SomethingB(ISomething):
    # implementation of the interface
(...)
class SomethingZ(ISomething):
    # implementation of the interface

我的另一个 class 需要构造函数中 classes SomethingA, SomethingB, ..., SomethingZ 之一的对象:

def __init__(self, something):
    '''
    Constructor

    :param something: Param description
    :type something: *Anything that implements ISomething interface*
    '''

    self._something = something

如您所见,我使用 Sphing Docstring。但是,我正在寻找一般答案。

我的问题是:如何记录一个变量是实现 ISomething 接口的任何 class 的对象。

目前,我记录这个变量的类型是"one from the list",即:

:type something: SomethingA|SomethingB|...|SomethingZ

但是,我不确定这是否是最佳方法。我觉得还是放笼统一点比较好。

我会使用 :precondition:

:前提条件: isinstance(something, ISomething)

因为在 Python 中一个 'interface' 只是一个 class 而其他 class 是子 class,可能只说 :type something: ISomething.

完整:

class ISomething:
    # some methods that should be implemented in classes
    # that implement this interface

class SomethingA(ISomething):
    # implementation of the interface

class SomethingB(ISomething):
    # implementation of the interface

class OtherClass:
    def __init__(self, something):
        '''
        Constructor

        :param something: Param description
        :type something: ISomething
        '''

       self._something = something