跳转到主要内容

mypy支持zope接口的插件

项目描述

用于支持zope.interface的mypy插件

Build Status Coverage Status Checked with mypy

目标是使zope接口在mypy意义上被视为类型。

使用方法

安装mypy和mypy-zope

pip install mypy-zope

编辑项目中的mypy.ini文件以启用插件

[mypy]
namespace_packages=True
plugins=mypy_zope:plugin

完成!现在您可以使用mypy检查项目

mypy your-project-dir

支持什么?

您可以浏览 示例文件 以了解支持的功能以及如何处理它们。

接口声明

您可以定义接口并提供实现

class IAnimal(zope.interface.Interface):
    def say() -> None:
        pass

@zope.interface.implementer(IAnimal)
class Cow(object):
    def say(self) -> None:
        print("Moooo")

animal: IAnimal = Cow()
animal.say()

接口 IAnimal 将被视为实现 Cow 的超类:您将能够将实现传递给接受接口的函数,并使用所有常规的多态技巧。

也可以使用具有与@imlementer装饰器相同效果的classImplements函数来声明实现。如果无法控制定义实现类的代码,这将非常有用。

classImplements(Cow, IAnimal)

animal: IAnimal = Cow()

模式字段类型推断

也支持将属性定义为zope.schema.Field

class IAnimal(zope.interface.Interface):
    number_of_legs = zope.schema.Int(title="Number of legs")

@zope.interface.implementer(IAnimal)
class Cow(object):
    number_of_legs = 4

在接口的上下文中,一些已知的zope.schema字段类型会自动转换为Python类型,因此在上面示例中,number_of_legs属性的类型是int。这意味着如果尝试将字符串赋值给IAnimal类型的实例的该属性,mypy将报告错误。自定义字段或插件无法识别的字段将具有Any类型。

字段属性

由于类型信息不会从接口传递到实现属性,因此对zope.schema.FieldProperty的支持有限,但mypy不会在类似这样的源上报告错误。

class IAnimal(zope.interface.Interface):
    number_of_legs = zope.schema.Int(title="Number of legs")

@zope.interface.implementer(IAnimal)
class Cow(object):
    number_of_legs = zope.schema.FieldProperty(IAnimal['number_of_legs'])

在这种情况下,Cow.number_of_legs的类型将成为Any,即使IAnimal.number_of_legs会被推断为int

适配器模式

可以通过这种方式调用Zope接口以查找适配器。

class IEUPowerSocket(zope.interface.Interface):
    def fit():
        pass

adapter = IEUPowerSocket(us_plug)
adapter.fit()

adapter变量的类型将被设置为IEUPowerSocket

条件类型推断

当在if语句中使用zope.interfaceimplementedBy()providedBy()方法时,mypy将知道这些语句中的类型。

if IAnimal.providedBy(ob):
    ob.number_of_legs += 2

在接口中声明重载方法

与常规的重载函数类似,接口中也支持@overload声明。

class IAnimal(zope.interface.Interface):
    @overload
    def say() -> str:
        ...

    @overload
    def say(count: int) -> List[str]:
        ...

    def say(count: int = None) -> Union[str, List[str]]:
        pass


@zope.interface.implementer(IAnimal)
class Cow(object):
    @overload
    def say(self) -> str:
        ...

    @overload
    def say(self, count: int) -> List[str]:
        ...

    def say(self, count: int = None) -> Union[str, List[str]]:
        if count is None:
            return "Mooo"
        return ["Mooo"] * count

zope.interface和zope.schema的类型存根

mypy-zope提供了zope.interfacezope.schema包的类型存根(*.pyi文件)。一旦启用插件,它们就会自动启用。

不支持什么?

以下zope.interface功能不受支持:

  • 将模块声明为接口实现者。
  • zope.schema.Listzope.schema.Dict字段进行类型推断。
  • 存根文件大部分不完整。
  • 接口兼容性检查器不会检查非方法属性的类型。

即可使用!

目前,该项目在各种大型项目中投入生产使用,并被认为是生产级别的,然而可能仍存在一些微小的错误。欢迎提出建议和pull请求!

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分布

mypy_zope-1.0.5.tar.gz (33.9 kB 查看哈希值)

上传时间:

由以下支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面